Tutorials
Posted on Jun 30, 2010 at 20:35
Apache 2 - Virtual Hosts
Edit hosts files
> vi /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 site1.com
127.0.0.1 site2.com
127.0.0.1 site3.com
Set loopback address to handle local urls of your choice. Here we are saying that site1.com, site2.com and site3.com will be the local urls used to access the three local test sites. This is handy for running sites locally for testing before deployment to a live server.
Edit apache2 vhosts configuration file
> vi apache2/conf/extras/httpd-vhosts.conf
#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
#
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host # configuration.
#
# Use name-based virtual hosting.
# NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any block.
#
<VirtualHost *:80>
DocumentRoot "/usr/local/apache2/htdocs/site1.com"
ServerName site1.com
ErrorLog "logs/site1.com-error_log"
CustomLog "logs/site1.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/usr/local/apache2/htdocs/site2.com"
ServerName site2.com
ErrorLog "logs/site2.com-error_log"
CustomLog "logs/site2.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/usr/local/apache2/htdocs/site3.com"
ServerName site3.com
ErrorLog "logs/site3.com-error_log"
CustomLog "logs/site3.com-access_log" common
</VirtualHost>
Edit the httpd-vhosts.conf file putting in a VirtualHost section for each site as shown.
Document Root = full path to site on local server
ServerName = url that will be typed in to access site
ErrorLog = log to use for error messages
CustomLog = log to use for server messages
Edit apache2 vhosts configuration file
> vi apache2/conf/extras/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache2/htdocs/site1.com"
ServerName www.site1.com
ErrorLog "logs/site1.com-error_log"
CustomLog "logs/site1.com-access_log" common
</VirtualHost>>
<VirtualHost *:80>
DocumentRoot "/usr/local/apache2/htdocs/site2.com"
ServerName www.site2.com
ErrorLog "logs/site2.com-error_log"
CustomLog "logs/site2.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/usr/local/apache2/htdocs/site3.com"
ServerName www.site3.com
ErrorLog "logs/site3.com-error_log"
CustomLog "logs/site3.com-access_log" common
</VirtualHost>
All that is required in order to get a real set of virtual websites up and running is to edit the httpd-vhosts.conf file. The only difference here from setting up a local host is you need to put in the fully qualified url in the ServerName setting as shown.