Skip to main content

Creating Virtual Host In Apache

Virtual hosts on Apache


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.You can read more here
Wikipedia - Virtual Host

If you have purchase vps than you can use virtual host to host multiple site from the single vps ( or form single droplet if you use digitalocean). Most of vps provider gives linux based os because they are free. I will explain how to create and setup virtual host on digitalocean lamp stack with ubuntu 16.04


Ubuntu 16.04 with Apache

Install Apache

If you don't have already installed then run this commands to install the apache on you os

sudo apt-get update
sudo apt-get install apache2

Setup folders for site to be hosted

Site that are hosted has to be inside the /var/www folder

You can give any name to site folder. But it is good to give name as the site like or just mchampaneri or mysite ( because it is my personal blogging web site )

Create folder for each site you want to host like 
site 1: /var/www/site1 ( site data ) 
          /var/www/site1/public ( site entry where index file has to be stored )
site 2: /var/www/site2

You folder tree should look like this
  • var 
    • www
      • site1
        • public
        • other folders
      • site2
        • public
        • other folders
      • other site folders

making public folder inside site folder is good thing so you can store private data of site ( like images uploaded by the user as private ).You have to do is only give read access to the public while other folder than public folder should not be accessed by the www-data.

Changing the folder permission

One thing the owner of the folder is by default who creates it. So if you check the owner of the folder you created inside the www folder will be the user in which you logged at time of creating folder.
If you want to make the folder modifiable by www-data you have to change owner www-data use -R with command to apply it to the every folder inside it. 

sudo chown -R www-data /var/www/site1/public
sudo chown -R www-data /var/www/site2/public

Entry point for the site will be 

Creating the Virtual Host .conf files

Next is creating the virtual host for both sites
run this command 

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

if will create a copy of default " 000-default.conf" file  named as "sit1.conf" . 000-default is default .conf file that ships with the os. Now open "site.conf" with vim. I assume that you have knowlade how to use vim in linux for file editing. So what you will get is like this

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Change the things in upper code

First change the ServerAdmin

DocumentRoot is the  folder of entry for you site.  ( In easy manner the folder that contains the index file of your site )
For us the DocumentRoot of our site1 is "/var/www/site1/public"
DocumentRoot /var/www/site1/public

If you have purchased custom domain for the site just add the ServerName 

Finally you site1.conf should looklike this

<VirtualHost *:80>

    // ServerAlias the alternate name for site like and points to same page
    // If you not add as alias you will not be able to access site with g
    // It will only displayed with url

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

Do the same thing for site2 as well just replacing information for site2.

You can host many site from single vps having different domain names with virtual hosting.

Enable the virtual hosts

After Creating the conf Virtual host file you have to enabled.
sudo a2ensite site1.conf

By running upper command you are enable the virtual host defined in file sit1.conf 
a2ensite = apache + enable  + site

Next step is edit your host file. Host file can be found at /etc/hosts localhost

Replace with your vps ip address

The last step is check you site in browser


Popular posts from this blog

Google blogger Ideas panel

Google blogger Ideas  I opened by blogger today, and..   I got this.  Google blogger Ideas  A panel suggesting a topic on which I can write my next blog. It's fetching unanswered question from web according to your previous post and topics. It was something, I was really looking for, after all it takes time to finding subject on which to write next and still being in the same niche.  Awesome feature Blogger! 

Apache : setup basic auth with apache in windows

Authentication is any process by which you verify that someone is who they claim they are. Authorization is any process by which someone is allowed to be where they want to go or to have information that they want to have. I will show here how to set up basic auth on the apache with windows. Pre-requests  Windows VPS Apache server ( That's it ) ( In windows it might be difficult to setup the Apache alone. So instead use something ling xampp , wamp or laragon .) RestClient (  I personally use the postman , but you can use your preferable client)  Windows VPS provider Steps  Enable the necessary modules in the Apache Create the password file Set the auth directives in the virtual host file. Verify basic auth. Enable the  necessary   modules  in the Apache Open the httpd.conf file in the apache's conf folder. httpd.conf file Enable the necessary modules to make the basic auth working. Necessary modules  mod_auth_basic

Firebase - update a spacific fields of single element of object of array in firestore

Firebase - update a spacific fields of single element of object of array in firestore  Its actully advisable to use map instead of array when ever it is possible. But, there are cetain cases where you don't have option to do so.  For example, you are directly saving the response from some outer source without any modification and they send you an array. In this case you will have array to work with. Firestore does not support array here is why  "bad things can happen if you have multiple clients all trying to update or delete array elements at specific indexes. In the past, Cloud Firestore addressed these issues by limiting what you can do with arrays " For more details information you can refer to Kato Richardson post Best Practices: Arrays in Firebase .  Firestore document having array [ used from stackoverflow question ] Suppose you have array of object something like shown in array. Now you want to update endTime field of the object on the index [1]