Google Cloud AWS

Setup Apache Virtual Hosts on Ubuntu 18.04 – Google Cloud

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

Pinterest LinkedIn Tumblr

Setup Apache Virtual Hosts on Ubuntu 18.04 – Google Cloud. Apache Virtual Hosts allows you to configure your domain name on the server with specific document root, security policies, SSL and much more.

You can also configure more than one website on a single server with multiple Virtual Host configurations.

This setup is tested on an instance running Ubuntu 18.04 with Apache 2 installed on Google Cloud. So, this guide will be useful to configure Virtual hosts on any cloud services like AWS, Azure and any VPS or Dedicated servers.


Create the Directory Structure

The document root is the directory where the website files for a domain name are stored. You can set the document root to any location you wish, in this guide you will use the following directory structure.

│   └── public
│   └── public
│   └── public

We can create a separate directory for each domain we want to host on our server inside the /var/www/html directory. Within each of these directories, we will create a public directory that will store the source code of each websites.

We shall use domain name as a demo for this setup.

sudo mkdir -p /var/www/

Now create a new index.html file inside the document root, that is /var/www/html/

Create a demo file using nano editor.

sudo nano /var/www/
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <title>Welcome to</title>
    <h1>Success! home page!</h1>

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

So you need to change the permission of the document root directory to the Apache user (www-data) .

sudo chmod -R 755 /var/www/html/
sudo chown -R www-data:www-data /var/www/html/

Create Virtual Host

By default on Ubuntu systems, Apache Virtual Hosts 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 following basic Virtual Host configuration file

sudo nano /etc/apache2/sites-available/example.conf
<VirtualHost *:80>
     ServerAdmin [email protected]
     DocumentRoot /var/www/html/

     <Directory /var/www/html/>
         Options -Indexes +FollowSymLinks
         AllowOverride All

     ErrorLog ${APACHE_LOG_DIR}/
     CustomLog ${APACHE_LOG_DIR}/ combined
  • ServerName: This is your domain name.
  • ServerAlias: All other domains that should match for this virtual host as well, such as the www subdomain.
  • DocumentRoot: The directory from which Apache will serve the domain files.
  • Options: This directive controls which server features are available on the specific directory.
    • -Indexes: Prevents directory listings.
    • FollowSymLinks: This option tells your web server to follow the symbolic links.
  • AllowOverride: Allows to configure and override the settings using .htaccess file
  • ErrorLogCustomLog: Specifies the location for log files.

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.

The easiest way to enable the virtual host is by using the a2ensite helper tool.

sudo a2ensite example.conf

Once the configuration is enabled, test the configuration for any syntax errors with the following command.

sudo apachectl configtest

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

Syntax OK

Restart the Apache for the changes to take effect.

sudo service apache2 restart

To verify that everything is working as expected, open in your browser to view the output of the index.html file created above.

Prepare yourself for a role working as an Information Technology Professional with Linux operating system


Now you have learned how to setup virtual hosts for Apache on Ubuntu 18.04 on Google Cloud Platform

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.