Apache2 Virtual host Configuration to domain and sub-domain in DigitalOcen


In this post, I would like to give you an example of how to set virtual host configuration to domain and subdomain. We can simply add two virtual hosts and it put into the .conf file.

So let’s see bellow simple example of configure domain and subdomain.

Step 1: Go to the path and create folder

Open the terminal and go to your live server folder.

C:\Users\Admin>ssh root@IP_Address
type password

after creating two folders in the directory

root@Hdgtyhjui-Amsertydam-Server:~# cd /var/www/
root@Hdgtyhjui-Amsertydam-Server:/var/www# sudo makdir folder1
root@Hdgtyhjui-Amsertydam-Server:/var/www# sudo makdir folder2

Step 2: Create virtual hosts

run this following command

root@Hdgtyhjui-Amsertydam-Server:cd /etc/apache2/sites-available
root@Hdgtyhjui-Amsertydam-Server:/etc/apache2/sites-available# sudo nano abc.conf

In the above step, go to the sites-available folder and create a new file whose name is abc.conf. when you run sudo nano abc.conf command open the file and paste the following code.

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName domain.com
        DocumentRoot /var/www/folder1

        <Directory /var/www/folder1>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Require all granted

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName subdomain.com
        DocumentRoot /var/www/folder2

        <Directory /var/www/folder2>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Require all granted

Here, We create two virtual hosts for one virtual host is folder1 and the second virtual host is folder2. In folder1, serverName is your base domain and in DocumentRoot given path is your base domain folder like folder1. The same thing in folder2, serverName is your subdomain and in DocuentRoot given path is your subdomain folder like folder2.

After that run the following command

sudo service apache2 restart

About Dhaval Shah

My name is Dhaval Shah. I'm a PHP developer, entrepreneur and CEO of DAG inventions PVT ltd. I live in India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Java-script, JQuery, Laravel, WordPress and Bootstrap.
Follow me on Github / Linkedin

Leave a Reply

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

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!