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 www.example.com and www.example.org.

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"
    ServerName www.example.com

    # Other directives here
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/www/example2"
    ServerName www.example.org

    # Other directives here
</VirtualHost>

The asterisks match all addresses, so the main server serves no requests. Due to the fact that the virtual host with ServerName www.example.com 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/arso.us.to.conf
<VirtualHost *:80>
    ServerName arso.us.to
    ServerAlias www.arso.us.to
    DocumentRoot /var/www/html/arso
    <Directory /var/www/html/arso>
    	Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all 
    </Directory>
</VirtualHost>

Replace aruba.us.to 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 arso.us.to.conf
sudo service apache2 reload

About the Author

Leave a Reply

Your email address will not be published.

%d bloggers like this: