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.

How to Configure Virtual Hosts On Nginx With CentOS 7

Nginx server blocks also known as Nginx virtual hosts are configurations for setting up multiple websites on a single server.

If you have more than one domain but you don’t want to incur additional costs on VPS plans, you may utilize the Nginx server blocks feature.

Although Nginx is a free and an open-source web server, it can handle more concurrent connections than Apache.

in this guide, we will show you how to set up multiple websites on Nginx with CentOs 7 as the operating system.

Prerequisites

Before you begin, make sure you have the following:

Step 1: Installing Nginx on CentOs 7 Server

We will start off by installing Nginx web server. The package is available on the EPEL repository. So, we need to add the information on the server:

$ sudo yum install epel-release

Then, install Nginx:

$ sudo yum install nginx

Press Y and hit Enter when prompted to confirm the installation.

Once the Nginx web server is installed, start it by typing the command below:

$ sudo systemctl start nginx

Then, enable Nginx to start at boot.

$ sudo systemctl enable nginx

Then, allow the HTTP and HTTPS traffic from CentOs firewall.

$ sudo firewall-cmd --zone=public --permanent --add-service=http
$ sudo firewall-cmd --zone=public --permanent --add-service=https
$ sudo firewall-cmd --reload

You can then confirm if the installation was successful by entering the IP address associated with your server on a browser like Google Chrome.

http://192.0.0.1

You should see the default Nginx home page as shown below:

Step 2: Setting Up Nginx Virtual Host on the First Domain

Next, we are going to configure the first virtual host on Nginx. We will use the example.com as the first domain.

The default Nginx configuration file can be located at /etc/nginx/nginx.conf

Also, the root of the default Nginx website points to  /usr/share/nginx/html.

Since the above directory setup can be a little bit confusing for hosting multiple websites on Nginx, we will create a structure which is more understandable.

First, let’s create the directory to hold our example.com websites data.

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

The -p option tells Linux to create any parent directory on the way.

The directory we created above is owned by the root user. However, we want our regular user to be able to modify files on it, so let’s change the directory ownership.

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

Next, we need to set up the appropriate file permissions to allow Nginx to read and execute the files from the public_html directory.

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

Once the directory structure is in place we can create a sample web page on the root of the example.com directory

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

Then, paste the information below on the file

<html>
  <head>
    <title>Sample web page on example.com website</title>
  </head>
  <body>
    <h1>Nginx server block 1</h1>
    This sample web page confirms that the first Nginx virtual host 
   or server block is working for example.com
  </body>
</html>

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

Next, we need to create a server block for the example.com website.

All Nginx virtual host files must end with .conf

The server block configuration files must be placed in the /etc/nginx/conf.d directory.

So let’s create the first Nginx block for the example.com website using nano text editor.

$ sudo nano /etc/nginx/conf.d/example.com.conf

Then, enter the information below on the file

server {
    listen  80;
    server_name example.com;

    location / {
        root  /var/www/example.com/public_html;
        index  index.html index.htm;
    }

    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }
}

Step 3: Setting Up Nginx Virtual Host on the Second Domain

Our server block for the first host is set up. Next, we can go ahead and configure the second virtual host for example.net domain.

First, we create the directory structure.

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

Then, change the directory ownership.

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

Next, let’s create a sample web page on the example.net root directory.

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

We can now add the information below to render the HTML web page

<html>
  <head>
    <title>Sample web page on example.net website</title>
  </head>
  <body>
    <h1>Nginx server block 2</h1>
    This sample web page confirms that the first Nginx virtual host 
   or server block is working for example.net
  </body>
</html>

Save and close the file.

We can now go ahead and create a server block for the example.net website.

$ sudo nano /etc/nginx/conf.d/example.net.conf

Then, enter the information below on the file

server {
    listen  80;
    server_name example.net;

    location / {
        root  /var/www/example.net/public_html;
        index  index.html index.htm;
    }

    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }
}

Restart the Nginx web server for the configurations to take effect:

$ sudo systemctl restart nginx

Step 4: Testing Nginx Server Blocks Configuration

To test the configuration add example.com and example.net on your hosts file together with the associated IP address of your VPS machine.

If you are on Windows, locate the file from C:\Windows\System32\drivers\etc\hosts

Then, add the two virtual hosts information at the end as show below.

# Copyright (c) 1993-2009 Microsoft Corp.
...
#	127.0.0.1       localhost
#	::1             localhost

..

149.28.188.94      example.com
149.28.188.94      example.net

Then, example.com on a web browser. You should see a page similar to the one shown below.

You can also visit example.net website which should render the below web page.

If you have reached this far, congratulations! You just learnt how to host multiple websites on Nginx web server running on Centos 7 operating system.

Conclusion

In this guide, we have shown you how to configure multiple websites on CentOs 7 with Nginx using server blocks or virtual hosts.

Sign up with Digital Ocean today and get $100 to host as many websites as you want on one VPS machine.