By means of Nginx Server Blocks you can run multiple website on a single server. Using Server Blocks, you can specify the separate document root for each domain. In addition, you also can create a separate security policy for each site and use different SSL certificates for each site and much more.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Nginx queries.
In this context, we shall look into how to set up Nginx Server Blocks on a Ubuntu 18.04 server.
As in Apache, Nginx Server Blocks are also known as Virtual Hosts.
Before perform this procedure, you need to ensure that the following requirements are met:
You should logged in on a Ubuntu server with a non-root user with sudo privileges.
A domain name should pointing to your server IP address.
Nginx should installed and configured, as shown in How to Install Nginx on Ubuntu 18.04 Server.
To begin, we are going to create a directory structure to store the site data and serve response to visitors.
The main directory is considered as DocumentRoot directory. You can specify any location as document root as per your choice but it is better way to set in directory structure.
So we will store all document root at /var/www directory:
$ /var/www/
├── example1.com
│ └── public_html
├── example2.com
│ └── public_html
Now, We will create separate directory for each domain inside /var/www directory. Within this directory, we'll create a public_html directory as domain document root directory to store website data:
$ sudo mkdir -p /var/www/example.com/public_html
After that, for testing purpose we are going to create a index.html file within the domain document root directory.
This page will display as default page when visitors visit your website.
With the following command, you can create a new index.html file with your favorite text editor:
$ sudo nano /var/www/example.com/public_html/index.html
Add the following lines into it:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Welcome!!</title>
</head>
<body>
<h1>Great! example.com set up completed!</h1>
</body>
</html>
All commands run as sudo user so we need to change ownership of all new created files and directories. So it will not create any permission issue later and our regular user can modify files without any problems:
$ sudo chown -R www-data: /var/www/example.com
By default on Ubuntu system, location for Nginx server blocks configuration files is at /etc/nginx/sites-available directory. To used them you need to enable by creating symbolic links to the /etc/nginx/sites-enabled/ directory.
Now, create a new file for example.com using your choice text editor by typing:
$ sudo nano /etc/nginx/sites-available/example.com.conf
Add the following lines in to this file:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/public_html;
index index.html;
server_name example.com www.example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
It's best practice to give file name same as domain name. You can give any names to your configuration file as per your choice.
Save and close file.
Next, We will enable nginx server block by making symbolic link at /etc/nginx/sites-enabled/ directory. To create symbolic link run the below command:
$ sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
We will test the Nginx configuration for correct syntax by typing:
$ sudo nginx -t
If there is no error, you will get following output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart the Nginx service to take effect using below command:
$ sudo systemctl restart nginx
Finally, to verify the server block is working as expected open http://example.com to your web browser and it should show you as following :
Great! example.com set up completed!
This article covers how to create an Nginx server blocks to host multiple website on a single Ubuntu machine. When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server.