How to Setup Apache Virtual Hosts on Ubuntu 18.04

r00t July 30, 2018

Setup Apache Virtual Hosts on Ubuntu 18.04

In this tutorial we are going to learn how to setup Apache Virtual Hosts. We will also install and configure its prerequisites. Apache web server is among the most popular and powerful web servers in the world, as a result of its ease of administration and flexibility. In this tutorial we will install Apache on a host which doesn’t have a web server or database server installed.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Ubuntu 18.04 Bionic Beaver minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Setup Apache Virtual Hosts on Ubuntu 18.04

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

apt-get update -y
apt-get upgrade -y

Step 2. Installing Apache web server on Ubuntu Bionic Beaver.

Installing Apache is as simple as running just one command:

sudo apt install apache2

Start the Apache service and make it start on system boot:

sudo systemctl start apache2.service
sudo systemctl enable apache2.service

Step 3. Set Up Virtual Hosts on Ubuntu 18.04.

For the purposes of this guide, we will make a virtual host for ramona1.com and another for ramona2.com. You can substitute these with your own registered domains, or if you don’t have any domains yet, you can still follow this guide and add ramona1.com and ramona2.com to your hosts file to trick your OS into resolving these domains in the browser:

sudo mkdir -p /var/www/ramona1.com/public_html
sudo mkdir -p /var/www/ramona2.com/public

We must also change the permissions for the general web directory:

sudo chmod -R 755 /var/www

Create Test Web Pages:

We’ll now create a simple index.html web page for each domain using the echo command:

sudo echo "Welcome to ramona1.com!" > /var/www/mytest1.com/public_html/index.html
sudo echo "Welcome to ramona2.com!" > /var/www/mytest2.com/public_html/index.html

Create New Virtual Host Files:

nano /etc/apache2/sites-available/ramona1.com.conf

Add following content:

<VirtualHost *:80>
ServerAdmin webmaster@ramona1.com
ServerName ramona1.com
ServerAlias www.ramona1.com
DocumentRoot /var/www/ramona1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

We can now repeat the above process for ramona2.com:

nano /etc/apache2/sites-available/ramona2.com.conf

Add following content:

<VirtualHost *:80>
ServerAdmin webmaster@ramona2.com
ServerName ramona2.com
ServerAlias www.ramona2.com
DocumentRoot /var/www/ramona2.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Then, enable the new Virtual Host Files:

sudo a2enmod rewrite
sudo a2ensite ramona1.com.conf
sudo a2ensite ramona2.com.conf
sudo systemctl reload apache2

Finally steps, Assuming you have already configured DNS on your domain registrar to point your domains to the IP of your Apache server, you should now be able to view these test webpages in the web browser.

Congratulation’s! You have successfully install and configured Apache on your Ubuntu 18.04 server. Thanks for using this tutorial installing Apache web server on Ubuntu 18.04 LTS (Bionic Beaver) system.

The Tags:

Leave a Comment

Comments are closed.