Step by Step Create Virtual Host in Ubuntu Vultr Server

Nutan
5 min readApr 20, 2021

--

Photo by Christian Wiediger on Unsplash

Prerequisite

Apache2 Web Server

if you have not installed Apache2 web server in Ubuntu server, you can follow link below:

Install Apache2 Web Server

Table of Contents

  1. Create/deploy a new server in vultr.com
  2. Login to Ubuntu Server using SSH
  3. Create a folder called www.test.loc inside /var/www directory
  4. Create index.html file inside www.test.loc folder
  5. Create virtual host
  6. Enable the Apache configuration(test.conf) for new site
  7. Reload or restart the apache2 service
  8. Open hosts file in your local system and make entry of IP address and domain name/server name
  9. Check http://www.test.loc in browser

1. Deploy a new Ubuntu server in vultr.com

When you deploy new server in vultr.com, you will get IP address, username and password. You have to make a note of that details. We need that details when we have to connect to newly deployed server.

2. Login to Ubuntu Server through SSH

Open command prompt and type following command to connect the server

ssh YOUR-SERVER-USERNAME@YOUR-SERVER-IPssh root@140.82.4.175

It will ask the password, type password and press enter.

3. Create folder www.test.loc folder in /var/www directory

Go to /var/www folder in terminal

cd /var/www

Check file inside /var/www

Type ll or ls

ll

Create project folder inside /var/www folder

mkdir www.test.loc

Give permission to /var/www/www.test.loc folder

chmod -R 775 /var/www/www.test.loc

4. Create index.html file inside www.test.loc folder

Go to project folder and create index.html file with vi command.

cd ~
cd /var/www/www.test.loc
vi index.html

vi index.html command will open vi editor, you have to write simple html code. You have to press “i” for insert code in vi editor. Then write following code in index.html.

<html>
<head>
<title></title>
</head>
<body>
<h1>This is welcome page. Hurray it is working.</h1>
</body>
</html>

Save and exit index.html file. For that you have to press Esc key then :wq after that hit enter key.

5. Create virtual host

Virtual hosting is a method for hosting multiple domain names (with separate handling of each name) on a single server. Read More

Create apache configuration file for new site.

We can make a copy default 000-default.conf or create a new .conf file. In this example i am creating a copy of 000-default.conf and it’s name is test.conf.

cd ~
cd /etc/apache2/sites-available/
cp 000-default.conf test.conf
vi test.conf

Write following code in test.conf file. Press “i” for insert code in vi editor.

<VirtualHost> and </VirtualHost> are used to enclose a group of directives that will apply only to a particular virtual host. Any directive that is allowed in a virtual host context may be used. When the server receives a request for a document on a particular virtual host, it uses the configuration directives enclosed in the <VirtualHost> section. Addr can be any of the following, optionally followed by a colon and a port number (or *):

A ServerName should be specified inside each <VirtualHost> block. If it is absent, the ServerName from the "main" server configuration will be inherited.

<VirtualHost *:80>    ServerAdmin webmaster@www.test.loc
DocumentRoot /var/www/www.test.loc
ServerName www.test.loc

<Directory /var/www/www.test.loc>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Press Esc, then :wq and hit enter. It will save and close the file.

6. Enable the Apache configuration(test.conf) for new site

Go back to one folder back, then type below command.

cd ..
sudo a2ensite test.conf

Check the enabled site

cd sites-enabled
ll

7. Reload or restart the apache2 service

We have added a new virtual hosts, we must restart or reload the apache server.

sudo systemctl restart apache2.service

8. Open hosts file in your local system and make entry of IP address and domain name/server name

What is hosts file?

A hosts file which is used by operating systems to map a connection between an IP address and domain names. This file is a simple text file with the mapping of IPs and domain names.

Open command prompt as administration in your local system and type following command

notepad c:\windows\system32\drivers\etc\hosts

Add www.test.loc in hosts file

140.82.4.175       www.test.loc

Note: 140.82.4.175 is my ubuntu server IP address and www.test.loc is my domain name.

Save and close the file.

9. Check http://www.test.loc in browser

http://www.test.loc/

--

--

Nutan

knowledge of Machine Learning, React Native, React, Python, Java, SpringBoot, Django, Flask, Wordpress. Never stop learning because life never stops teaching.