
Apache Virtual Hosts allows you to host multiple websites on a single server. Apache will divide its functionality and components into individual units so we can customize independently. In this tutorial, you will learn steps to set up Apache Virtual Hosts on a Ubuntu 18.04 server.
Create Virtual Hosts On Ubuntu 18.04
With Virtual Hosts, we can specify separate website document root, security policy, use different SSL certificates and much more.
Prerequisites
- A Ubuntu 18.04 server with a non-root user with sudo privileges.
- Apache should installed and configured, as shown in How to Install Apache on Ubuntu 18.04 Server.
- A domain name should pointing to your server IP address.
Creating the Directory Structure
At first, we need to create a directory where we will store website files of a domain and serve response to website visitors. Generally, it called DocumentRoot. You can set the document root to any location that you want but it’s best practice to set in directory structure. So we will store all at /var/www
.
/var/www/
├── example1.com
│ └── public_html
├── example2.com
│ └── public_html
Basically, we will create separate directory inside /var/www directory for each domain which we want to host on our server. Inside of these directories, we will create a public_html directory that will store the domain website files.
sudo mkdir -p /var/www/example.com/public_html
Create a index.html
file inside the domain document root directory for testing purpose. By default, This page will display when visitors visit your website.
To create a new index.html
file using your favorite text editor type:
sudo nano /var/www/example.com/public_html/index.html
After that, add the below lines into it:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Welcome!!</title>
</head>
<body>
<h1>Great! example.com set up completed!</h1>
</body>
</html>
All commands are executed as sudo user and newly created files and directories are owned by the root user. So we need to change ownership of document root directories to avoid permission issue for our regular user. Thus, regular user can modify files in our web directories without any issues.
sudo chown -R www-data: /var/www/example.com
Creating Virtual Host Files
Apache Virtual Hosts configuration files will be store in /etc/apache2/sites-available
directory and we can enable it by creating symbolic links to the /etc/apache2/sites-enabled
directory.
Create a new file using your choice text editor by typing :
sudo nano /etc/apache2/sites-available/example.com.conf
<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
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
- ServerName: This should be your domain name and match with virtual host configuration.
- ServerAlias: All other domains or subdomains that should match for this virtual host as well, usually the www subdomain.
- DocumentRoot: Path of virtual host directory that from which Apache will serve the domain files.
- Options: This directive controls which server features are available in a specific directory.
- -Indexes: It will prevent directory listings.
- FollowSymLinks: Apache will follow the symbolic links if this option is enabled.
- AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.
- ErrorLog, CustomLog: Specifies the location for log files.
You can give any names to your configuration file but it’s recommended to give file name same as domain name.
Now, we will create symbolic link at /etc/apache2/sites-enabled directory to enable newly created website.
By default in Ubuntu systems have a helper script to create symbolic links. To create using a2ensite helper script execute following command :
sudo a2ensite example.com
You also can create symbolic link manually by type:
sudo ln -s /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-enabled/
Once its created, check the syntax errors using:
sudo apachectl configtest
It will show below output if there are no errors:
Output
Syntax OK
You should restart apache2 service to get changes effect by below command :
sudo systemctl restart apache2
Finally, you can verify by opening your http://example.com to your web browser and it should show you as following :

Conclusion
In this guide, You learned how to create apache virtual host files to host multiple domains on a single Ubuntu server. If you have any question you can ask to below comment box.
Leave a Reply