Skip to main content

Creating Virtual Host In Apache

Virtual hosts on Apache


Definition

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

Requirements 

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 
mchampaneri.in 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
          /var/www/site2/public

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 
/var/www/site1/public/index.*


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
</VirtualHost>


Change the things in upper code

First change the ServerAdmin
ServerAdmin admin@site1.in

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 
ServerName site1.in

Finally you site1.conf should looklike this

<VirtualHost *:80>
    ServerAdmin admin@site1.in
    ServerName site1.com

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

    ServerAlias www.test.com
    DocumentRoot /var/www/test1.com/public
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

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

127.0.0.1 localhost 149.142.xxx.xxx  site1.in

Replace 149.142.xxx.xxx with your vps ip address


The last step is check you site in browser










Comments

Popular posts from this blog

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]

Sciter : GUI Application with Golang using HTML/CSS

GUI library for golang sciter This is the words from Sciter's Web site, Sciter brings a stack of web technologies to desktop UI development. Web designers and developers can reuse their experience and expertise in creating modern looking desktop applications. Various GUI frameworks offer different UI declaration and styling languages, such as QML and  XAML (Microsoft WPF) . On the contrary, Sciter allows using time proven, robust, and flexible HTML and CSS for GUI definition and GPU accelerated rendering.   Before using sciter I already tried other alternatives but none of them was satisfactory as an example first i tried andlabs / ui  library   i already have written a post on it. You can read it on post gui programming with golang .  But this library is still under construction and has no support for production apps. Secondly, I go for electron but the problem was my simple calc like the app was of size 150mb. Which is 15mb of go and other was the e