Site icon WPDig.com

How to configure an Nginx server block on Ubuntu Linux

This post shows students and new users the steps to set up an Nginx server block file on Ubuntu Linux. A server block is an Nginx feature that allows users to run more than one website on a single server.

A server block file contains configuration directives for a website, including site document root, security policies, SSL certificate settings, and much more. Each website that is configured within an Nginx server block operates independently of each other with separate and unique settings.

The Nginx server lockout feature enables the webmaster to maximize server resources by running multiple websites on one host instead of multiple hosts running multiple websites.

How to create website directory structures in Ubuntu Linux

When you run multiple websites on a single host, each website will have its own document root. The root of a document is the directory where website files for a domain are stored and served in response to requests.

Here is an example of a directory structure for multiple websites with unique content and domains.

/var/www/
├── example.com
│   └── public_html
├── example.net
│   └── public_html

As you can see above, each domain will have its own folder with a document root included.

Example: / var / www /domain/ public_html

Run the following commands to create a directory for example.com domain with your document root.

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

Each document root will need a index.html file to be shown to clients. Run the following commands to create a index.html file for him example.com domain.

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

Then copy and paste the content below into the file and save it. Below is just a basic HTML file for testing purposes.

<!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>

Once you save the file, we are ready to configure Nginx to reference this content. To avoid permission issues, change the ownership of the domain document root directory and all files within the directory to www-data Username.

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

How to create an Nginx server blcok file on Ubuntu Linux

Now that you have created a domain content in the above directory, go ahead and configure the Nginx server block configuration file for the above domain content.

On Ubuntu Linux, the configuration files for the Nginx server block are located in the / etc / nginx / sites-available directory.

To create a server block file in the available sites directory for our above content, run the commands below to create a site-specific virtual host configuration file

sudo nano /etc/nginx/sites-available/example.com.conf

Here is an example configuration that should work with most environments. Copy and paste the content below into the file above and save it.

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;
}

Once the file is saved you can go ahead and enable it to become a working server block.

To enable the new server block configuration file, use the symbolic link command that creates a symbolic link from the server’s block file to the enabled sites directory.

Run the following commands to enable the configuration file for our domain.

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

After that, run the following commands to restart the Nginx service.

sudo systemctl restart nginx

Once Nginx restarts it now navigates to the server hostname or IP address and it should display the content file we created earlier.

Repeat this for other domains and server block configuration files that you want to create. You can create as many virtual host files as you can as long as the server can run all of them.

You should do that!

Conclusion:

In this tutorial we have seen how to create an Nginx server block on Ubuntu Linux. If you find any errors above or have something to add, use the comment form below.

Exit mobile version