DEV Community

Cover image for How to setup and configure NGINX on Ubuntu
Vishnu Satheesh
Vishnu Satheesh

Posted on

How to setup and configure NGINX on Ubuntu

In this article, you will learn how to setup and configure NGINX on ubuntu, configuring the firewall, managing the NGINX service, and setting up server blocks to host multiple domains on a single server.

But seriously, what is NGINX, and why do we need it?

NGINX (pronounced "engine-x") is a powerful web server that helps deliver your website or app to users. When someone types your domain into a browser, NGINX takes that request and sends back your app or website.

But that’s not all, it can also act as a reverse proxy, meaning it takes incoming requests and passes them to another server (like your app running in Docker). It helps handle more traffic, load content faster, and even improves security.

Think of it like a smart traffic controller for your web application.

What You Need to Get Started

Before starting to setup, make sure you have a regular user with sudo privileges set up on your server. Additionally, having a registered domain name is optional, but it can be useful later.

So, let’s begin,

Step 1: Install NGINX

To get started, update your package list and install NGINX by running the following commands in your terminal:

sudo apt update
sudo apt install nginx

Enter fullscreen mode Exit fullscreen mode

This will download and install NGINX on your server.

Once installed, NGINX should start running automatically. You can check its status using:

sudo systemctl status nginx

Enter fullscreen mode Exit fullscreen mode
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Thu 2025-05-22 11:32:05 CEST; 2 days ago
       Docs: man:nginx(8)
    Process: 412781 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 412783 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 4007914 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
   Main PID: 412784 (nginx)
      Tasks: 9 (limit: 76810)
     Memory: 27.7M (peak: 37.9M)
        CPU: 31.933s
     CGroup: /system.slice/nginx.service
             ├─ 412784 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─4007934 "nginx: worker process"
             ├─4007935 "nginx: worker process"
             ├─4007936 "nginx: worker process"
             ├─4007937 "nginx: worker process"
             ├─4007938 "nginx: worker process"
             ├─4007939 "nginx: worker process"
             ├─4007941 "nginx: worker process"
             └─4007942 "nginx: worker process"

May 22 11:32:04 vo systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...
May 22 11:32:05 vo systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.
May 24 12:40:05 vo systemd[1]: Reloading nginx.service - A high performance web server and a reverse proxy server...
May 24 12:40:06 vo nginx[4007914]: 2025/05/24 12:40:05 [notice] 4007914#4007914: signal process started
May 24 12:40:06 vo systemd[1]: Reloaded nginx.service - A high performance web server and a reverse proxy server.

Enter fullscreen mode Exit fullscreen mode

The output shows that NGINX is running successfully. But the easiest way to confirm it is by visiting your server’s IP address in a browser to see if the NGINX welcome page appears.

Step 2: Adjusting the Firewall (UFW)

If your server uses UFW (Uncomplicated Firewall), you'll need to allow NGINX traffic so that it can respond to incoming web requests.

UFW helps control which traffic is allowed or blocked from reaching your server. Think of it like a gatekeeper, it only lets through what you approve.

Step 1: Check Available NGINX App Profiles

Run this command to see what NGINX profiles are available:

sudo ufw app list
Enter fullscreen mode Exit fullscreen mode

You’ll see a list of available application profiles:

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS

Enter fullscreen mode Exit fullscreen mode

Nginx HTTP: Allows only port 80 (non-secure HTTP)
Nginx HTTPS: Allows only port 443 (secure HTTPS)
Nginx Full: Allows both HTTP and HTTPS

Step 2: Allow NGINX Through the Firewall

To allow both HTTP and HTTPS traffic, run:

sudo ufw allow 'Nginx Full'
Enter fullscreen mode Exit fullscreen mode
Step 3: Enable UFW (if it’s not already enabled)
sudo ufw enable
Enter fullscreen mode Exit fullscreen mode
Step 4: Check the Status
sudo ufw status
Enter fullscreen mode Exit fullscreen mode

You should see something like:

Status: active
To                         Action      From
--                         ------      ----
Nginx Full                 ALLOW       Anywhere

Enter fullscreen mode Exit fullscreen mode

Step 3: Check NGINX in Browser

Open your browser and type your server’s IP address.

If you don’t know how to find the IP address of your server, run the following command:

hostname -I
Enter fullscreen mode Exit fullscreen mode

Then, open your browser and type the IP address in the address bar.

nginx configuration

If you see this page, it means NGINX is working properly and your server is set up correctly.

Step 3: Managing the NGINX process

Now that your web server is up and running, here are some basic commands to manage NGINX,

To stop the NGINX server, run:

sudo systemctl stop nginx
Enter fullscreen mode Exit fullscreen mode

To start it again, use:

sudo systemctl start nginx
Enter fullscreen mode Exit fullscreen mode

To restart the server (stop and start again in one command):

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

To reload the configuration without stopping the server:

sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

To check if NGINX is running:

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Step 4: Setting Up Server Blocks (Virtual Hosts)

Now that NGINX is up and running, let’s talk about how to host multiple websites on the same server using server blocks (also called virtual hosts).

Think of server blocks as a way for NGINX to know which website to show based on the domain name in the request. For example, you might want example.com to load one website and yourblog.com to load another, all from the same server and IP address.

Here’s a simple example to host a second site:

Step 1 : Create a directory for your website files
sudo mkdir -p /var/www/yourblog.com/html
Enter fullscreen mode Exit fullscreen mode
Step 2 : Add an index.html file
echo "<h1>Welcome to Your blog</h1>" | sudo tee /var/www/yourblog.com/html/index.html
Enter fullscreen mode Exit fullscreen mode
Step 3 : Create a new server block file
sudo nano /etc/nginx/sites-available/yourblog.com
Enter fullscreen mode Exit fullscreen mode

Add the following content:

server {
    listen 80;
    server_name yourblog.com www.yourblog.com;

    root /var/www/yourblog.com/html;
    index index.html;

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

Enter fullscreen mode Exit fullscreen mode
Step 4 : Enable the configuration
sudo ln -s /etc/nginx/sites-available/yourblog.com /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode
Step 5 : Test NGINX and reload
sudo nginx -t
sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Now, if you point yourblog.com to your server’s IP (via DNS or /etc/hosts), NGINX will show your custom website.

Conclusion

Setting up and configuring NGINX on Ubuntu is a crucial step in deploying modern web applications. Whether you're serving a simple static site or acting as a reverse proxy for complex backend services, NGINX offers performance, flexibility, and ease of management. In this guide, you learned how to install NGINX, configure the firewall, manage the service, and set up server blocks to host multiple websites on the same server.

With NGINX properly configured, your server is now equipped to efficiently handle web traffic, serve content securely, and scale with your application’s growth.

If you found this guide helpful or have suggestions, feel free to drop a comment or reach out I’d love to hear from you! 😊

Happy coding and don’t forget to spread your smile everywhere you go! 😄

If you’d like to connect or support my work:
📧 Feel free to reach out via email: [email protected]

☕ If you found this helpful and want to support my content, you can donate : Buy me a Coffee

Top comments (0)