Virtual Hosting Setup - Windows 98/XP & Linux with Apache

NYPHP - PHundamentals

PROBLEM:

You'd like to have two (or more) client projects located within the DocumentRoot directory (/var/www/html or whatever directory you may use for DocumentRoot) of your Linux server but you want to keep their files separate. So you place the files in separate subdirectories (/var/www/html/myproj1 and /var/www/html/myproj2). But you run into an interesting problem. In "myproj1" your web pages have a link to the project's home page and this home page is normally located, not in /var/www/html/myproj1 but in /var/www/html. That is, it is normally located in your default DocumentRoot directory. You could set all of the home page links to refer to this subdirectory but when you move your code from your development server to your client's server, all links referring to the "home" page will refer to the "myproj1" subdirectory and not the default DocumentRoot (see Note 1).

One way to handle this problem and to have all of your page links work correctly is to set up virtual hosts. Virtual hosts will resolve all of the different path issues and you can develop your project as if it were the only project on your server. You'll have eliminated the need to worry about your home page being in a subdirectory of DocumentRoot. Therefore, different projects will be in their own subdirectories under /var/www/html and the links will work correctly both on your development server and on your client's server.

The solution provided below assumes that you want to set up virtual hosting within a networked office and that your working environment (that is, your development environment) consists of:

  1. Windows 98 or Windows XP on your desktop
  2. A Linux server that is accessed through a fixed IP address. That is, you view projects through a browser by accessing it with a single fixed IP address, e.g., http://192.168.1.111.

SOLUTION:

1. Make sure you have administrative rights on Windows XP and superuser (a/k/a "root") access on the Linux server.

2. You will need to change two files:

  1. On Windows XP: "hosts," located in c:\windows\system32\drivers\etc\. (On Windows 98: the file is located in c:\windows\.)
  2. On Linux: "httpd.conf," located in /etc/httpd/conf.

3. In the "hosts" file the first line (localhost) already exists. Add one or more lines with the IP address of your Linux box and the name you want to use to access the project. For example, since my Linux box has an IP address of 192.168.1.111, I added the line "192.168.1.111 myproj1" and then added "192.168.1.111 myproj2" for the second project. (see Note 2)

127.0.0.1 localhost
192.168.1.111 myproj1
192.168.1.111 myproj2

4. In httpd.conf you set the following:

  1. ServerName: Using my IP address as an example, this section of the Apache configuration file would read "ServerName 192.168.1.111:80" where the IP address is the one for my Linux box and the "80" refers to the standard Port 80 for http access.
  2. NameVirtualHost: again, note the IP address and port number.
       NameVirtualHost 192.168.1.111:80
  3. Virtual Host: The first listing below is the "default" virtual host. The other two listings represent two different client projects.
<VirtualHost 192.168.1.111:80>
        DocumentRoot /var/www/html
        ServerName linux
</VirtualHost>

<VirtualHost 192.168.1.111>
        DocumentRoot /var/www/html/myproj1
        ServerName myproj1
</VirtualHost>

<VirtualHost 192.168.1.111>
        DocumentRoot /var/www/html/jeff
        ServerName myproj2
</VirtualHost>

Note that the name of the subdirectory for "myproj2" does not match the subdirectory name. The DocumentRoot is just the actual path to the files whereas the ServerName is the name you would use in your browser to access the files. (see Note 3) Safety Note: You may want to make a backup copy of your httpd.conf file in case you make a serious error. You can use the command "cp httpd.conf httpd.conf.bak" to make a copy of the configuration file.

5. Restart Apache. (You can use the command "apachectl graceful" to restart Apache.)

TESTING:

Create two different PHP files (or regular HTML files) and place one in the directory /var/www/html/myproj1 and the other in the directory /var/www/html/jeff. If you set up everything correctly, you should be able to see the first web document using the URL http://myproj1 and the second web document using the URL http://myproj2.

Basic Definitions:

DocumentRoot: the default directory used by Apache to serve web documents. When a web browser points to a domain, whether it is accessed by a domain name such as "http://www.mydomain.com" or by an IP address such as "http://192.168.1.111," Apache will send back to the web browser the document it finds in the DocumentRoot directory.

VirtualHost: When more than one website is on a single web server, each website is handled by a "virtual host." That is, from the perspective of the website visitor, it looks as if each website resides on its own webserver. In actuality, these virtual hosts reside in separate subdirectories on the very same webserver. The VirtualHost settings tell Apache that, when someone wants to view http://myproj1, its default DocumentRoot directory is /var/www/html/myproj1. When someone wants to view http://myproj2, they tell Apache that its default DocumentRoot directory is /var/www/html/jeff.

ServerName: The name you want to use when viewing the project through a browser. Therefore, while one of your projects may be physically located in the subdirectory "/var/www/html/jeff", when you want to view the project through your web browser, you want the project to be referred to as "myproj2."

Notes:

1- We are assuming that your client's server does not have virtual hosts set up.

2- When you enter a URL in your web browser, your browser first checks the list to see if there is an IP address associated with the URL you entered. In other words, if you entered the URL http://www.google.com your browser goes out to a DNS (Domain Name Server) to find the associated IP address (which happens to be 216.239.53.100) and sends a request to the correct web server. So, in this case, when you enter the URL http://myproj1, the browser first checks the "hosts" file. If it finds a match the appropriate IP address is returned and a request is sent to the correct webserver.

3- Though this article has emphasized having the actual paths for the files under the DocumentRoot directory, keep in mind that VirtualHosts do not necessarily have to be subdirectories of the original document root. For example, one VirtualHost can have a DocumentRoot path of /home/var/www, while a different VirtualHost can have a DocumentRoot of /var/dev/site.


Contributors to this note include the following:

  • Jon Baer
  • Raymond DeRoo
  • Daniel Kushner
  • Chris Snyder
  • Michael Southwell
  • Marc Antony Vose
  • Hans Zaunere
  • the PHundamentals team: Jeff Siegel, Mike Southwell