Grid Guides

Explore How System On Grid Can Work For You

How to Setup Nginx Server Blocks on Ubuntu 18.04 LTS


Table of Contents

Introduction

Virtual hosts are used to run one or multiple websites on a single server. They allow the server to share its resources without the need of all services provided to use the same hostname. Virtual hosts are called server blocks on Nginx web server.

In this guide, we will explain to you how to setup Nginx server blocks on Ubuntu 18.04 LTS.

Prerequisites

To setup Nginx server blocks on Ubuntu 18.04 LTS, you should either have access to a root user account or a non-root user account with root privileges using the sudo command. Setup a non-root user account by following the guide, How to do Initial Server Setup with Ubuntu 18.04.

How to Setup Nginx Server Blocks

Install Nginx Web Server

Before installing Nginx server, update your server’s package index using apt package management suite. This is because to update the Ubuntu system to the most recent version as the software we use in the process will have Ubuntu’s default packages and to complete a few necessary installations.

$ sudo apt update

Then, install the Nginx server.

$ sudo apt install nginx

After installing the Nginx web server, test if it is working or not by typing the IP address in a web browser. It should direct to the Nginx’s default landing page.

http://IP_address

Output: nginxserverblocksonubuntu

Create a Web Directory for each Server Block

To explain you the process, I am going to create two Nginx server blocks namely, server1 and server2.

Now, you need to create two web directories to store the data of your Nginx server blocks.

Create a web directory for server1 server block using the below command.

$ sudo mkdir -p /var/www/html/server1.com/public_html

Create a web directory for server2 server block using the below command.

$ sudo mkdir -p /var/www/html/server2.com/public_html

You have successfully created two web directories which are owned by the root user.

Now, you need to change the ownership from the root user to the regular user. To do so, use the below commands.

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

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

In the above commands, USER refers to the currently logged in user.

Then, modify the read permissions to the Nginx root directory to give read-only permission for all users except the root user. To do so, use the below command.

$ sudo chmod -R 755 /var/www/html/

Create Sample Web Pages for each Server Block

Now, you need to create a sample web page for each of your server blocks, server1 and server2.

Create a sample web page for server1 using the below command.

$ sudo vi /var/www/html/server1.com/public_html/index.html

A file will be opened. Add the following lines in it.

<html>
 <head>
 <title>www.server1.com</title>
 </head>
 <body>
 <h1>This is server1</h1>
 </body>
</html>

Save and close the file.

Similarly, create a sample web page for server2 using the below command.

$ sudo vi /var/www/html/server2.com/public_html/index.html

A file will be opened. Add the following lines in it.

<html>
 <head>
 <title>www.server2.com</title>
 </head>
 <body>
 <h1>This is server2</h1>
 </body>
</html>

Save and close the file.

Create a Configuration File for each Server Block

Now, you need to create a configuration file for each of your server blocks, server1 and server2.

For that, you need to copy the contents of the default server block configuration file to the new server blocks configuration files. Do it by using the below commands.

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/server1.com.conf

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/server2.com.conf

Now, you need to edit the server1 configuration file. To do so, open it in a text editor using the below command.

$ sudo vi /etc/nginx/sites-available/server1.com.conf

Make the necessary changes as highlighted in bold letters below.

#Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html/server1.com/public_html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name server1.com www.server1.com;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

Save and close the file.

Similarly, edit the server2 configuration file by opening it in a text editor using the below command.

$ sudo vi /etc/nginx/sites-available/server2.com.conf

Make the necessary changes as highlighted in bold letters below.

#Default server configuration
#
server {
listen 80;
listen [::]:80;

#SSL configuration
#
#listen 443 ssl default_server;
#listen [::]:443 ssl default_server;
#
#Note: You should disable gzip for SSL traffic.
#See: https://bugs.debian.org/773332
#
#Read up on ssl_ciphers to ensure a secure configuration.
#See: https://bugs.debian.org/765782
#
#Self signed certs generated by the ssl-cert package
#Don't use them in a production server!
#
#include snippets/snakeoil.conf;

root /var/www/html/server2.com/public_html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name server2.com www.server2.com;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

Save and close the file.

Enable Nginx Server Blocks

After making the necessary changes, remove the default server block configuration file using the below command.

$ sudo rm /etc/nginx/sites-enabled/default

Now, enable the configuration files of the new server blocks by using the below commands.

$ sudo ln -s /etc/nginx/sites-available/server1.com.conf /etc/nginx/sites-enabled/

$ sudo ln -s /etc/nginx/sites-available/server2.com.conf /etc/nginx/sites-enabled/

Now, restart the Nginx to make the changes effective.

$ sudo systemctl restart nginx

Test Nginx Server Blocks

Now, it’s time to test your Nginx server blocks. To do so, open /etc/hosts file in a text editor using the below command.

$ sudo vi /etc/hosts

Add your server blocks in the file like below.

216.200.116.191 server1.com www.server1.com
216.200.116.191 server2.com www.server2.com

Save and close the file.

Now, open a web browser and enter http://server1.com and http://server2.com.

Output for http://server1.com:

testserver1

Output for http://server2.com:

testserver2

Conclusion

You have successfully created Nginx server blocks on Ubuntu 18.04 LTS.