Running several name-based web sites on a single IP address.

If your server has multiple hostnames that resolve to a single address, and you want to respond differently for and

Creating virtual host configurations on your Apache server does not magically cause DNS entries to be created for those hostnames. You must have the names in DNS, resolving to your IP address, or nobody else will be able to see your website. You can put entries in your hosts file for local testing, but that will work only from the machine with those hosts entries

# Ensure that Apache listens on port 80
Listen 80
<VirtualHost *:80>
    DocumentRoot "/www/example1"

    # Other directives here

<VirtualHost *:80>
    DocumentRoot "/www/example2"

    # Other directives here

The asterisks match all addresses, so the main server serves no requests. Due to the fact that the virtual host with ServerName is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first <VirtualHost>.

Real world example

Create file like this in  /etc/apache2/sites-available:

#  /etc/apache2/sites-available/
<VirtualHost *:80>
    DocumentRoot /var/www/html/arso
    <Directory /var/www/html/arso>
    	Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all 

Replace with actual cite name and /var/www/html/arso with actual directory. Then enable site and restart or reload server with this commands:

sudo a2ensite
sudo service apache2 reload

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: