...
Skip to main content
Print

How to Install Nginx on Ubuntu

You install Nginx on Ubuntu with apt install nginx. Then you open the firewall for HTTP and HTTPS and set up a server block, so your website is reachable on your own domain. Nginx is a fast and lightweight web server that is also widely used as a reverse proxy. In this article you install Nginx on a VPS with Ubuntu or Debian.

Step 1: install Nginx

First update the package list and then install Nginx:

apt update
apt install nginx -y

Check that Nginx is running:

systemctl status nginx

Step 2: open the firewall

If you use UFW, allow HTTP and HTTPS:

ufw allow 'Nginx Full'
ufw status

Then open the IP address of your VPS in your browser. If you see the Nginx welcome page, the web server is working.

Step 3: create a server block for your domain

Create a folder for your website and a configuration file for your domain:

mkdir -p /var/www/example.com/html
nano /etc/nginx/sites-available/example.com

Put this configuration in the file and replace example.com with your own domain:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html;

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

Enable the configuration, check the syntax and reload Nginx:

ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx

Point the A record of your domain to the IP address of your VPS. After that your website is reachable on your own domain.

Step 4: secure your website with HTTPS

With Certbot you request a free SSL certificate that configures Nginx automatically:

apt install certbot python3-certbot-nginx -y
certbot --nginx -d example.com -d www.example.com

Nginx on AlmaLinux

On AlmaLinux you install and start Nginx with dnf and open the firewall with firewalld:

dnf install nginx -y
systemctl enable --now nginx
firewall-cmd --permanent --add-service=http --add-service=https
firewall-cmd --reload

Useful Nginx commands

shellXITENS
nginx -tchecks the configuration for errors.
systemctl reload nginxloads a new configuration without interruption.
tail -f /var/log/nginx/error.logshows the error messages live.

Related articles

Table of Contents