How To setup Apache Virtual Hosts On Ubuntu 18

Introduction

Apache is one of the best web servers that accounts for more than 50% of all active websites and applications on the internet. It is a powerful and flexible package that enables users to deliver content on the web without a struggle.

Apache web server breaks its components and functionality into minute units that can be configured and customized independently. One of these units is the Apache Virtual Host; a component used to describe a domain or a website.

Apache Virtual Hosts enable you to handle multiple websites on one VPS or dedicated server. They permit you to stipulate your site document root, use unique SSL certificate for each site, create different security policies for each domain, and much more.

This tutorial will help you create Apache Virtual Host on your Ubuntu 18.04 system.

Ready? Let’s get started!

Before You Start

For this installation to work out perfectly, you require the following:

  • A domain pointing to a public server Internet Protocol. This tutorial will utilize the domain example.com. (Kindly use your domain name)
  • Fully configured Apache o your Ubuntu 18.04
  • Sudo user with non-root privileges

Step 1 - Creating A Directory Structure

The first step when setting up Apache Virtual Host is to define the directory structure. The spine of our directory structure will be a document root; a location where all the website files for our domain will be stored. The document root can be set to any location, but in our guide we’ll utilize the structure below:

/var/www/
├── domain1.com
│   └── public_html
├── domain2.com
│   └── public_html
├── domain3.com
│   └── public_html

Basically, we should create unique directory inside /var/www for every domain to be hosted on our server. Then, within each the directories, we generate another directory; public_html, that will be used to save the websites files for the domain.

First execute the command below to create a document root directory for example.com:

$ sudo mkdir -p /var/www/example.com/public_html

Next, we’ll create a file; index.html, inside the document root directory of our domain. Then open the file using a nano editor.

 $ sudo nano /var/www/example.com/public_html/index.html

Add the content below to create a demo file:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

Note: We are executing the commands in our tutorial as sudo users. Also, the directories and files we have created are owned by a root use. This may result to permission issues and it's always wise to modify the ownership of your document root directory to  www-data (Apache User). Run the command:

$ sudo chown -R www-data: /var/www/example.com

That’s it! The ownership is changed and you should anticipate no permission issues.

Step 2 - Creating Apache Virtual Hosts

Ubuntu systems store the configuration files of Apache Virtual Hosts in the directory; /etc/apache2/sites-available. This means that creating symlinks to the directory; /etc/apache2/sites-enabled, can easily enable these configuration files. Now, run the command below to open a configuration file:

$ sudo nano /etc/apache2/sites-available/example.com.conf

Add the content below to create a basic configuration file for the Virtual Host:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverrideAll
    </Directory>

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

Here is an explanation of the content featured in theConfiguration file:

  • ServerName: This your domain name
  • ServerAlias: This value represents all the other domains, such as your www subdomains.
  • DocumentRoot: This the directory used by Apache to serve domain files.
  • Options: This is a directory used to determine the server features present in a directory. The -Indexes bar directory listings whereas, FollowSymLinks enables Apache to use the symlinks.
  • AllowOverride: As the name suggests, this part stipulates which .htaccess can override configuration directives.
  • ErrorLog, CustomLog: Stipulates the log files’ location.

There is no definite formula for the naming of the configuration files. However, it's always wise to name the configuration file for Apache Virtual Host, using your domain name.

Now, we need to enable the new Apache Virtual Host file. We’ll create a symlink (symbolic link) from the Apache virtual host file to the directory; sites-enabled. Let’s use the the a2ensite helper to enable the virtual host file:

$ sudo a2ensite example.com

Alternatively, create the symlink manually:

$ sudo ln -s /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-enabled/

Once that is done, execute the command below to verify the syntax:

$ sudo apachectl configtest

This will give you the output below:

Syntax OK

Now, restart your Apache2 service to implement the changes:

$ sudo systemctl restart apache2

Finally, go to your web browser and search http://example.com. This will give you the following output:

How To setup Apache Virtual Hosts On Ubuntu 18

Conclusion

Congratulations! You have successfully set up an Apache Virtual Host on your Ubuntu 18.04 virtual server. If you have multiple domains and would like to create Apache Virtual Hosts for them, repeat the steps outlined in the tutorial for each of the domain.

Check out these top 3 Linux hosting services

Was this article helpful?