How to set up Apache virtual hosting

2376

By James Lees

If Apache is installed correctly, you can set up virtual hosting easily by editing Apache configuration files. The main .conf files in /etc/apache2 include httpd.conf, error.conf, server-tuning.conf, and a bunch more. These files are set for read-only access for normal users; you must gain root access with the sudo or su command to change them.

Edit /etc/apache2/httpd.conf and find this line:

Include /etc/apache2/vhosts.d/*.conf

Replace it with a line that specifies a .conf file we will create and call vhost.conf:

Include /etc/apache2/vhosts.d/vhost.conf

When you’ve made the change, save the file. Now, again as root, create the vhost.conf by renaming the vhost.template file in /vhosts.d/:

mv vhost.template vhost.conf

Now edit the file to set it to allow multiple name-based Web sites on a single IP address. First add in the virtual host directive with NamevirtualHost *. The asterisk is a wild card character that allows any address to be set. Under that directive you can add your first virtual host:

NamevirtualHost *ServerAdmin This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 
DocumentRoot /srv/www/htdocs/directory_of_your_choice/
ServerAlias yourdomain.com *.yourdomain.com

The first part is the opening tag for the defined virutal host block. ServerAdmin lets you display the administrator email address when an error such as a 404 occurs. DocumentRoot is the root directory for the defined site; for example, sites I have hosted in the past all had accounts set up within /srv/www/htdocs/hosted/sitename/. Next is the ServerAlias or ServerName, which controls what names people can use to see and access a site. You could set this to a directory within the main NameVirtualHost or the domain of the site so the server can display the content associated with the domain name. The reason for the two ServerAlias entries above is that servers are often called from more than one ServerName, so you set up a ServerAlias with more than one address listed. The wild card allows for any domain request with anything in front of .yourdomain.com.

What if you want to have multiple virtual hosts set up under two different NameVirtualHost blocks, with one being the server’s main IP address and the other being for the localhost? The code would look like this:

NameVirtualHost *ServerAdmin 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 
DocumentRoot /srv/www/htdocs/
ServerAlias mixfevers.com *.mixfevers.com
ServerAdmin 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 
DocumentRoot /srv/www/htdocs/snapnshare/
ServerAlias snapnshare.com *.snapnshare.com
erverAdmin 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 
DocumentRoot /srv/www/htdocs/~/boomer/
ServerAlias boomerwebzine.com *.boomerwebzine.com
NameVirtualHost 127.0.0.1   
DocumentRoot /srv/www/htdocs/   
ServerName localhost

Once you’ve saved the configuration file, you need to restart Apache as root in order for your change to take effect:

# /etc/init.d/apache2 restart

The setup above works well for a small company hosting multiple sites on one server, or for a hobbyist who designs sites and wants to host them. It allows you point your domains at a static or dynamic IP address, letting the server figure out what shows up for the domain.

Apache provides more information on setting up and maintaining different setups of virtual host in its documentation.