How To Install Nginx on Ubuntu and Ubuntu-based Distribution

Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Overview

Some of the biggest and busiest websites on the internet are hosted by Nginx, one of the most well-known web servers in the world. It’s a simple option that works well as a reverse proxy or web server.

This tutorial will cover installing Nginx on your Ubuntu 22.04 server, configuring server blocks to host several domains from a single server, modifying the firewall, and controlling the Nginx process.

Using the apt packaging system, Nginx, a powerful web server, can be easily installed on Ubuntu 22.04. This guide will walk you through the installation process, adjusting the firewall settings, checking your web server’s status, managing the Nginx process, and setting up server blocks for multiple domains.

Step 1: Installing Nginx

Since Nginx is available in Ubuntu’s default repositories, the installation is straightforward. Begin by updating the local package index:

sudo apt update


Then, install Nginx:

sudo apt install nginx


Now run this in the terminal:

curl -4 icanhazip.com


You will get the IP address of your server. When you have your server’s IP address, enter it into your browser’s address bar:

http://your_server_ip
image 2 - How To Install Nginx on Ubuntu and Ubuntu-based Distribution

You should receive the default Nginx landing page. If you are on this page, your server is running correctly and is ready to be managed.

Accept the installation procedure, and apt will install Nginx along with any required dependencies.

Step 2: Adjusting the Firewall


Before testing Nginx, adjust the firewall to allow access. Nginx registers itself as a service with ufw, making it easy to allow Nginx access. List the available application configurations:

sudo ufw app list


Enable traffic on port 80 (HTTP):

sudo ufw allow 'Nginx HTTP'


Verify the change:

sudo ufw status


Step 3: Checking your Web Server


After installation, Ubuntu 20.04 starts Nginx. Confirm that the service is running:

systemctl status nginx


You should see an active and running status. Test your web server by accessing the default Nginx landing page using your server’s IP address.

Step 4: Managing the Nginx Process


Learn basic management commands for Nginx:

Stop the web server:

sudo systemctl stop nginx


Start the web server:

sudo systemctl start nginx


Restart the web server:

sudo systemctl restart nginx


Reload the configuration without dropping connections:

sudo systemctl reload nginx


Disable automatic startup:

sudo systemctl disable nginx


Re-enable automatic startup:

sudo systemctl enable nginx


Step 5: Setting Up Server Blocks (Recommended)


Configure server blocks to host multiple domains. Create a directory structure for your domain:

sudo mkdir -p /var/www/your_domain/html
sudo chown -R $USER:$USER /var/www/your_domain/html
sudo chmod -R 755 /var/www/your_domain


Create a sample index.html:

sudo nano /var/www/your_domain/html/index.html


Inside, add the following sample HTML:

<html>
    <head>
        <title>Welcome to your_domain!</title>
    </head>
    <body>
        <h1>Success!  The your_domain server block is working!</h1>
    </body>
</html>

Add sample HTML, save, and create a server block configuration:

sudo nano /etc/nginx/sites-available/your_domain


Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name into here /etc/nginx/sites-available/your_domain

server {
        listen 80
        listen [::]:80;

        root /var/www/your_domain/html;
        index index.html index.htm index.nginx-debian.html;

        server_name your_domain www.your_domain;

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

Notice that we’ve updated the root configuration to our new directory, and the server_name to our domain name.

Next, let’s enable the file by creating a link from it to the sites-enabled directory, which Nginx reads from during startup:

Paste the configuration, enable the file, and restart Nginx:

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx


Note: Nginx uses a common practice called symbolic links, or symlinks, to track which of your server blocks are enabled. Creating a symlink is like creating a shortcut on disk, so that you could later delete the shortcut from the sites-enabled directory while keeping the server block in sites-available if you wanted to enable it.

Nginx should now be serving your domain name. You can test this by navigating to http://your_domain, where you should see something like this:

image 3 - How To Install Nginx on Ubuntu and Ubuntu-based Distribution


This guide will walk you through the installation process, adjusting the firewall settings, checking your web server’s status, managing the Nginx process, and setting up server blocks for multiple domains.

Step 6: Getting Familiar with Important Nginx Files and Directories


Familiarize yourself with essential directories and files:

Content:

/var/www/html: Web content directory.


Server Configuration:

/etc/nginx: Nginx configuration directory.
/etc/nginx/nginx.conf: Main Nginx configuration file.
/etc/nginx/sites-available/: Directory for per-site server blocks.
/etc/nginx/sites-enabled/: Directory for enabled server blocks.
/etc/nginx/snippets: Directory for reusable configuration fragments.


Server Logs:

/var/log/nginx/access.log: Logs every request.
/var/log/nginx/error.log: Logs Nginx errors.



Now that Nginx is installed, you can explore various options for serving content and implementing technologies for a richer experience. To expand your application stack, consider the LEMP stack installation. For securing your domain with HTTPS, proceed to the guide on securing Nginx with Let’s Encrypt on Ubuntu 22.04.

Share The Blog With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Leave a Reply

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

Advanced topics are covered in our ebooks with many examples.

Recent Posts

pexels-abet-llacer-919734
What is Linux and what does it do?