Content Introduction Prerequisites Virtual Host Redirect to HTTPS Conclusion Introduction To secure the data transfer redirecting the HTTP traffic to...
Read MoreNginx Server Blocks can be used to run host multiple websites on a single server or machine. The Nginx Server Blocks are very similar to the virtual hosts in Apache. The blocks allow you to use different SSL certificates for each site and you can specify the site document root directory (which contains the website files), you can create a separate security policy for each site, and much more.
In this document, we’ll discuss how to set up server blocks with Nginx on Ubuntu.
The website files are stored inside the document root and served as a response to the requests for the domain name. You can set any location of this document root.
Below is the example of the directory structure we are using:
/var/www/
├── zehncloud1.com
│ └── public_html
├── zehncloud2.com
│ └── public_html
├── zehncloud3.com
│ └── public_html
As we want to host all of the domains on the server, we will create the directories for each of them inside /var/www directory, and then we will create a public_html directory that stores the files for the website.
Create the root directory for the domain e.g. zehncloud.com
sudo mkdir -p /var/www/zehncloud.com/public_html
Create an index.html file inside the document root directory of the domain for testing the site.
sudo vi /var/www/zehncloud.com/public_html/index.html
Insert the lines inside the index.html file
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Welcome to zehncloud.com</title>
</head>
<body>
<h1>Welcome to zehncloud.com home page!</h1>
</body>
</html>
Command to change the permissions and ownership of files and directories from root user to Nginx user (www-data):
sudo chown -R www-data: /var/www/zehncloud.com
The Nginx configuration files for the server blocks are stored in /etc/nginx/sites-available directory by default and these are enabled through the symbolic links to the directory /etc/nginx/sites-enabled.
Create a new file e.g. domain.com inside /etc/nginx/sites-available directory
sudo vi /etc/nginx/sites-available/zehncloud.com
Paste below lines into this file:
server {
listen 80;
listen [::]:80;
root /var/www/zehncloud.com/public_html;
index index.html;
server_name zehncloud.com www.zehncloud.com;
access_log /var/log/nginx/zehncloud.com.access.log;
error_log /var/log/nginx/zehncloud.com.error.log
location / {
try_files $uri $uri/ =404;
}
}
Create a symbolic link from this new file of server block to enable it to the sites-enabled directory, which Nginx reads during startup.
sudo ln -s /etc/nginx/sites-available/zehncloud.com /etc/nginx/sites-enabled/
Test the syntax of the Nginx configuration
sudo nginx -t
If there is no error then the output should be:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart the services of Nginx
sudo systemctl restart nginx
Open the browser of your choice and enter the URL e.g. https://www.zehncloud.com to verify the configuration is working fine or not.
The output should be like below:
In the above document, you have learned how to configure an Nginx server block to host more than one domain on a single Ubuntu server. You can repeat the above steps and create multiple server blocks for all your websites or domains.
Content Introduction Prerequisites Virtual Host Redirect to HTTPS Conclusion Introduction To secure the data transfer redirecting the HTTP traffic to...
Read MoreContent Introduction Requirement Getting Started Conclusion Introduction Angular is an open-source web application framework. It is a TypeScript-based free and development...
Read More