YouTube Icon

Code Playground.

How To Set Up Nginx Server Blocks on Ubuntu 20.04

CFG

How To Set Up Nginx Server Blocks on Ubuntu 20.04

A worker block is a Nginx order that characterizes settings for a particular space, permitting you to run more than one site on a solitary worker. For every site, you can set the webpage archive root (the catalog which contains the site documents), make a different security strategy, utilize diverse SSL endorsements, and significantly more. 

This article depicts how to set up Nginx worker impedes on Ubuntu 20.04. 

Prerequisites

Guarantee that you have met the accompanying prerequisites before proceeding: 

  • Area name highlighting your public worker IP. 
  • Nginx introduced on your Ubuntu framework. 
  • You are signed in as root or client with sudo benefits . 

In certain articles, the expression "Worker Blocks" is alluded to as a "Virtual host". A virtual host is an Apache expression. 

Creating the Directory Structure

The archive root is where the site documents for a space name are put away and served because of solicitations. You can set the record root to any area you need. In this model, we will utilize the accompanying registry structure: 

/var/www/
??? domain1.com
?   ??? public_html
??? domain2.com
?   ??? public_html

Every space facilitated on the worker will have its report root set to/var/www/<domain_name>/public_html. 

Start by making the root catalog for the area: 

sudo mkdir -p /var/www/domain1.com/public_html

We'll additionally make an index.html record inside the area archive root catalog that will be indicated when you visit the space in your program: 

/var/www/example.com/public_html/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

Since the orders above are executed as a sudo client, the recently made records and indexes are possessed by root. To stay away from any consent issues change the responsibility for space archive root index and all records inside the catalog to the Nginx client (www-information) : 

sudo chown -R www-data: /var/www/domain1.com

Creating a Server Block

On Ubuntu frameworks, Nginx worker block design documents are situated in/and so forth/nginx/locales accessible catalog. They can be empowered by making emblematic connects to the/and so on/nginx/locales empowered index, which Nginx read during the startup. 

Open your word processor and make the accompanying worker block record: 

/etc/nginx/sites-available/example.com
server {
    listen 80;

    server_name example.com www.example.com;

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

    index index.html;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}
  • server_name: The spaces that should coordinate for this worker block design. 
  • root: The registry from which Nginx will serve the space documents. 
  • access_log, error_log: Specifies the area for log records. 

The setup document can be named anything you need, yet for the most part, it is ideal to utilize the area name. 

To empower the new worker block record, make a representative connection from the document to the destinations empowered catalog, which Nginx read during startup: 

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

Test the Nginx setup for right sentence structure: 

sudo nginx -t

On the off chance that there are no blunders, the yield will resemble this: 

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the Nginx administration for the progressions to produce results: 

sudo systemctl restart nginx

At last, to confirm that the worker block is functioning true to form, open http://example.com in your program of decision, and you will see something like this: 

Conclusion

We have told you the best way to make Nginx worker squares and host various areas on a solitary Ubuntu worker. You can rehash the means delineated above and make extra worker blocks for every one of your spaces. 

In the event that you are confronting any issue, don't hesitate to leave a remark.




CFG