How to Set Up Nginx Server Blocks on Ubuntu 18.04 Server

Nginx is one of the best free web servers that offer accelerated content and application delivery. The open-source software was built for speed, improved security, and stability for the world’s busiest sites.

Ubuntu 18.04, on the other hand, is a strong and secure open-source operating system that is fully customizable.

With Nginx and Ubuntu 18.04 combined, one can run a very powerful web server. The beauty of Nginx is the ability to host multiple sites on a single Ubuntu 18.04 server (e.g. a VPS machine).

In this guide, we will show you how to set up multiple websites on Ubuntu 18.04 server with Nginx server blocks.

Prerequisites

In order to accomplish the setup, make sure you have the following:

  1. A VPS account. Signup with Digital Ocean and enjoy up to $100 worth of free trial credit.
  2. A VPS instance running Ubuntu 18.04 as the operating system.
  3. A non-root user that can perform sudo tasks on the VPS instance

Step 1: Installing Nginx Web Server on Ubuntu 18.04

The first step is installing the Nginx web server. Before you do this, SSH to your server and update the software information index to ensure we are pulling the latest version from Ubuntu’s software repository.

$ sudo apt-get update

Then, install Nginx.

$ sudo apt-get install nginx

Output:

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following additional packages will be installed:

  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0

  libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip

  libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter

  libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common

  nginx-core

Suggested packages:

  libgd-tools fcgiwrap nginx-doc ssl-cert

The following NEW packages will be installed:

  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0

  libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip

  libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter

  libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx

  nginx-common nginx-core

0 upgraded, 18 newly installed, 0 to remove and 103 not upgraded.

Need to get 2,423 kB of archives.

After this operation, 7,860 kB of additional disk space will be used.

Do you want to continue? [Y/n]

Press Y when prompted to confirm the installing and hit Enter to continue.

Once Nginx is installed, you can verify the installation by entering the public IP address associated with your VPS machine on a web browser like Google Chrome.

192.0.0.1

You should see the default Nginx web page on Ubuntu 18.04 server as shown below.

Step 2: Creating root Directories for the Websites

By default, Nginx creates one server block with /var/www/html as the root directory. Since we want to host multiple sites on the server, we will have to create a separate directory for each website.

We will be creating directories for example.com and example.net website inside the /var/www/ directory. It is a good practice to create a public_html sub-directory on each website.

To do this, run the commands below:

$ sudo mkdir -p /var/www/example.com/public_html
$ sudo mkdir -p /var/www/example.net/public_html

Remember to create all directories for your websites in this step, the number of websites does not matter provided your server resources can handle the traffic. 

Step 3: Setting the Appropriate File Permissions

If you create directories with the sudo command, they will belong to the root user. However, we want to make sure that the regular users on the Linux system can upload website files.

To do this, we will change the ownership of the website document root directories to the current user.

$ sudo chown -R $USER:$USER /var/www/example.com/public_html
$ sudo chown -R $USER:$USER /var/www/example.net/public_html

To ensure that Nginx will be able to read and execute the files placed on the public_html directories, we need to issue the right permissions.

$ sudo chmod -R 755 /var/www/example.com/public_html
$ sudo chmod -R 755 /var/www/example.net/public_html

Step 4: Creating Sample Nginx Index Files

With the directory setup and permissions in place, we can now go ahead and create a sample index file for each of our website.

We will do this using nano text editor

$ sudo nano /var/www/example.com/index.html

Paste the information below

<html>

<head>

  <title>

  Server Block 1/ Virtual Host 1

  </title>

  </head>

  <body>

  <p>This is Nginx virtual host for example.com website</p>

  </body>

</html>

Then press CTRL+X, Y and Enter to save the file.

Repeat the same procedure for the example.net website.

$ sudo nano /var/www/example.net/index.html

Then, enter the information below on the file.

<html>

 <head>

 <title>

 Server Block 1/ Virtual Host 2

 </title>

 </head>

 <body>

 <p>This is Nginx virtual host for example.net website</p>
 
 </body>

</html>

Save and close the file

Step 5: Creating Nginx Server Blocks on Ubuntu 18.04 for Multiple Websites

Our index pages are ready to be served. However, we haven’t instructed Nginx about the website directories that we have created and the name of our domains. This is where Nginx server blocks or virtual hosts come in to play.

As mentioned a while ago Nginx comes with a default virtual host or server block that serves content from the /var/www. The configuration file of this website is located at /etc/nginx/sites-available/default.

Basically, you need to add an Nginx server block on the /etc/nginx/sites-available/ directory for each website you intend to host with Nginx. However, this configuration file won’t be loaded when Nginx start until a link is created under the /etc/nginx/sites-enabled/ directory.

So to allow Nginx to load our example.com and example.net websites, we need to create a separate configuration file for each website on the /etc/nginx/sites-available/ directory and then create symbolic link on the /etc/nginx/sites-enabled/ directory.

First, create a server block for the example.com website.

$ sudo nano /etc/nginx/sites-available/example.com

Then, enter the information below in the file.

server {

            listen 80;

            listen [::]:80;

            server_name example.com;

            root /var/www/example.com/public_html;

            index index.html;

 

            location / {

                        try_files $uri $uri/ =404;

            }

}

Save and close the file by pressing CTRL+X, Y, and Enter.

We also need to create a new server block for the example.net website

$ sudo nano /etc/nginx/sites-available/example.net

Then, enter the information below in the file.

server {

            listen 80;

            listen [::]:80;

            server_name example.net;

            root /var/www/example.net/public_html;

            index index.html;

 

            location / {

                        try_files $uri $uri/ =404;

            }

}

Save the file by pressing CTRL+X, Y and Enter.

Step 6: Enabling Nginx Server Blocks on Ubuntu 18.04

With the Nginx server blocks in place, we can enable them by running the commands below

$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/

In order for the changes to take effect, restart Nginx web server.

$ sudo systemctl restart nginx

Step 7: Testing Nginx Server Blocks on Ubuntu 18.04

Once everything is in place, you can test your configuration. If you are running Windows on your local machine, you need to add example.com and example.net together with the public IP address associated with your VPS on the hosts’ file.

C:\Windows\System32\drivers\etc\hosts

Open the file and add the below information. Remember to replace the 192.0.0.1 with your VPS public IP address

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
...

192.0.0.1      example.com
192.0.0.1      example.net

Save the file and visit example.com website. If you followed the guide, you should see the below website.

Then, visit example.net website and confirm if you can see the below output.

Congratulations, if you have reached this far. That’s all when it comes to setting up multiple websites on Nginx with Ubuntu 18.04 server

Conclusion

In this guide, we have taken you through the steps of setting up Nginx server blocks on Ubuntu 18.04 VPS in order to run multiple websites from one server instance.

You can extend the server blocks and hosts as many websites as you want provided your server resources can handle the load.

Signup with Digital Ocean today to enjoy up to $100 worth of free trial credit and run multiple websites on their VPS server.

Ads by Google

Leave a Reply

Your email address will not be published. Required fields are marked *