Hosting Multiple Sites on the Same Server Using Apache2

I just set up an LAMP stack on my old T42 laptop, and decided to use this to host a few websites.

Most of the tutorials and info I came across showed me how to host a single site, but I want to host multiple websites from my new server. Here’s what I came up with:

Apache (very) Basics

Before starting, here are some important commands, concepts, and folders to be aware of:

  • ‘/etc/init.d/apache2′ – The path to the Apache2 web server
  • ‘/etc/apache2′ – The Apache2 configuration folder
  • ‘/etc/apache2/sites-available’ – Create your site configuration files in this folder
  • ‘/etc/apache2/sites-enabled’ – Folder containing symlinks to the currently enabled available sites
  • ‘a2ensite’ – an Apache utility to enable sites
  • ‘a2dissite’ – an Apache utility to disable sites
  • Virtual Hosts – Apache-speak for each of the sites running on the server.

In a virgin Apache configuration, the /etc/apache2/sites-available folder will contain one or two default configuration files. These point to the operating system’s default web path, which will contain a basic ‘index.html’ file to test that the server is configured correctly. On my Ubuntu 14.04.3 Trusty Tahr distribution, the default path is /var/www/html/.

Apache needs to be configured to start automatically – make sure this is set to do so.

Also, the router needs to forward port 80 to the server – this is the port to which Apache is listening.

Gaining access to my router is easy – just type 192.168.0.1 into a browser to go to the login screen, then set Port 80 to forward to the IP address for the server computer, which in my case is 192.168.0.12.

My router uses DCHP to dynamically assign IP addresses to machines. This means it could assign a different address to the server each time the router is restarted. On my router (Hitron), I was able to ‘reserve’ the IP address for the server machine so that I don’t need to reset this after each router reboot (effectively making it a ‘static’ IP address).

Next I needed to get my external IP address using https://www.whatismyip.com.

I then went to GoDaddy where I manage the zone files for my domain names, and set the domains to point to this external IP address so they can make their way to the server.

Preparing Apache

First thing is to disable the default site. Look in the ‘sites-enabled’ folder – there should be a symlink to ‘default’, or sometimes ‘000-default’. Use ‘a2dissite’ to disable this default site. (Note this step is not absolutely necessary, but at least the current index page should be replaced with something friendlier than the Apache test page.)

Root privileges are needed for almost everything here, so try switching to root with ‘sudo -s’ to save some typing. At the command prompt:

$ sudo a2dissite 000-default

Apache then prompts to reload the configuration files to enact the change. Type:

$ sudo service apache2 reload

Apache will reload its configuration files, this time with no sites enabled.

Start by getting just one site up and running, and then adding more in turn when the new configuration is confirmed to work properly.

Writing Apache Configuration Files

Each site hosted using Apache needs a configuration file. These files need to be created in the ‘sites-available’ folder and can be created using any text editor.

The first site I wanted to host is ‘camperella.com’. To do this, I create a new configuration file in /etc/apache2/sites-available/ called ‘camperella.com.conf’. This file can be named anything so long as it ends with .conf.

$ sudo nano camperella.com.conf

Now type in the script to serve up the domain as so:

Screenshot_110115_041535_PM

Let’s go through it line by line.

ServerAdmin: This is what Apache will report as the email address of the site’s admin.

ServerName: The name of the domain this website will respond to.

ServerAlias: Other domains can be included as aliases that will also resolve to this website (useful if someone misses www, or if you want to add any subdomains).

DocumentRoot: Write the path to the folder on server machine that contains the website.

Enabling The New Site

Now it’s time to actually enable the site. At the command prompt:

$ sudo a2ensite camperella.com.conf

Apache will now configure itself to read from the new config file. This will create a symlink in ‘sites-enabled’ to the relevant config file which Apache can now read from.

Now, reload the config files, and for good measure restart Apache as well:

$ sudo service apache2 reload

$ sudo service apache2 restart

When the server restarts, type the domain name into a web browser and check that everything worked. (This will require at least an index.html page in the folder for the website)

Adding More Sites

Using our original site as a template, it is possible to add as many sites as we want. Let’s add a second site, acmeartgallery.com.

First, copy the camperella config file into the same folder but with the name of the new site:

# sudo cp camperella.com acmeartgallery.com

Now, edit the new file, updating the original entries with the appropriate domain and content path for the new site:

# sudo nano acmeartgallery.com

Screenshot_110115_042921_PM

Note the new ServerAdmin, ServerName and Alias information, as well as the local path where this new website is kept.

Now, enable the site, reload the config files, and restart the Apache server:

$ sudo a2ensite acmeartgallery.com.conf

$ sudo service apache2 reload

$ sudo service apache2 restart

Again, when the server restarts, type the domain name into a web browser and check that everything worked.

References