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 hereWikipedia - 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 ossudo 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 folderYou 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 sitesrun 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
Comments
Post a Comment