Hosting Virtual Hosts with Nginx Server on Ubuntu

Hello, here I’m, going to show you how to install Nginx server and configure it for hosting web sites. First of all let’s see what is Nginx server. Actually it is an open-source web server with high performance, stability, low resource utilized with simple configurations.

Following steps show you how to install and configure Nginx server to hosting websites.

Installing Nginx on Ubuntu server

$ sudo apt-get update
$ sudo apt-get install nginx

To confirm that the Nginx server has been installed, run the following command to get the version of the Nginx server.

$ sudo nginx -v

If the version would not be shown try to update “apt-get” or try to change the resource list which is located at /etc/apt/sources.list

Now you can see whether the Nginx server is running via using the following command. Then you can see that Nginx server running.

$ sudo service nginx status

After installing the Nginx server, you need to make a copy of default which is located at site-available in Nginx server because when there is new site to publish we need to change the content of this default configuration file.

$ sudo cp /etc/nginx/sites-available/default sudo vi /etc/nginx/sites-available/fileNameNow

the time to configure the website, please make sure to create a root directory to your site. In here I’m going to make a directory on “/var” and change the permission to access over the network.

$ sudo mkdir -p /var/myApp/public 
$ sudo chmod 755 /var/myApp/

In another step, the directory owner should be changed to the correct user which is defined on default configuration file as shown in the figure-01. .


The following command is used to set the particular user to the previously created directory.

$ sudo chown www-data:www-data /var/myApp/public/

After that, the index.html and other files should be added to the directory which was created previously. And then open the configuration file and change the configurations. The following commands are used to change the configuration.

$ sudo vi /etc/nginx/sites-available/myApp

According to the figure-02 , there are four configurations to be changed. First one is to set the directory which contains the web application. After that need to defined the indexes as shown in number 1.

root /var/myApp/public;
 index index.html index.htm;

In number two, user can set a specific web URL to access the website over the network.


In number three, it is used for automatically detect the index type.

autoindex on;

In number four is used to configure the proxy server for the website.

/var/kohls-repo/public {               

As the another step you have to configure the port number as the figure-03 is displyed.


Another important thing is to make the symlink to the existing directory that previously created. In order to achieve that the following command is created the symlink to the desired directory.

$ sudo ln -s /etc/nginx/sites-available/myApp /etc/nginx/sites-enabled/myApp

Now restart the Nginx server.

$ sudo service nginx restart

Finally you can load the website using http://ipaddress:81
Thank You.



One thought on “Hosting Virtual Hosts with Nginx Server on Ubuntu

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s