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.
Prerequisites
- A running Compute Engine, see the Setting up Compute Engine Instance with Ubuntu 18.04
- Initial Ubuntu Server Set up.
- Setup Google Cloud DNS for your Domain name.
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.
/var/www/html
├── example.com
│ └── public
├── domain2.com
│ └── public
├── domain3.com
│ └── 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 example.com
domain name as a demo for this setup.
sudo mkdir -p /var/www/example.com/public
Now create a new index.html file inside the document root, that is /var/www/html/example.com/public
Create a demo file using nano editor.
sudo nano /var/www/example.com/public/index.html
<!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>
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/example.com
sudo chown -R www-data:www-data /var/www/html/example.com
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>
ServerName example.com
ServerAlias www.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/html/example.com/public
<Directory /var/www/html/example.com/public>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
ServerName
: This is your domain name.ServerAlias
: All other domains that should match for this virtual host as well, such as thewww
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
fileErrorLog
,CustomLog
: 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 http://example.com
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
Conclusion
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.