DEV Community

Cover image for How to install NGINX on ubuntu 22.04
Max Diamond
Max Diamond

Posted on

How to install NGINX on ubuntu 22.04

What is NGINX?

NGINX, pronounced as "engine-x," is an open-source, high-performance web server, reverse proxy server, and load balancer that has redefined the way web applications are delivered to users worldwide. In the world of node, NGINX can be considered the go-to web server for connecting your apps to internet. So lets dive into how to set it up on Ubuntu 22.04, and how to configure your app with server blocks.

Installing NGINX on Ubuntu 22.04

Firstly, we'll need to install NGINX on your server. If you're using servers provided by Digital Ocean, this may already be installed and running.

To install NGINX, start by running the following:

sudo apt update
sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

Once installed, you can head to your server's IP address to see the default NGINX page.

Configuring a Server Block

Assuming you have an application running, likely with PM2, we'll move onto configuring a server block to enable access from the internet.

Firstly, we'll create a new file in /etc/nginx/sites-available/[yourapp].com .

Within this file, we'll add the following config. Be sure to change the http://localhost:3333 to point at the port your app is running on.

server {
    listen 80;  # Port to listen on (typically 80 for HTTP)
    server_name yourapp.com www.yourapp.com;  # Your domain name(s)

    location / {
        proxy_pass http://localhost:3333;  # Your Express app's address
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    # Optionally, configure access and error logs
    access_log /var/log/nginx/yourapp_access.log;
    error_log /var/log/nginx/yourapp_error.log;
}
Enter fullscreen mode Exit fullscreen mode

Save this file using CTRL + X and Y .

Reload NGINX

Before we go ahead and restart NGINX, we'll test that NGINX is happy with our changes using sudo nginx -t.

Finally, we'll restart NGINX using sudo systemctl restart nginx so our changes take effect.

Accessing your app

As long as your app is running, and NGINX is configured properly, you should be able to access it via your Server's IP address or a domain pointing towards your server.

Top comments (0)