The Apache HTTP Server is one of the world’s most popular servers. The term “Virtual Host” refers to running more than one web site on a single server. For example if you had two companies, one named “Company A” and the other “Company B”, and both had domain names “www.companyA.com” and “www.companyB.com” respectively; then you could use a single server to host the web sites for both companies using the VirtualHost configuration directive of the Apache httpd server. This blog describes setting up Virtual Hosts on Apache 2.2 on a CentOS server.
Enable Named Based Virtual Hosting
There are 2 ways you can enable Virtual Hosting, one is based on name resolution, the other is IP resolution. With IP resolution, you specify the “ServerName” directive as an IP address, and then incoming requests for the specified IP address are directed to that VirtualHost. With name resolution, the name is mapped to the “ServerName”.
Given the two companies example, the following changes should be made to the “httpd.conf” file. In my case, I’m using CentOS (Linux distro) which is similar to Red Hat Enterprise Linux. Configuration files for Debian or other Linux distros might be a bit different. To edit the httpd config file, enter vi /etc/httpd/conf/httpd.conf and make the following config changes:
... NameVirtualHost *:80 ... <VirtualHost *:80> ServerAdmin myName@companyA.com DocumentRoot /var/www/html/A/web ServerName www.companyA.com <Directory /> Options Indexes FollowSymLinks MultiViews AllowOverride All </Directory> ErrorLog logs/CompanyA-error_log CustomLog logs/CompanyA-access_log common </VirtualHost> <VirtualHost *:80> ServerAdmin myName@companyB.com DocumentRoot /var/www/html/B/web ServerName www.companyB.com <Directory /> Options Indexes FollowSymLinks MultiViews AllowOverride All </Directory> ErrorLog logs/CompanyB-error_log CustomLog logs/CompanyB-access_log common </VirtualHost> ...
In the above example, there are two separate DocumentRoots which are “A/web” and “B/web” respectively for the two companies. The directives within the Directory directives I show above are typical when using a Symfony project. You may use different directives depending on your requirements.
Once you make changes to your http.conf file, you will need to restart the Apache httpd server. This is done by issuing the command service httpd restart. Make sure you get the green OK indicators.
Testing the Config
So let’s presume you’ve made the above changes in a test environment and you want to verify that it’s going to work. On another machine (or the same machine), you will need to edit the hosts file and add in the name of both “companyA” and “companyB”. On a Linux machine you will need to edit the “/etc/hosts” file, and on Windows you need to edit “C:\Windows\system32\drivers\etc\hosts”. Open that file and make the following entries:
192.168.1.20 www.companyA.com 192.168.1.20 www.companyB.com
In the above example, “192.168.1.20” is the IP address of the Apache HTTP server and “www.companyA.com” is the name that we will resolve.
Now on your test machine, open a browser and type in: http://www.companyA.com/
This will send the request to the first Virtual Host. Then if in the same browser you enter in http://www.companyB.com this will resolve to the second Virtual host.
This should be enough to get you started with Virtual Hosts. Good luck!
Can you tell us more about this? I’d like to find out some additional information.
What exactly do you need to know?