What is Virtual Host:
Virtual host is a mechanism to set up multiple hosts in a single system the user feels that they are getting redirected to different locations when in reality their requests are sent to the same IP, just on different directories. Every virtual host is identified by virtualname. Virtual are useful because you can utilize a single IP location and single machine to host multiple websites. Let’s see how we can setup virtual host on our own machine in few simple steps
Pre-requisites:
In order to setup vhost you need to install and configureapache 2 on your ubuntu system. You can refer tothis link to setup apache.
Step 1: Intitalizing and configuring directory
The first step is to create directory which contains the website i.e the document root of your site.
create the folder for your website invar/www/html
:
sudo mkdir /var/www/mysite
You need to have root priviliges to create a folder invar/www/html
Now we need to change the permissions formysite
so that it can be accessed without root privilages. We will change the owner to current logged-in user to gain full access ofmysite
folder:
sudo chown$USER:$USER /var/www/mysite/
Where$USER
contains name of current logged-in user.
Also we can ensure thatmysite
is readable by all the users:
sudo chmod755 /var/www/mysite/
Step 2: Creating demo page inmysite
Now that we are all done with setting up directory let's add some content to it.
Note: You can skip this step if you have website content ready with you.
Create a file inmysite
usingnano
or you can use any editor.
nano /var/www/mysite/index.html
Add some content like this:
<html><title>My Site</title><body><h1>Hello and Welcome to My Site<h2>This is where it all started......</body></html>
PressCtrl+X
and save the file.
Step 3: Creating configuration files for virtual host
We are all done with setting up content for our site. Let's move on to setup our virtual host file.
Apache provides a default file for virtual host called000-default.conf
present in/etc/apache2/sites-available/
which we can copy and modify a bit for our virtual host.
Let's copy the default file and open it in nano:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysite.local.confsudonano /etc/apache2/sites-available/mysite.local.conf
You will see a bunch of parameters in this file. We will modify four of them for basic setup.SeverAdmin
-> the email of your website's adminSever Name
-> the address of your websiteDocument Root
-> the directory which contains the website.Server Alias
-> a sudo or virtual name that should be displayed on search bar while accessing the site.
<VirtualHost*:80> ServerName 127.0.0.2 ServerAlias mysite.local ServerAdmin admin@mysite.com DocumentRoot /var/www/mysite ErrorLog${APACHE_LOG_DIR}/error.log CustomLog${APACHE_LOG_DIR}/access.log combined</VirtualHost>
Save the file and exit.
Step 4: Enabling newly added virtual host
After configuring the virtual host file we have to enable it usinga2ensite
command (which rougly translates to apache2 enable site)
Change the directory and enable the site:
cd /etc/apache2/sites-available/sudoa2ensite mysite.com.conf
To make the changes restart the apache server
sudosystemctl restart apache2
Step 5: Configuring hosts file
The last step is to configure/etc/hosts
file which contains list of IP addresses and names. It acts like a local DNS file.
Let's open the file usingnano
:
sudonano /etc/hosts
Now add the IP address and name of the site.
Note: the name should be same as the name of file for virtual host i.emysite.local.conf
127.0.0.1 localhost127.0.0.2 mysite.local
And that's it!!
Open browser and typehttp://mysite.local
and you can access your site.
Congratulations! you just setup a virtual host.
References:
https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-18-04-quickstart
https://linuxhint.com/install_apache_web_server_ubuntu/
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse