Steps to Setup Apache Virtual Hosts on Ubuntu 18

Virtual hosting is a method for hosting multiple domain names (with separate handling of each name) on a single server (or pool of servers). This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same host name.

Step 1 – Installing Apache:

Apache HTTP Server is a free and open-source web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web.

sudo apt update

sudo apt install apache2

After install Start and Enable Service

Step 2 – Enable Service:

#systemctl enable httpd

Step 3 – Start Service:

#systemctl start httpd

Step 4 – Allow Firewall:

sudo firewall-cmd --permanent --zone=public --add-service=http 

sudo firewall-cmd --permanent --zone=public --add-service=https

sudo firewall-cmd --reload

Step 5 – Create Path Web:

mkdir -p /var/www/test1.com
mkdir -p /var/www/test2.com

Step 6 – Create HTML File:

This is for Test1 web page

vi /var/www/test1.com/index.html


            <!doctype html >
            <html lang="en">
            <head> 
               <title> to test1.com</title>
            </head>
            <body> 
                <h1>! test1.com home page!</h1>
            </body>
            </html>

This is for Test2 web page

vi /var/www/test2.com/index.html


            <!doctype html >
            <html lang="en">
            <head> 
               <title> to test2.com</title>
            </head>
            <body> 
                <h1>! test2.com home page!</h1>
            </body>
            </html>

Step 7 – Make permision:

chown -R apache: /var/www/test1.com

chown -R apache: /var/www/test2.com

Step 8 – Create Virsual Host:

This is for test1.com conf

vi /etc/httpd/conf.d/test1.com.conf


<VirtualHost *:80>
ServerName test1.com
ServerAlias www.test1.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www/test1.com 

<Directory /var/www/test1.com>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>

ErrorLog /var/log/httpd/test1.com-error.log
CustomLog /var/log/httpd/test1.com-access.log combined
</VirtualHost>

This is for test2.com conf

vi /etc/httpd/conf.d/test2.com.conf


<VirtualHost *:80>
ServerName test2.com
ServerAlias www.test2.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www/test2.com 

<Directory /var/www/test2.com>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>

ErrorLog /var/log/httpd/test2.com-error.log
CustomLog /var/log/httpd/test2.com-access.log combined
</VirtualHost>

Step 9 – Restart

After Setup Restart Service Hosting

systemctl restart httpd