How to Configure Nginx Server Blocks on Ubuntu 22.04

How to Configure Nginx Server Blocks on Ubuntu 22.04

In the realm of web hosting and server management, Nginx stands tall as a versatile, high-performance HTTP and reverse proxy server. With its robust capabilities, Nginx efficiently handles the demands of some of the largest websites on the Internet. In this comprehensive guide, we'll delve into the intricacies of setting up Nginx server blocks on Ubuntu 22.04. By the end of this tutorial, you'll master the art of hosting multiple websites on a single server, each tailored to your specific requirements.

Understanding Nginx Server Blocks

Before diving into the configuration process, let's elucidate the concept of Nginx server blocks. Think of server blocks as directives within Nginx configuration files that delineate settings for individual domains. By employing server blocks, you can effortlessly manage multiple websites from a solitary server, a feat facilitated by Nginx's flexibility and scalability.

Prerequisites

Before embarking on the configuration journey, ensure that your system meets the following prerequisites:

  • The domain name is configured to point to your server's public IP address.

  • Nginx is installed on your Ubuntu 22.04 system.

  • You are logged in as either the root user or a user endowed with sudo privileges.

Creating the Directory Structure

The foundation of any web hosting endeavor lies in the directory structure where website files reside. With Nginx, you have the liberty to designate the document root for each domain, empowering you to organize your web content efficiently. Let's illustrate the directory structure:

/var/www/
├── yourdomain1.com
│   └── public_html
├── yourdomain2.com
│   └── public_html

Initiate the process by crafting the root directory for your domain:

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

Subsequently, fashion an index.html file and deposit it within the domain's root directory. This file will serve as the default landing page when visitors access your domain:

/var/www/yourdomain1.com/public_html/index.html

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

To avert potential permission issues, ensure that the ownership of the domain document root directory and its contents is attributed to the Nginx user (www-data):

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

Crafting a Server Block

Nestled within the /etc/nginx/sites-available directory, Nginx configuration files govern the behavior of server blocks. To birth a new server block, initiate your preferred text editor and concoct the following configuration file:

/etc/nginx/sites-available/yourdomain1.com

server {
  listen 80;
  server_name yourdomain1.com www.yourdomain1.com;
  root /var/www/yourdomain1.com/public_html;
  index index.html;
  access_log /var/log/nginx/yourdomain1.com.access.log;
  error_log /var/log/nginx/yourdomain1.com.error.log;
}

In this configuration snippet:

  • listen 80; instructs Nginx to listen on port 80, the default HTTP port.

  • server_name delineates the domain(s) associated with this server block.

  • root specifies the document root directory for the domain.

  • index designates the default landing page for the domain.

  • access_log and error_log specify the locations for access and error logs, respectively.

Enabling the Server Block

To activate the newly minted server block, forge a symbolic link to the /etc/nginx/sites-enabled directory, which Nginx peruses during startup:

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

To validate the correctness of your Nginx configuration syntax, execute the following command:

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

If no errors manifest, proceed to restart the Nginx service to effectuate the changes:

sudo systemctl restart nginx

Conclusion

Congratulations! You've adeptly navigated the intricacies of configuring Nginx server blocks on Ubuntu 22.04. Armed with this knowledge, you possess the prowess to host and manage multiple domains with finesse. Should you encounter any hurdles along the way, don't hesitate to reach out. Your journey towards web hosting mastery has just begun!