How to Setup Virtual Hosts on Ubuntu

how to create virtual host in VPS linux

What is Virtual Host and why its needed ? Answer : The term Virtual Host is practic to run multiple website on single machine. or on single IP address.

Example : Website 1 like www.example1.com is running on 192.168.10.108 again another website like www.example2.com is also runnging on 192.168.10.108. So using Virtual host we can manage in Apache2.

Let’s start the Setup Virtual hosts on Ubuntu.
Prerequisite :

  1. UBUNTU 14.08 on wards VPS with root access.
  2. An Apache2 installed server.
  3. domain name A record must be point to the server public IP address. ( here we use 192.168.1.108 )

Here, Start with two example websites. which we points to the 192.168.10.108 IP server.

step 1: Update your system.

sudo apt-get update

step 2: Connect your VPS using SSH. and create directory name with example1.com and example2.com

sudo mkdir /var/www/html/example1.com
sudo mkdir /var/www/html/example2.com

step 3: Now we can create index.html in example1.com folder to identidfy virtual host working correct or not. Write somthing like Hello World.

 sudo vim /var/www/html/example1.com/index.html 

step 4 : Create virtual host file.

Apache comes with default config file. 000-default.conf for example1.com we need to create a example.conf file. we copy default 000-default.conf file and modify it using below command.

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

After copy example1.com.conf edit it.

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

Need to replace with below code.

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

step 5: now enable this virtual host file to apache2 using below code.


cd /etc/apache2/sites-available/

sudo a2ensite example1.com.conf

step  6: now enable this changes to server. restart apache2 services.

 sudo service apache2 restart 

step 7:  just go to browser and check domain is points to your server.

Note : If you want to create a local virtual host then you have to edit hosts file. Which is located to the

 /etc/hosts 

and add one line

 127.0.0.1 example1.com 

You can repeat this process to host as many sites as the server can handle.
I hope you like this article. Keep visiting my website for more upcoming articles. If you need any help with How to stepup virtual hosts on Ubuntu or any other OS. you can contact me. You can ask me questions in comments also. You can connect me on social media as well links are below in the footer section. Keep connected. Happy Coding.

Prashant Sutharhttps://prashantsuthar.com
My self Prashant Suthar, I had experience worked with NodeJS, Core PHP, WordPress, CodeIgniter, Shopify, Prestashop, Opencart and many frameworks. I had some basic knowledge about server setup and maintenance. I also worked with third parties APIs like Twillio audio,video, SMS API, FB messenger API and many more. I am working as team lead & sr. developer in Ahmedabad, GJ, IN.

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Similar Articles