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.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Apache Virtual Hosts queries.
In this context, we shall look into how to configure Apache Virtual Hosts on a Ubuntu 18.04 server.
With Virtual Hosts, we can specify separate website document root, security policy, use different SSL certificates and much more.
Before proceeding with this procedure, we must ensure that the following requirements are met:
1. Create 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.
i. 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
├── linuxapt.com
│ └── public_html
Basically, we will create separate directory inside /var/www directory for each domain which we want to host on our server.
ii. 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.
iii. To create a new index.html file using your favorite text editor type:
$ sudo nano /var/www/example.com/public_html/index.html
iv. 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
2. Create 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>
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 :
Great! example.com set up completed!
This article covers how to create apache virtual host files to host multiple domains on a single Ubuntu server. Each website published on the Internet is hosted on a web server (host), connected to the network with a public IP address and able to manage requests for web pages on a browser (client) such as Chrome, Firefox or Internet Explorer.
If you need to host a website on your machine, first, install a web server. Among those available, one of the best known is Apache, an open source software compatible with both Unix and Windows systems.
How to Install Apache on Ubuntu ?
1. To install Apache 2 through the official Ubuntu repositories, proceed by typing:
$ sudo apt-get update
$ sudo apt install apache2
Since Apache communicates externally, it should be inserted among the exceptions of your Firewall.
2. In case of using UFW, allow bidirectional communications between Apache and a Client by typing:
$ sudo ufw allow 'Apache Full'
3. Apache should already be running. To check its stateuse the command:
$ sudo systemctl status apache2