How to Host Multiple Websites with Apache 2.4

Mark Korsak
Linode Cube
Published in
3 min readJun 28, 2016

Utilizing name-based virtual hosting on your web server allows you to host multiple websites all from the same server. Even a single 1GB Linode can support many websites, depending on the amount of content and visitors. Hosting more than one website on a server can help save on costs and more efficiently use the server space you have.

All instructions in this guide are written for Ubuntu or Debian distributions of Linux.

What is Name-Based Virtual Hosting?

Virtual hosts are separate configurations of websites within a single server, essentially splitting the server into multiple “virtual” website hosts. Name-based virtual hosts are designated by whichever domain name accesses the server. Two domain names — say, example.com and test.com — can both point to the same IP address of the web server, and then your Apache service will load the appropriate site based on the domain name used.

If a user accesses the server with example.com, it will check the configuration for that domain name and load the document root listed there; otherwise, if a user accesses the server with test.com, it will read that configuration file and load that document root. Alternately, multiple websites from a single server can be hosted through IP-based virtual hosting, which lets you load configuration files based on which IP address of the server is being used. However, it is best practice to minimize the number of IP addresses on a server and use name based-virtual hosting, instead.

Differences between Apache 2.2 & 2.4

Apache launched version 2.4 of its popular web server in 2012, the software’s first major update in over 6 years. This update changed how virtual host configuration files were handled. Consequently, it wasn’t uncommon for websites to run into issues as the new web server was implemented into newer versions of popular Linux distributions.

One of the major changes to keep in mind when creating virtual host configurations is that all configuration files must end in “.conf.” This wasn’t the case before Apache 2.4, so if your configuration filenames didn’t end in .conf, you could find your sites no longer working after the update.

Another major change was that every configuration file requires the line “Require all granted” inserted into an appropriate directory block. More details on the update changes between Apache 2.2 to 2.4 and how to handle them can be found here: https://www.linode.com/docs/security/upgrading/updating-virtual-host-settings-from-apache-2-2-to-apache-2-4

Create Your Virtual Host Configuration Files

Inside your “sites-available” folder you’ll need to create a configuration file for each website you plan to host. You can name this file anything you wish, provided that it ends with “.conf.” I would recommend keeping it simple enough to remember, such as “example.com.conf” (replacing example.com with the website you are making this for).

Inside the file you will provide the configuration needed for your web server to know what to do with the request. You can follow this example configuration file here:

<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName www.example.com
ServerAlias example.com

DirectoryIndex index.html index.php
DocumentRoot /var/www/html/example.com/public_html
LogLevel warn
ErrorLog /var/www/html/example.com/log/error.log
CustomLog /var/www/html/example.com/log/access.log combined
</VirtualHost>

In the above example you’ll need to replace the ServerAdmin with your email address. Likewise, replace the ServerName and ServerAlias with the domain name you wish to use, and the correct folder for your DocumentRoot, ErrorLog, and CustomLog. You can find more information about what options you can include here: http://httpd.apache.org/docs/current/sections.html

Enable Your Virtual Hosts

After each virtual host is created, you’ll need to enable it. You can use the following command to enable each configuration file, replacing the example filename with the name of the new configuration file:

a2ensite example.com.conf

After this, you’ll need to restart your web server:

service apache2 reload

Pending no errors, once your web server is restarted, all websites from your configuration files will be served online. If DNS is set-up and propagated, you’ll be able to visit each of those sites now in your web browser!

If you run into any issues with this, I suggest reading the troubleshooting guide we have available from Linode: https://www.linode.com/docs/troubleshooting/troubleshooting-common-apache-issues

--

--

Mark Korsak
Linode Cube

Owner of @CLASHTournament | Nat'l Esports Event Host and Media Producer | @ScreenwaveMeida Esports Coordinator