Jeremy Davis's picture

Here's how I do it with 2 separate domains both configured to point to the same IP (ie the server IP, or the router IP with port forwarding, etc.

The 2 FQDNs I will use for this example are: stuff.example1.com & morestuff.example2.net

This example is assuming a clean/default TKL LAMP install. It will apply generally to other setups but that can't 100% be guaranteed. Also this is how I do it and it works - I'm not guaranteeing that this is the best or only way! :)

So let's get to it:

For starters create your first new 'site':

nano /etc/apache2/sites-available/stuff.example1.com

This will open a blank file for editing. Inside this file paste/type this:

<VirtualHost *:80>
        ServerName stuff.example1.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/stuff.example1.com/
        <Directory /var/www/stuff.example1.com/>
                Options Indexes FollowSymLinks MultiViews
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>

Exit and save (<Ctrl><x>, <Enter>) and do the same with your second site (and subsequent sites):

nano /etc/apache2/sites-available/morestuff.example2.net
<VirtualHost *:80>
        ServerName morestuff.example2.net
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/morestuff.example2.net/
        <Directory /var/www/morestuff.example2.net/>
                Options Indexes FollowSymLinks MultiViews
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>

 

Finally enable your 2 (or more) new sites and restart Apache.

a2ensite stuff.example1.com
a2ensite morestuff.example2.net
service apache2 restart

Notes:

  • If you have a static IP you can (and probably should) change out the * for your server WAN IP address
  • This configuration leaves the direct IP of your server available, and if someone browses to it, they will see your file heirarchy. There are a number of ways to avoid this.
    • Edit the default site: Change the DocumentRoot (and <Directory ...) in the default site to point to one of your other sites.
    • Another pretty easy way is to edit the /etc/apache2/sites/available/default site and comment out the DocumentRoot line as well as the whole <Directory /var/www/>stuff in here</Directory> section (this will give you a 404 - not found error page).
    • My favourite way is to just put a placeholder html file there:
      echo "<title>Move along...</title><h1><br>Move along please, <br><br> Nothing to see here..." > /var/www/index.html
    • You could use a .htaccess file to disable access to the doc root altogether.
    • Probably lots of other ideas...