How To Configure Apache Virtual Hosts on Ubuntu 16

Try it in our public cloud & Get $5 Credit
CLAIM NOW

How To Configure Apache Virtual Hosts on Ubuntu 16

Virtual hosting is the technique of hosting multiple websites on one server. Virtual hosting enables efficient utilization of computing resources because the multiple websites share the computing resources of the host server. Apache is one of the most web servers in the world, and it provides a mechanism for enabling virtual hosting on a web server. Virtual hosting works by creating a directory for each domain/website and then redirecting visitors of a domain to that site’s specific directory. The whole process is handled transparently by Apache, and the user is not aware that the server is hosting other websites. Apache virtual hosting is very scalable and can be expanded to handle a large number of sites.

Getting started
To complete this walkthrough successfully the following are required:
• A node (Dedicated or Cloud Server) running Ubuntu 16
• All commands must be entered as root
Apache web server installed

Tutorial
This guide demonstrates how to implement virtual hosting for two domains, www.globo.tech and support.globo.tech, on a single server. The two domains can be substituted for any domain that you may use.

First, create the directories for each website in the /var/www directory using the following commands:
sudo mkdir -p /var/www/www.globo.tech
sudo mkdir -p /var/www/support.globo.tech
Next change the ownership of the directories so that the regular user can modify them:
sudo chown -R $USER:$USER /var/www/www.globo.tech
sudo chown -R $USER:$USER /var/www/support.globo.tech

Next change the permissions for the directories so that visitors to the site can have read access to the web pages:
sudo chmod -R 777 /var/www/www.globo.tech
sudo chmod -R 777 /var/www/support.globo.tech

The next step is creating a virtual host file for each website; this is accomplished by copying the default virtual host file and then editing it appropriately. Use the following commands to copy the default host file:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/www.globo.tech.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/support.globo.tech.conf

The virtual host files have to be modified so that they can direct requests appropriately. Open the virtual host file for the www.globo.tech site using an editor of your choice. In this example nano has been used:

sudo nano /etc/apache2/sites-available/www.globo.tech.conf

Subsequently, add a directive for the ServerName, this is the domain for the website:

ServerName www.globo.tech

Next, modify the DocumentRoot directive so that it matches the directory that you have created for the website

DocumentRoot /var/www/www.global.tech

If the changes have been implemented correctly the virtual host file will look this:
<Virtualhost>
ServerAdmin webmaster@localhost
ServerName www.global.tech
DocumentRoot /var/www/www.global.tech
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</Virtualhost>

The same changes should be made for the support.global.tech website. Open the virtual host file using the following command:
sudo nano /etc/apache2/sites-available/support.globo.tech.conf

Alter the file so that it matches the following:
<Virtualhost>
ServerAdmin webmaster@localhost
ServerName support.global.tech
DocumentRoot /var/www/support.global.tech
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</Virtualhost>

The final step is for enabling the new virtual hosts and reloading the web server. Apache provides the a2ensite tool for enabling sites. Use the following commands to enable the two sites:
sudo a2ensite www.global.tech.conf
sudo a2ensite support.global.tech.conf

Finally, reload apache using the following command:
sudo systemctl restart apache2

Conclusion
You have now setup virtual hosting, and you can use your single server to serve two websites. You can experiment further by adding more sites to your server. If this walkthrough has assisted you, do not hesitate to share with other people.