How To Set Up Apache2 Virtual Hosts on Ubuntu 16.04 / 18.04 / 18.10

Here is how you can set up Apache2 Virtual Hosts on Ubuntu. In particular on Vultr’s instance i.e. Ubuntu 16.04 / 18.10 and 18.04 LTS.

Apache2 Virtual Hosts can run more than one website on a single server machine. With Virtual Hosts, you can specify the multiple site document roots, define a separate security policy for each site, use different SSL certificates and much more.

Prerequisites

  • Have an Ubuntu 16.04 x64 / 18.01 x64 / 18.10 x64 instance.
  • Logged in as a root with sudo privileges.
  • You will also need to have Apache2 installed by following these instructions.

To get started with the set up of Apache2 Virtual Hosts, follow the steps below:

Step 1: Create the Directory Structure

The first step that we are going to take is to create a directory structure that will hold the website data and will be serving to visitors. The document root is the directory where the website data for a domain name is stored. You can define the document root to anywhere you want but in this article, we will use the default document root which is /var/www/html and our directory structure will be

/var/www/html/
|-- yourdomain1.com
|   |__ public
|-- yourdomain2.com
|   |__ public
|-- yourdomain3.com
|   |__ public

We will create a directory here for the virtual hosts we plan on making. Within each of these directories, we will create a public folder that will hold our actual website data. This gives us some flexibility in our hosting.

You can create directories by using the following command:

sudo mkdir -p /var/www/html/yourdomain1.com/public
sudo mkdir -p /var/www/html/yourdomain2.com/public
sudo mkdir -p /var/www/html/yourdomain3.com/public

Step 2: Grant Permissions

As we are running the commands as a sudo user and the newly created files and directories are owned by the root user.

To avoid any permission issues we can change the ownership of the domain document root directory to the apache user (www-data):

sudo chown -R www-data:www-data /var/www/html/yourdomain1.com/public
sudo chmod -R 755 /var/www/html/html/yourdomain1.com/public

Step 3: Create a Virtual Hosts

By default, Apache2 Virtual Hosts on Ubuntu configuration files are stored in /etc/apache2/sites-available directory and can be enabled by creating symbolic links to the /etc/apache2/sites-enabled directory.

Open your editor of choice and create the basic Virtual Host configuration file in /etc/apache2/sites-available/yourdomain1.com.conf with the following code.

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>
<Directory /var/www/html/yourdomain1.com/public>
    Options -Indexes +FollowSymLinks
    AllowOverride All
</Directory>
<VirtualHost *:80>
    ServerName yourdomain1.com
    ServerAlias www.yourdomain1.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/yourdomain1.com/public

    ErrorLog ${APACHE_LOG_DIR}/yourdomain1.com-error.log
    CustomLog ${APACHE_LOG_DIR}/yourdomain1.com-access.log combined
</VirtualHost>

To enable the new virtual host file we need to create a symbolic link from the virtual host file to the sites-enabled directory, which is read by apache2 during startup. To enable the virtual host is by using the a2ensite helper i.e.

sudo a2ensite yourdomain1.com
sudo systemctl restart apache2.service

Once done, test the configuration for any syntax errors with:

sudo apachectl configtest

If there are no errors you will see the following output:

Syntax OK

Now that you have your virtual hosts configured, you can test your set up by opening any browser and browse to the server domain name. You should see that everything is working correctly.

Conclusion

You have learned how to set up an apache2 virtual host configuration to host multiple domains on Ubuntu server. You can repeat the steps we outlined above and create additional virtual hosts for all your domains.

Leave your Comment