How to Set Up Nginx Server Blocks on Ubuntu 22.04

Choose a different version or distribution

Introduction

Before we begin talking about how to set up Nginx Server Blocks on Ubuntu 22.04, let's briefly understand – What are Nginx Server Blocks?

Nginx Server Blocks, also known as virtual hosts, are a powerful feature of the Nginx web server. They allow hosting multiple websites or applications on a single server, each with its own domain or IP address.

With Nginx Server Blocks, you can efficiently manage and separate different websites, customize configurations, and optimize performance. This flexibility makes Nginx Server Blocks a popular choice for developers and system administrators.

In this tutorial, you will learn how to set up Nginx server blocks on Ubuntu 22.04. We will also address a few FAQs on how to install Nginx Server Blocks on Ubuntu 22.04.

Advantages of Nginx Server Blocks

  1. Efficient hosting: Nginx Server Blocks allow hosting multiple websites or applications on a single server, optimizing resource usage.
  2. Custom configurations: Each server block can have its own configuration, enabling tailored settings for individual websites or applications.
  3. Domain separation: Server Blocks enable managing multiple domains or IP addresses, providing clear separation between websites.
  4. Performance optimization: Nginx's high-performance architecture enhances website speed and handles high traffic loads efficiently.
  5. Easy scalability: Server Blocks simplify adding or removing websites, making it easy to scale your infrastructure as your needs evolve.

How to install Nginx on Ubuntu 22.04

Follow the directions provided in order to install Nginx on Ubuntu 22.04.

Step 1: Update system packages

Use CTRL+ALT+T to update the system packages first:

sudo apt update

Every package has been updated.

Step 2: Install Nginx

Next, use the given command to install Nginx on your Ubuntu 22.04 computer:

sudo apt install nginx -y

Step 3: Check Nginx version

Next, use the given command to install Nginx on your Ubuntu 22.04 machine:

systemctl status nginx

The output displayed will show that our system's Nginx service is active and running.

Step 4: Firewall Configuration

Now, turn on your system's firewall:

sudo ufw enable

Step 5: List installed applications

Use the following command to view the list of installed programs:

sudo ufw app list

Step 6: Open ports for Nginx

First, we will use the following command to enable Nginx in "HTTP":

sudo ufw allow 'Nginx HTTP'

Alternately, enable HTTPS:

sudo ufw allow 'Nginx HTTPS'

Another choice is to fully enable Nginx for HTTP and HTTPS:

sudo ufw allow 'Nginx FULL'

Step 7: Check Firewall status

Now, enter the following command to learn the status of the firewall:

sudo ufw status

Step 8: Access Nginx

After setting up the firewall, use "localhost" or the "server IP" to access Nginx in the browser:

Nginx is currently operating flawlessly. As a result, we will now proceed to configure server blocks for it.

How to set up Nginx server block on Ubuntu 22.04

In order to install the Nginx server block on Ubuntu 22.04, follow the given instructions.

Step 1: Create Directory

Create a directory for the chosen domain in the first step. The domain name in this instance will be "example.com":

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

Step 2: Set Directory Ownership

Next, set the ownership of the newly created directory using the "$USER" environment variable. The currently logged-in user will be set as the command's owner when it is executed:

sudo chown -R $USER:$USER /var/www/example.com/html

Step 3: Set File permissions

Finally, we'll give our "example.com" domain directory read, write, and execute file permissions:

sudo chmod -R 755 /var/www/example.com

Step 4: Create HTML file

Create an HTML file using the "nano" editor that will act as our domain's home page:

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

Copy the provided code, open an HTML file, press "CTRL+O" to save the modifications, then press "CTRL+X" to return to the terminal:

Step 5: Set up Nginx server block

In the directory provided, we will now configure a Nginx server block for our domain:

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

Enter the following information to the file that has just been opened, hit "CTRL+S" to save it, then return to the terminal:

server {
        listen 80;
        listen [::]:80;
        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;
        server_name example.com www.example.com;

        location / {
               try_files $uri $uri/ =404;
        }
}

Step 6: Enable Nginx server block

To enable the newly built Nginx server block, build a symlink and unlink the default block:

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

Step 7: Nginx testing

Verify whether Nginx is functioning properly.

sudo nginx -t

Step 8: Restart Nginx

Nginx can be restarted on Ubuntu 22.04 by using the supplied command:

sudo systemctl restart nginx

Step 9: Access Nginx server

Lastly, launch your preferred browser and navigate to the created Nginx server block by typing the following domain name into the address bar:

The output shows that the Nginx server block has been successfully installed on Ubuntu 22.04.

FAQs to Set Up Nginx Server Blocks on Ubuntu 22.04

How can I create a new Server Block in Nginx?

To create a new Server Block, you need to create a new configuration file in the /etc/nginx/sites-available directory and link it to the /etc/nginx/sites-enabled directory.

How do I enable a Server Block in Nginx?

To enable a Server Block, you can create a symbolic link from the configuration file in the /etc/nginx/sites-available directory to the /etc/nginx/sites-enabled directory using the ln command.

How do I configure a Server Block for a specific domain?

In the Server Block configuration file, specify the domain name in the server_name directive and define the necessary settings for that domain.

Can I have multiple Server Blocks for the same domain?

Yes, you can have multiple Server Blocks for the same domain by specifying different server names and port numbers in each configuration file.

How do I test the Nginx configuration for Server Blocks?

You can test the Nginx configuration for Server Blocks by running the command sudo nginx -t in the terminal. It will check for syntax errors in the configuration files.

How do I reload Nginx after modifying Server Block configurations?

After making changes to the Server Block configurations, you can reload Nginx by running the command sudo systemctl reload nginx in the terminal.

How do I disable a Server Block in Nginx?

To disable a Server Block, you can remove the symbolic link from the /etc/nginx/sites-enabled directory or delete the corresponding configuration file in the /etc/nginx/sites-available directory. Then, reload Nginx for the changes to take effect.

Conclusion

Hope this detailed tutorial helped you understand how to set up Nginx Server Blocks on Ubuntu 22.04.

If you have any suggestions or queries, kindly leave them in the comments section.