Sunday, 30 December 2012

How To Set Up Virtual Hosting on Apache


A very fast internet connection and an Apache web server are all you need to run an Apache virtual host. You will not require you to have IP addresses for each domain because you are not using a physical location. The virtual host is able to operate because of the configurations that you make on the Apache installation. Once you include these settings in the setup, the web server is able to serve different websites based on their respective HTTP header hostnames. Here is a detailed description of how to set up virtual hosting on Apache.


Register domain and configure DNS
First, begin with a registration of the domain. This is a rather simple process that involves selecting your desired name and paying for the service. The next step is to add the new domain to the DNS server. Once you are done with that part, point it to your virtual hosting server IP and restart the DNS service.

Activate virtual host and input the necessary details for each website
Apache picks the first domain entered on its configuration as the default domain to render. You must have a virtual host configuration on the Apache httpd.conf file so that each domain receives its own logs.

The default httpd.conf file will have a line that contains "httpd-vconf.conf" line as a comment. Activate the virtual host by uncommenting this line.

Now you can add the specific details of each domain on your server by modifying the httpd-vhosts.conf file. Use the line below to make your name-based virtual host to listen to port 80:

NameVirtualHost*:80

You then need to include Apache directives for each website in the <VirtualHost*:80></VirtualHost> tags. All kinds of Apache directives will work within these two tags and you can use them freely as you wish.

Here is an example of two websites configured to use the same virtual host. The websites are mynameisgood.com and essentialpeople.com.

# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80

<VirtualHost *:80>
ServerAdmin anyone@mynameisgood.com
DocumentRoot "/usr/local/apache2/docs/mynameisgood"
ServerName mynameisgood.com
ServerAlias www.mynameisgood.com
ErrorLog "logs/mynameisgood/error_log"
CustomLog "logs/mynameisgood/access_log" common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin anyone@essentialpeople.com
DocumentRoot "/usr/local/apache2/docs/essentialpeople"
ServerName essentialpeople.com
ServerAlias www.essentialpeople.com
ErrorLog "logs/essentialpeople/error_log"
CustomLog "logs/essentialpeople/access_log" common
</VirtualHost>

The above configuration on the virtual host will enable users to receive the mynameisgood.com domain website when they type that into their browsers. In addition, all the access_log and error_log for the mynameisgood.com domain go to the directory at /usr/local/apache2/logs/mynameisgood.

After setting up the virtual host, you have to check for any error on the configuration syntax. On your shell terminal, you will need to run the "httpd-S" command. You will get a "Syntax OK" message when everything is fine; otherwise, your feedback will contain all the errors and their locations. Restart Apache after you complete all the steps outlined above.

The process of setting up an Apache virtual host is not very complicated. Just follow the steps outlined above and your virtual host will be up and running in a few minutes. You can have an IP-based virtual host or a name based virtual host. The latter is the most common choice among the webmasters community because it only requires one NIC card for the physical server. Remember to point the DNS of all the websites to the same IP address on the physical server so that the name-based virtual host works properly.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.