Setting Up A Virtual Host In Apache

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Setting Up A Virtual Host In Apache as PDF for free.

More details

  • Words: 1,292
  • Pages: 4
Setting Up A Virtual Host in Apache Setting up a virtual host in the Apache web server is not exactly a PHP topic, but many PHP developers use the Apache web server to test web pages on their development machine. There is a lot of information around on how to do this, but the first time I tried it, I found the existing information to be more confusing than helpful. Hopefully, this page will simplify the process a bit. Please note that this information pertains to setting up a virtual host in Apache on a Windows machine for use as a local testing server. Setting up a virtual host for an actual production server is beyond the scope of this article and you should refer to the official Apache documentation for that.

Configuring Apache The first file we'll need to edit is the Apache httpd.conf file. If you installed the Apache software using the download from the Apache web site, you should have a menu item that will open this file for editing. Click Start->Programs->Apache HTTP Server>Configure Apache Server->Edit the Apache httpd.conf Configuration File. If you don't have that start menu item, start your text editor and open the file. It will be in a sub-folder named conf of your Apache folder. For example, mine is here: C:\Program Files\Apache Group\Apache\conf\httpd.conf

Notes for Apache Server Versions Since 2.2 Configuration Note that Apache changed the preferred method for configuring the Apache server with the release of Apache 2.2. For versions beginning with 2.2, the peferred configuration is more modular. Setting up a virtual host as described here will still work with the newer versions, but to follow the modular approach, the editing of httpd.conf is only to uncomment (remove the # from the beginning of the following line: #Include conf/extra/httpd-vhosts.conf

Everything else is entered in the file httpd-vhosts.conf, which will be located in the extra folder below the below the folder containing httpd.conf. As mentioned, the method described here will still work.

Security

Version 2.2 also changed some of the default security configuration parameters. To set things up the way you'll need them, you'll need to add the following block to either your httpd.conf file, just above the virtual hosts, or to your httpd-vhosts.conf file: Order Deny,Allow Allow from all

The above assumes you're using the directory structure described below. Adjust that as necessary to reflect your actual directory. Now, for this example, we'll assume that you have your web sites located in a folder on your C drive called My Sites. Each web site has a sub-folder of its own under that folder, like this: C:\My Sites\Site1 C:\My Sites\Site2

Say also, for this example, that the domains for the two sites are site1.com and site2.com. We're going to set up virtual hosts for those two sites using the domain names site1.local and site2.local. That way, you'll be able to tell at a glance whether you're looking at the live site, or your testing site. In reality, you can call the domains anything you want. You could just as easily name them microsoft.monkeybutt and ibm.greentambourine. I choose to use the convention of using the same domain name along with the .local TLD to simplify and minimize the typing needed to switch between the live site and the testing site. The only important point, and it's really important, is that you NEVER use an actual, real, live domain name. If you used, for example, site1.com for the local virtual host, you would never be able to actually reach the live site. All requests for the live site would be re-directed to your local virtual host. Go to the very bottom of your httpd.conf file in your text editor. You should see an example of a virtual host there. Each line of that example will begin with an octothorpe (#). The octothorpe character marks the line as a comment, so the example is not executed. Add the following lines below that example: NameVirtualHost 127.0.0.1 DocumentRoot "C:\My Sites\Site1" ServerName site1.local DocumentRoot "C:\My Sites\Site2" ServerName site2.local

That's all you need to do! Save and close the file. That will tell the Apache server everything it needs to know in order for it to serve the pages using the domain names site1.local and site2.local. One note is that, in the above example, we have a space in the path. Because of that, we put quotation marks around the document root directory. If the path does not have any spaces in it, do not quote the path. If the directory used for your sites were, for example MySites instead of My Sites, the document root line would look like this instead: DocumentRoot C:\MySites\Site1

Resolving the DNS issue Obviously, if you typed http://site1.local in your browser, it would not be found by your Internet provider's DNS server. We're next going to edit another file to work around that. The second file you need to edit is called hosts, with no file extension. It is a Windows system file and it will enable you to enter specific addresses for specific domains instead of using a DNS lookup. The normal location for this file is: C:\WINNT\system32\drivers\etc\hosts

or C:\Windows\system32\drivers\etc\hosts

If you don't find it there, do a search in your windows directory for the word hosts in the file name. The file you want is called hosts, with no file extension. The correct file will begin with the following lines: # Copyright (c) 1993-1999 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.

Once again, in this file, the octothorpe character is a comment marker. Lines beginning with it are comments. In all likelihood, there will be nothing there, except for comments. If there are any other non-commented entries, leave them alone. Just go to the bottom of the file, below all the comments and any existing entries and add the following two lines: 127.0.0.1 site1.local 127.0.0.1 site2.local

That's all you need to do there. Save and close the hosts file. You're almost done! The only remaining thing you need to do is to re-start the Apache server. You need to do this because Apache only reads the configuration file when it first starts up. Click Start->Programs->Apache HTTP Server->Control Apache Server-

>Restart. If you don't have that menu item, open a command prompt and change to the Apache directory, and type the following command and press the Enter key: apache -w -n "Apache" -k restart

You should see a message like this: The Apache service is restarting. The Apache service has restarted.

That's it! You're done! Close the command window and start your web browser. In the browser's address bar, type http://site1.local and hit the Enter key. You should now see your local copy of your site1. Okay, now I'll mention one very small, but possibly important, caveat. When you create the virtual hosts like this, the default http://localhost will no longer work. In many cases, that is unimportant. However, if you're using something like phpMyAdmin, you'll still need it. The solution to that is to create one additional virtual host called "localhost" that points to the original Apache htdocs folder. It might look something like this: DocumentRoot C:\Apache\htdocs ServerName localhost

Don't forget to include that additional virtual host when you edit the Windows hosts file. Note that there are other optional settings you can use to configure the virtual host. The above uses only two lines and that's all that's really necessary. You can read about other options in the Apache documentation. Note that this link is to the Apache web site and it will open a new browser window. I hope you found this more helpful than confusing. Good luck!

Related Documents

Virtual Host
June 2020 12
Setting Up A Network
November 2019 32
Setting Up A Ftp
October 2019 31
Virtual Host Linux
November 2019 7
Setting Up A Classroom Blog
November 2019 20