How To Set Up Apache2 Virtual Hosts on Ubuntu

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

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.04 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: Setting Up Document Root

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 and our directory structure will be

/var/www/
├── yourdomain1.com
│   └── public_html
├── yourdomain2.com
│   └── public_html
├── yourdomain3.com
│   └── public_html

We will create a directory here for the virtual hosts we plan on making. Within each of these directories, we will create a public_html 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/yourdomain1.com/public_html
sudo mkdir -p /var/www/yourdomain2.com/public_html
sudo mkdir -p /var/www/yourdomain3.com/public_html

The -p flag tells mkdir to create any necessary parent directories along the way:

Step 2: Grant Permissions

Since the commands above are executed as a sudo user, the newly created files and directories are owned by the root user.

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

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

Step 3: Creating Virtual Hosts

On Ubuntu, Apache2 Virtual Hosts configuration files are located in /etc/apache2/sites-available directory. They can be enabled by creating symbolic links to the /etc/apache2/sites-enabled directory, which Apache2 reads during the startup.

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/yourdomain1.com/public_html>
    Options -Indexes +FollowSymLinks
    AllowOverride All
</Directory>
<VirtualHost *:80>
    ServerName yourdomain1.com
    ServerAlias www.yourdomain1.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/yourdomain1.com/public_html

    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 should now have the ability 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.

If you are facing any problems, feel free to leave a comment.

Related Post

How to Install WordPress 5.2.3 on Ubuntu with Apache2, MariaDB and PHP 7.2

Here is how you can install WordPress 5.2.3 on Ubuntu. In particular on Vultr’s instance i.e. Ubuntu 16.04 LTS / 18.04 LTS / 18.10.

How to Install WordPress on Ubuntu with Nginx, MariaDB and PHP-FPM

Here is how you can install WordPress on Ubuntu with Nginx, MariaDB and PHP-FPM. In particular on Vultr’s instance i.e. Ubuntu 19.10.

Leave a Reply