Every Web developer Must have knowledge about Virtual Host Configuration

Hello geeks, I am a php and angular professional using lamp stack and sharing you all about the virtual host configuration for hosting multiple domains or subdomain on a single ip/server.

First of all i wanted to tell you all the developers that don’t recommend your client to user shared hosting and don’t let you also use shared hosting and go for the cloud servers like Aws ,DigitalOcean,rackspace,Windows Azure,Google cloud,Linode etc.

I also started experiments a lot by using some of the servers, you can start with any of the cloud service provider discussed above. So basically i am providing you the complete tutorial for host multiple domain using virtual host configuration on a single server instance.

Once you are ready with installing linux and lamp on your server, There are multiple ways to add virtual hosts in linux servers , I Have Done experiment on recently on DigitalOcean so i am sharing that experience:

Note: I am assuming that you have created a droplet(instance) for which you want to host multiple site domains

Step 1: In digitalocean controlpanel there is a networking menu in top where you can add your domains one by one here are the steps to do it

1.1: Add you domain in

1.2: i am using test-domain-name.com , you can put domain same like this and click on add domain button

1.3: now when you will click you will find Ns records auto added like this

1.4:Now add A record ,you can see A is selected in tabs,in first input box put @ and in second choose your created droplet and then click on create record

1.5:Now you need to create Cname record so choose tab Cname and put the value visible in this image,in first box use www,in second use @,and then click on create record

1.6:now go to mx tab and click on add gmail mxrecords

1.7: and finally you will have a complete list of your added records like this

This is the complete process by which you can add records one by one for multiple domains,for another domain add repeat the process.


Step 2: Create your folders in /var/www/ directory for your muiple projects/sites

you can use following command(s) for creating directory/directories in linux/ubuntu one by one

sudo mkdir -p /var/www/public_html/my_vhosts/domain1.com
sudo mkdir -p /var/www/public_html/my_vhosts/domain2.com
sudo mkdir -p /var/www/public_html/my_vhosts/domain3.com

So now put the test pages or original project codes in the differ differ folders

Step 3: Now we need to add the virtual host using creating apache vhost file(s) for your domain(s)

there is always a default file for the ip/server and we just need to add the files having same type of content with different site paths and domain names and each we need to start that conf file.

Let me explain how to copy that default file ( /etc/apache2/sites-available/000-default.conf ) with our domain name and edit the content

3.1 Copy the default configuration and edit the vhost file for domain(s)

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain1.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain2.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain3.com.conf

Now all your multiple domain conf files are ready to edit, i am showing how to edit any one of them so you can edit all accordingly:

3.2 Edit the file as follow:

sudo vim /etc/apache2/sites-available/domain1.com.conf

you will see that your file is opened in terminal, now just press insert button on your keyboard and you will find the code in that file something like that:

<VirtualHost *:80>
 ServerAdmin webmaster@localhost
 DocumentRoot /var/www/html

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

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

<IfModule mod_dir.c>
 DirectoryIndex index.php index.pl index.cgi index.html index.xhtml i ndex.htm
 </IfModule>

</VirtualHost>

Now just Edit the file as following

<VirtualHost *:80>
 ServerAdmin your-email-id@mail-server.com
 ServerName domain1.com
 ServerAlias www.domain1.com
 DocumentRoot /var/www/public_html/my_vhosts/domain1.com
<Directory /var/www/public_html/my_vhosts/domain1.com>
 Options Indexes FollowSymLinks
 AllowOverride All
 Require all granted
 </Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
 DirectoryIndex index.php index.pl index.cgi index.html index.xhtml i ndex.htm
 </IfModule>
</VirtualHost>

after edting like this you need to save and edit from the editor

So Press escape button in your keyboard the immediately input >> :wq and just Enter by keyboard

Your changes will be saved in that file

As so on for all of your domain repeat the step 3 process and you are now done with creating configuration for multiple hosts

Step 4: Now just activate your multiple sites by following command on your terminal

sudo a2ensite domain1.com.conf
sudo a2ensite domain2.com.conf
sudo a2ensite domain3.com.conf

After activating one by one the terminal will show a message to reload/restart the apache services to reflect the changes .

So you can use any one of following command to reload apache configuration or restart the apache service

sudo service apache2 reload
sudo service apache2 restart

Hurray You are Done with configuration and bindings

Note: Please confirm that you also change the nameservers of your domains from your domain controlpanel either from godaddy/bigrock/namecheap/or any other domain provider/

your name server suppose to use with every domain are following:

ns1.digitalocean.com

ns2.digitalocean.com

ns3.digitalocean.com


Thanks for reading this if you have any question then please comment/ask me on jeetendra.apps@gmail.com . i will happy to resolve any of your issue related to this

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.