Skip to content
HOSTIGAN
Linux

How to install Nginx on Ubuntu

Install Nginx on Ubuntu 24.04, create a server block for your domain, add PHP-FPM and a free Let's Encrypt certificate, and use Nginx as a reverse proxy for apps.

Intermediate 5 min read Updated November 24, 2026

Nginx is a fast, lightweight web server. It serves static files, hands PHP to PHP-FPM and works as a reverse proxy in front of Node.js, Python or Docker applications. On Ubuntu it takes a few minutes to install, and the layout of its configuration files is easy to follow once you have seen it.

In this guide you will install Nginx on Ubuntu 24.04, serve a domain from its own server block, add PHP, switch on HTTPS with Let's Encrypt and set up a reverse proxy. The commands also work on Ubuntu 22.04, apart from version numbers.

Before you start

You need:

  • An Ubuntu server you can reach over SSH, with a user that has sudo rights.
  • A domain whose A and AAAA records (for both example.com and www.example.com) point to the server. Let's Encrypt needs this to issue a certificate. See how to point your domain to your VPS.
  • Ports 80 and 443 free. If Apache is installed, stop and disable it first.
  • A firewall that already allows SSH. See how to configure a firewall on your Linux VPS.

Install Nginx

sudo apt update
sudo apt install nginx
systemctl status nginx

Ubuntu 24.04 installs Nginx 1.24 from its own repository, which receives security fixes for the life of the release. Open HTTP and HTTPS in UFW using the profile Nginx ships with:

sudo ufw allow "Nginx Full"
sudo ufw status

Browse to http://YOUR_SERVER_IP and you should see the "Welcome to nginx!" page.

Note: If you need a newer mainline or stable release than Ubuntu ships, nginx.org runs its own Ubuntu repository with signed packages; follow the instructions at nginx.org. Those packages use /etc/nginx/conf.d/ instead of sites-available, so adjust the paths below.

Everyday service commands

sudo nginx -t                    # test the configuration
sudo systemctl reload nginx      # apply changes without downtime
sudo systemctl restart nginx     # full restart
sudo systemctl enable nginx      # start at boot (default on Ubuntu)

Make nginx -t a habit before every reload. If the test fails, the running server keeps the old configuration and nothing breaks.

How the configuration is organised

PathPurpose
/etc/nginx/nginx.confGlobal settings: worker processes, logging, gzip, includes
/etc/nginx/sites-available/One file per site
/etc/nginx/sites-enabled/Symlinks to the sites that are active
/etc/nginx/snippets/Reusable pieces, such as PHP settings
/var/www/Website files
/var/log/nginx/access.log and error.log

For each request, Nginx first picks a server block by matching the hostname against server_name, and then the best location block inside it by matching the path.

Create a server block for your domain

Create a directory for the site and a test page:

sudo mkdir -p /var/www/example.com/public
echo "<h1>example.com works</h1>" | sudo tee /var/www/example.com/public/index.html
sudo chown -R www-data:www-data /var/www/example.com

Create the configuration file /etc/nginx/sites-available/example.com:

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    root /var/www/example.com/public;
    index index.html index.php;

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

Enable it, remove the default site, test and reload:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx
curl -I http://example.com

Files should be readable by the www-data user: 644 for files and 755 for directories is a sound default.

Add PHP with PHP-FPM

Nginx does not run PHP itself; it passes PHP requests to PHP-FPM over a socket. Install php-fpm and the extensions you need by name. Avoid the bare php metapackage, which can pull in Apache.

sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip
systemctl status php8.3-fpm

On Ubuntu 24.04 that is PHP 8.3 with the socket at /run/php/php8.3-fpm.sock. Update the server block:

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

The /index.php?$args fallback lets applications such as WordPress handle pretty URLs. To test, create a file with <?php phpinfo(); in the web root, open it in the browser, and delete it straight afterwards, because it reveals a lot about your server. Running WordPress specifically is covered in how to install WordPress on a VPS.

Turn on HTTPS with Let's Encrypt

Certbot fetches a free certificate and edits the server block for you:

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

Choose to redirect HTTP to HTTPS when asked (newer Certbot versions do this by default). Renewal is automatic through a systemd timer; check it and run a dry run:

systemctl list-timers | grep certbot
sudo certbot renew --dry-run

Use Nginx as a reverse proxy

Many applications listen on a local port, for example a Node.js app or a Docker container on 127.0.0.1:3000. Nginx can sit in front, handle HTTPS and pass requests through:

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    listen [::]:80;
    server_name app.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

The X-Forwarded-* headers let the app see the visitor's real IP and protocol; the Upgrade headers make WebSockets work. Keep the app bound to 127.0.0.1 so Nginx is the only way in. With Docker this matters even more, because published container ports bypass UFW. See how to install Docker on an Ubuntu VPS.

Small hardening and tuning steps

Add these inside the http { } block of nginx.conf or in a file in /etc/nginx/conf.d/:

server_tokens off;            # hide the version number in headers and error pages
client_max_body_size 64m;     # default is 1m; raise it for uploads
gzip_types text/css application/javascript application/json image/svg+xml;

Ubuntu already enables gzip for HTML; gzip_types extends it to other text formats.

Redirect www and add a second domain

Most sites should answer on one hostname only. Once HTTPS works, you can send every request for www.example.com to example.com with a small extra server block, so search engines and visitors see one address:

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    return 301 https://example.com$request_uri;
}

Remove www.example.com from the server_name line of the main block when you add this, or Nginx will warn about a conflicting server name.

A second domain works exactly like the first: a new directory under /var/www, a new file in sites-available with its own server_name and root, a symlink in sites-enabled, nginx -t, reload and Certbot. Nginx keeps each site separate based on the hostname the browser sends. Give each site its own log files with access_log and error_log lines in its server block, which makes troubleshooting much easier once several sites share the server.

Common errors and how to fix them

SymptomLikely causeFix
502 Bad GatewayPHP-FPM or the proxied app is down, or the socket/port is wrongsystemctl status php8.3-fpm, check fastcgi_pass / proxy_pass
403 ForbiddenNo index file, or www-data cannot read the filesAdd index.html/index.php, fix ownership; never chmod 777
404 on every page except the front pagetry_files ends in =404End it with /index.php?$args
bind() to 0.0.0.0:80 failedAnother service holds port 80sudo ss -tlnp 'sport = :80', then stop Apache
nginx -t failsTypo, missing ;, missing certificate fileRead the file and line number in the error
Domain does not load at allDNS not pointing to the serverdig example.com +short

The answer is almost always in /var/log/nginx/error.log, and journalctl -u nginx shows problems with the service itself.

In short

Install Nginx with apt, open "Nginx Full" in UFW, create one server block per domain in sites-available and enable it with a symlink. Add PHP-FPM for PHP sites, Certbot for HTTPS and proxy_pass for applications. Run nginx -t before every reload and read error.log when something goes wrong.

Run Nginx on Hostigan

A Hostigan VPS runs on KVM with NVMe storage, IPv4 and IPv6 and full root access, so you can install Nginx from Ubuntu's repository or nginx.org exactly as shown. If you would rather not manage a web server at all, web hosting with cPanel takes care of it for you.

Frequently asked questions

Both are mature and fast enough for most sites. Nginx is light on memory and excellent as a reverse proxy, while Apache supports per-directory .htaccess files that some applications expect.

Next in “Your own web server on Linux” · 5 of 6How to install Docker on an Ubuntu VPS