How to Configure Apache Virtual Host on Ubuntu 16.04 LTS

The Apache is the world most popular Http server which serves web content to the Internet since 1995. And become leading web server in the world. An open developer community developed Apache and maintain under apache software foundation. The secret of the popularity of Apache, it is customizable and after all, it’s an open source application. You can configure Apache’s individual function independently. A virtual host is one of the basic units which indicates the individual domain. Within a single machine or VPS, you can configure multiple websites using a single IP and interface. Each domain arranged in such a way that direct visitor to a specific directory holding the web content.

In this article, we will describe you how to configure Apache Virtual hosts on Ubuntu 16.04 LTS.

Configure Apache Virtual Host:

Here we configure each domain to an individual user directory. The main reason behind this that users are not able to modify or interferes others. you can jail the user to his directory for security reason.

Prerequisites:

First of all, you need Linux box or VPS ubuntu 16.04. After that install, LAMP Stack click here for LAMP installation tutorial. If want to set up only Apache then follow the command.

$ sudo apt-get update
$ sudo apt-get install apache2

After completing above you can proceed for Vhost configuration.

Step 1: Add user and Web content directory:

Creating a user and directory for the domain

$ sudo useradd -m abc
$ sudo mkdir /home/abc/public_html

Create another user and its directory.

$ sudo useradd -m xyz
$ sudo mkdir /home/xyz/public_html

Step 2: Adding a demo page for each domain:

Now create an index file for testing purpose.

$ sudo nano /home/abc/public_html/index.html

#insert following line to index file

<html>
  <head>
    <title>Welcome to abc.com</title>
  </head>
  <body>
    <h1>Hi!  The abc.com virtual host is working!</h1>
  </body>
</html>

Similarly, create XYZ demo page.

$ sudo nano /home/xyz/public_html/index.html

#insert following line to index file

<html>
  <head>
    <title>Welcome to xyz.com</title>
  </head>
  <body>
    <h1>Hi!  The xyz.com virtual host is working!</h1>
  </body>
</html>

Step 3: Create and Configure Vhost file for each domain:

  $ sudo nano /etc/apache2/sites-available/abc.com.conf

Insert the following line to abc vhost file.

<VirtualHost *:80> 
    ServerAdmin abc@abc.com
    ServerName abc.com
    ServerAlias www.abc.com
    DocumentRoot /home/abc/public_html/

    <Directory /home/abc/public_html/>
       Options Indexes FollowSymLinks
       AllowOverride None
       Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Similarly, create XYZ vhost file.

 $ sudo nano /etc/apache2/sites-available/xyz.com.conf

Now copy and past the following:

<VirtualHost *:80> 
    ServerAdmin xyz@xyz.com
    ServerName xyz.com
    ServerAlias www.xyz.com
    DocumentRoot /home/xyz/public_html/

    <Directory /home/xyz/public_html/>
       Options Indexes FollowSymLinks
       AllowOverride None
       Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 4: Enable the New Virtual Host Files

$ sudo a2ensite abc.com.conf
$ sudo a2ensite xyz.com.conf

When you are finished, you need to restart Apache to make these changes take effect:

$ sudo service apache2 restart

Step 5: Adding Domain information to local Host file:

$ sudo nano /etc/hosts

Add following line to the end of hosts file

127.0.0.1   abc.com
127.0.0.1   xyz.com

Step 6: Test your Virtual Hosts:

Now if you are in a test environment on your local LAN. And you are using a windows machine for test your sites then you have to set a DNS record to your windows host file. That is  located at c:/windows/system32/Driver/etc/hosts. Open this file using notepad and add FQDN with your server IP. In my case server, IP is 192.168.182.50

Open Command prompt and type notepad C:/windows/system32/Driver/etc/hosts and add the following line at the end of the file.

192.168.182.50  abc.com
192.168.182.50  xyz.com

Eventually, open a browser then type your domain name to test whether its working or not.

http://abc.com

Virtual Host

http:// xyz.com

xyzhost

Conclusion:

So finally we are able to run multiple websites from a single Linux box or VPS. You can expand your virtual hosts using above method. There are not limits to add the virtual host on Apache so feel free to add Vhosts as much as you wish. But remember the there is a limitation on your physical server processing power and memory consumption to handle this. Here we made a shell script to easy installation for you. Thank you for reading our blog. Hope it helpful for you. If you have any query please comment us.

 #!/bin/sh
echo ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
echo "; Welcom to TechNHIT LAMP Stack with VHost installation ;"
echo "; Thank You for installanton Virtual Host ;"
echo ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"

echo -n "Enter Your Domin (FQDN) name :"
read vuser
echo -n "Enter Admin Email Address :"
read eml
echo "You have enter Domin Name : $vuser"
len=`echo $vuser|awk '{print length}'`
echo "lenth is $len"
dn=""
p=$(echo ${vuser%.*} | wc -c) #Finding position of . in domen name

if [ $p -eq 8 ]
then
echo "your virtual username will be `expr substr $vuser 1 $((p-1))` "
dn=`expr substr $vuser 1 $((p-1))`
elif [ $p -lt 8 ]
then
echo "your virtual username will be `expr substr $vuser 1 $((p-1))` "
dn=`expr substr $vuser 1 $((p-1))`
else
echo "your virtual username will be `expr substr $vuser 1 8`"
dn=`expr substr $vuser 1 8`
fi

if grep -c "^$dn:" /etc/passwd ; then

echo -n "User already exist,Please Enter Another User Name:"
read dn
fi

echo "Do you want to proceed (Yes/No)?"
read ch
if [ "$ch" = "y" ] || [ "$ch" = "Y" ] || [ "$ch" = "yes" ] || [ "$ch" = "YES" ]
then
sudo apt-get update
sudo apt-get install apache2 mysql-server php libapache2-mod-php php-mysql php-gd php-mcrypt

sudo useradd -m $dn

#sudo chown -R $dn:$dn /home/$dn/

sudo mkdir /home/$dn/public_html
sudo touch /home/$dn/public_html/index.php

f="/home/$dn/public_html/index.php"
sudo sh -c "echo '<?php phpinfo(); ?>' >> $f"


sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/$vuser.conf

f="/etc/apache2/sites-available/$vuser.conf"

sudo sed -i -e '11c\'"ServerAdmin $eml" $f
sudo sed -i -e '12c\'"ServerName $vuser" $f
sudo sed -i -e '13i\'"ServerAlias www.$vuser" $f
sudo sed -i -e '14i\'"DocumentRoot /home/$dn/public_html/" $f
sudo sed -i -e '15i\'"<Directory /home/$dn/public_html/>" $f
sudo sed -i -e '16i\'"Options Indexes FollowSymLinks" $f
sudo sed -i -e '17i\'"AllowOverride None" $f
sudo sed -i -e '18i\'"Require all granted" $f
sudo sed -i -e '19i\'"</Directory>" $f


sudo a2dissite 000-default.conf #Desible the default sites
sudo a2ensite $vuser.conf #Enable the sites
sudo service apache2 restart #Apache configration reload and restart

#Add record to hosts file
f2="/etc/hosts"
sudo sed -i -e '1i\'"127.0.0.1 $vuser" $f2

fi
#End of Script

 

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

2 thoughts on “How to Configure Apache Virtual Host on Ubuntu 16.04 LTS

  1. Pingback: Install LAMP Stack on Ubuntu 16.04 LTS - NHIT Tech Blog

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.