How to Set up a Nginx Server Block on Ubuntu Linux

This post provides a guide on configuring an Nginx server block file on Ubuntu Linux to host multiple websites on a single server. The tutorial covers creating website directory structures including a site’s document root, modifying permissions, preparing basic HTML content for testing, and setting up Nginx virtual host files. The steps are repeatable for…

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

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

The Nginx server block feature allows web admins to maximize server resources by running multiple websites on one host instead of multiple hosts running multiple websites.

How to create website directory structures on Ubuntu Linux

Each website will have its document root when running multiple websites on a single host. A document root is a directory where the website files for a domain are stored and served in response to requests.

Below 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 folder with a document root included.

Example: /var/www/domain/public_html

Run the commands below to create a directory, for example.com domain with its document root.

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

Each document root will need an index.html file to be shown to clients. Run the commands below to create an index.html file for the 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 can configure Nginx to reference this content. To avoid permission issues, change the domain document root directory ownership and all files within the directory to the www-data user.

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

How to create an Nginx server block file on Ubuntu Linux

Now that you’ve created domain content in the directory above configure the Nginx server block configuration file for the above domain content.

The Nginx server block configuration files on Ubuntu Linux are in the /etc/nginx/sites-available directory.

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

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

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

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 enable it to become a working server block.

You use the symbolic link command to enable the new server block configuration file, which creates a symbolic link from the server block file to the sites-enabled directory.

Run the commands below 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 commands below to restart the Nginx service.

sudo systemctl restart nginx

Once Nginx is restarted, you browse to the server hostname or IP address, and it should display the content file we created above.

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

That should do it!

Conclusion:

  • Configuring Nginx server blocks allows you to host multiple websites on a single server, optimizing resource usage.
  • Each website requires a unique directory structure, document root, and configuration settings.
  • By following the steps outlined, users can create and enable server blocks for different domains effectively.
  • Always ensure proper permissions are set for document root directories to avoid access issues.
  • Regularly check server logs to monitor traffic and troubleshoot any problems that may arise.
  • Feel free to create additional server blocks as needed, expanding your web hosting capabilities.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version