Setting Up Multiple Name Based Websites in Apache2

by bitznbitez

Since I’m in the middle of setting up a new system and a new apache2 webserver I’ll outline the steps to setup multiple name based websites in apache2 using the same ip and port.   In this example I’m setting up a development environment and using localhost port 80.   You can adjust these instructions if you are setting up a real webserver on other ips or other ports etc.

First we need to install apache2 if not already installed

apt-get install apache2

Now we can work on configuring a new virtual host.   We begin by becoming root and changing our directory to /etc/apache2

Next we enable virtual hosts by editing conf.d/virtual.conf and adding the following :

# Enable listening for multiple virtual hosts
NameVirtualHost *:80

NOTE: Do not use NameVirtualHost * without the port or you will get errors like the following when restarting/reloading apache2 :

[Fri Apr 06 00:58:22 2012] [error] VirtualHost *:80 -- mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results

Now cd into sites-available, copy the default config to your new virtual host name, in this case mysite_dev.   I have bolded the lines you change/add.   You will use whatever directory path is the one to where you created the directory tree for your new website.

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName mysite_dev

        DocumentRoot /local/www/mysite_dev
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        <Directory /local/www/mysite_dev/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory “/usr/lib/cgi-bin”>
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ “/usr/share/doc/”
    <Directory “/usr/share/doc/”>
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from ::1/128


At this point you should edit your /etc/hosts file and add the name mysite_dev as an alias on the localhost line. localhost mysite_dev

You have now created the site in the sites-available directory but you need to create a symbolic link for the file in sites-enabled directory as well.    You can/should learn how to use ln -s to do this but if you prefer use the apache2 command….

a2ensite mysite_dev

At this point you will be instructed to reload apache by the command.

service apache2 reload

If on reload etc. you see the warning

apache2: Could not reliably determine the server's fully qualified domain name

You will need to create the file /etc/apache2/conf.d/fqdn with the following

ServerName localhost

Try another reload again….

service apache2 reload

At this point you should have a working website that you can access as http://mysite_dev/ on your system.