Host multiple domains on one server with Apache

Apache allows you to host multiple domain names from a single IP address using Virtual Hosts. I’ll show you how to edit Apaches sites-enabled file to do this. First, open the 000-default file in /etc/apache2/sites-enabled/. It should look like this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

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

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

    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 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

We will want to add the following section to the bottom:

<VirtualHost *:80>
DocumentRoot /var/www/directoryWithSiteFiles
ServerName domainname.com
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/directoryWithSiteFiles>
    Options Indexes FollowSymLinks MultiViews ExecCGI Includes
    AllowOverride None
                Order allow,deny
                Allow from all
        </Directory>
# Other directives here
</VirtualHost>

replacing domain.com with your domain name and directoryWithSiteFiles with the directory that holds the files you want hosted at that domain. Notice that this is for the naked domain domain.com. To also host www.domain.com, you will need another section added on like this:

<VirtualHost *:80>
DocumentRoot /var/www/directoryWithSiteFiles
ServerName www.domainname.com
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/directoryWithSiteFiles>
    Options Indexes FollowSymLinks MultiViews ExecCGI Includes
    AllowOverride None
                Order allow,deny
                Allow from all
        </Directory>
# Other directives here
</VirtualHost>

You can do this for multiple sites (I have three on my server right now). Remember: you will have to change your DNS server to point to the IP of this server, as well.