| by Arround The Web | No comments

How To Set Up Nginx Server Blocks on Ubuntu 22.04

Nginx is an open-source, freely available HTTP server software. Additionally, it operates as a proxy server for email (SMTP, POP3, IMAP). Nginx also acts as a load balancer and reverse proxy for UDP, TCP, and HTTP servers. According to W3Tech, NGINX is currently the most widely used web server since it routinely outperforms Apache and other servers in benchmark tests assessing web server speed.

This blog will demonstrate the method to set up Nginx server blocks on Ubuntu 22.04. Let’s get started!

How to install Nginx on Ubuntu 22.04

For the purpose of installing Nginx on Ubuntu 22.04, follow the given instructions.

Step 1: Update system packages
First of all, hit “CTRL+ALT+T” and update the system packages:

$ sudo apt update

All packages are updated:

Step 2: Install Nginx
Next, install Nginx on your Ubuntu 22.04 system with the help of the provided command:

$ sudo apt install nginx -y

Step 3: Check Nginx version
After installing Nginx, verify if it is currently running or not:

$ systemctl status nginx

The given output indicates that the Nginx service is active and running on our system:

Step 4: Firewall Configuration
Now, enable the Firewall on your system:

$ sudo ufw enable

Step 5: List installed applications
View the list of installed applications using the following command:

$ sudo ufw app list

Step 6: Open ports for Nginx
Firstly, we will enable Nginx in “HTTP” by utilizing the provided command:

$ sudo ufw allow 'Nginx HTTP'

Or enable it in HTTPS:

$ sudo ufw allow 'Nginx HTTPS'

Another option is to enable Nginx fully for both HTTP and HTTPS:

$ sudo ufw allow 'Nginx FULL'

Step 7: Check Firewall status
Now, type out the given command to get to know about the Firewall status:

$ sudo ufw status

Step 8: Access Nginx
After configuring Firewall, it is time to access Nginx on the browser using the “localhost” or the “server IP”:

At this point, Nginx is working perfectly. So, we will now move ahead to set up server blocks for it.

How to set up Nginx server block on Ubuntu 22.04

For the purpose of setting up the Nginx server block on Ubuntu 22.04, follow the given instructions.

Step 1: Create Directory
In the first step, create a directory for the selected domain. In our case, the domain name will be “example.com”:

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

Step 2: Set Directory ownership
Next, utilize the “$USER” environment variable for setting the ownership of the created directory. The specified command will set the current logged-in user as its owner:

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

Step 3: Set File permissions
Then, we will assign the read, write, and execute file permissions to our “example.com” domain directory:

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

Step 4: Create HTML file
Using “nano” editor, create an HTML file that will be served as the home page of our domain:

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

Paste the given code in the opened HTML file, press “CTRL+O” for saving the added changes and switch back to the terminal by hitting “CTRL+X”:

Step 5: Set up Nginx server block
Now, we will set up an Nginx server block for our domain in the given directory:

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

Add the following content to the opened file, press “CTRL+S” to save it, and switch back to 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
Create a symlink for enabling the created Nginx server block:

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

Step 7: Nginx testing
Make sure that Nginx is working properly or not:

$ sudo nginx -t

Step 8: Restart Nginx
Restart Nginx on Ubuntu 22.04 with the help of the provided command:

$ sudo systemctl restart nginx

Step 9: Access Nginx server
Lastly, open your favorite browser, and access the created Nginx server block by surfing the specified domain name:

The given output indicates that we have successfully set up the Nginx server block on Ubuntu 22.04.

Conclusion

To set up Nginx Server Blocks on Ubuntu 22.04, firstly, update the system packages. Then, install Nginx with the “$ sudo apt install nginx -y” command. Then enable Firewall and open ports for Firewall. Next, create a directory for your domain, and change its directory permissions and file permission. Next, create an HTML file, and set up an Nginx server block that can be accessed via the added domain. This blog demonstrated the method of setting up Nginx blocks on Ubuntu 22.04.

Share Button

Source: linuxhint.com

Leave a Reply