How-to

Install nginx on Ubuntu 22.04 LTS

On Ubuntu 22.04 one apt command gives you a working nginx. The only real decision is whether the archive version is new enough or you want the upstream repository.

Which nginx version should I install on 22.04?

Ubuntu 22.04 ships nginx 1.18 in its main archive, patched for security by Ubuntu for the life of the release. It is stable and fine for serving sites and proxying apps. The nginx project publishes its own packages with a newer stable branch and a mainline branch, which bring HTTP/3, newer TLS options and features added since 1.18. Use the archive package unless you need one of those; it gets updates through normal apt upgrade and fits the Ubuntu layout with sites-available and sites-enabled.

SourceVersion lineConfig layoutUpdates via
Ubuntu archive1.18 with backported fixessites-available, sites-enabledUbuntu security updates
nginx.org stablecurrent stable branchconf.d/*.confnginx.org repository
nginx.org mainlinenewest featuresconf.d/*.confnginx.org repository

How do I install the Ubuntu package?

sudo apt update
sudo apt install -y nginx
systemctl status nginx --no-pager
curl -sI http://127.0.0.1 | head -1

The package starts nginx and enables it at boot. The last line should print HTTP/1.1 200 OK from the default welcome page. If you have not set up a non-root admin account yet, do that first with The adduser command and work through sudo.

How do I install from the nginx.org repository instead?

sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo gpg --dearmor -o /usr/share/keyrings/nginx-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
printf 'Package: *\nPin: origin nginx.org\nPin-Priority: 900\n' | sudo tee /etc/apt/preferences.d/99nginx
sudo apt update && sudo apt install -y nginx
sudo systemctl start nginx; sudo systemctl enable nginx

Swap packages for packages/mainline in the URL for the mainline branch. The pin makes apt prefer nginx.org builds over the Ubuntu one. This package does not start on install and uses /etc/nginx/conf.d/ for sites, so put server blocks there as .conf files.

How do I open the firewall?

If you use ufw, the Ubuntu package registers application profiles:

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status verbose

Allow SSH before enabling ufw, or the enable step cuts your session. Nginx Full opens 80 and 443. The upstream package has no ufw profile, so use sudo ufw allow 80,443/tcp there.

How do I add my own site?

Create a document root and a server block, then enable it with a symlink:

sudo mkdir -p /srv/www/shop.example.net/public
sudo chown -R deploy:www-data /srv/www/shop.example.net
sudo tee /etc/nginx/sites-available/shop.example.net >/dev/null <<'EOF'
server {
    listen 80;
    server_name shop.example.net;
    root /srv/www/shop.example.net/public;
    index index.php index.html index.htm;
    access_log /var/log/nginx/shop-example-net.log combined;
    location / { try_files $uri $uri/index.html =404; }
}
EOF
cd /etc/nginx/sites-enabled && sudo ln -s ../sites-available/shop.example.net .
sudo nginx -t && sudo systemctl reload nginx

Point the domain's A record at your machine's public IPv4 first. nginx -t checks the syntax and refuses to continue on an error, so a typo never takes a live server down. Remove the default site with sudo rm /etc/nginx/sites-enabled/default once yours works. Then add HTTPS with Let's Encrypt with nginx.

How do I size nginx for the hardware?

The defaults are already sensible: worker_processes auto; starts one worker per CPU thread, and each worker handles thousands of connections. On a 6 core box like S1 that is 12 workers. Two settings are worth raising on busy sites:

# /etc/nginx/nginx.conf, main context and events block
worker_rlimit_nofile 65535;
events { worker_connections 4096; multi_accept on; }

Static files are rarely the bottleneck; PHP, Python or Node behind nginx usually are. Give the app enough cores and RAM and let nginx handle the connections in front. On machines with a 10 Gbps port, a single nginx instance can serve cached files far faster than most sites ever need.

Where are the logs and config files?

Main config is /etc/nginx/nginx.conf, site files are in sites-available (archive package) or conf.d (upstream), and logs go to /var/log/nginx/, rotated daily by logrotate. sudo nginx -T prints the full merged configuration, which is the quickest way to see which file a directive really comes from.

Ubuntu 22.04 and 24.04 are both free images on every machine. For a new build, 24.04 gives nginx 1.24 from the archive and support until 2029; 22.04 runs until 2027. To control the service day to day see Start, stop and reload nginx.

Machines That Make Good nginx Web Servers

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E2Intel Xeon E3-1270 v34 / 832 GB DDR31x 480 GB SSD2x 1 Gbps$38.32$114.96 (3 months)
S1Intel Xeon E-2276G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$53.32$53.32 (1 month)
S2Intel Xeon E-2356G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$58.39$58.39 (1 month)
S6Intel Xeon E-2288G8 / 1664 GB DDR42x 1 TB NVMe2x 10 Gbps$89.92$89.92 (1 month)

Keep Reading

Let's Encrypt with nginx

Get free TLS certificates for nginx with certbot, choose the nginx or webroot method, and confirm automatic renewal works.

Open

Start, stop and reload nginx

When to reload and when to restart nginx, the systemctl and nginx -s commands, and how to recover when it will not start.

Open

Ubuntu

Ubuntu 24.04 or 22.04 LTS on a whole machine: the default pick when you want the biggest pool of packages and how-tos.

Open

Web hosting with a control panel

Host client sites under a free hosting panel; one IPv4 serves every HTTPS site through SNI and NVMe keeps PHP pages fast.

Open

FAQ

Quick Answers

Version 1.18 from the main archive, with security fixes backported by Ubuntu. The nginx.org repository offers newer stable and mainline builds.

No. It reads server blocks from /etc/nginx/conf.d/*.conf. You can add an include for sites-enabled if you prefer that layout.

That package leaves the service stopped after install. Start it and enable it at boot with systemctl.

Leave worker_processes auto. It matches the number of CPU threads, which is right for nearly every server.

24.04 for a new build, since it has a newer nginx and a longer support window. 22.04 is a good match when your app is certified for it.