Install Nginx on Ubuntu
| Which version you get | |
| Install | |
| Start at boot | |
| Check that Nginx answers | |
| Hide the version | |
| Open the firewall with ufw | |
| Modules on 22.04 and 24.04 | |
| Next steps | |
| Remove Nginx |
Ubuntu installs Nginx from its own repository with apt; the service starts right away. The outputs come from fresh containers of Ubuntu 24.04 and 22.04, checked on 26 October 2026; the older outputs from Ubuntu 20.04 are marked as such. Ubuntu is based on Debian, so Install Nginx on Debian is much the same, and the other distributions are in Install Nginx on Linux.
Which version you get
Each Ubuntu release keeps its Nginx version and gets only fixes:
| Release | Package version |
|---|---|
| Ubuntu 24.04 LTS | 1.24.0-2ubuntu7.18 |
| Ubuntu 22.04 LTS | 1.18.0-6ubuntu14.21 |
| Ubuntu 20.04 LTS | 1.18.0-0ubuntu1 |
For newer versions use the packages from nginx.org, see Newer versions from nginx.org.
Install
Refresh the package lists and see which version apt would install:
sudo apt update
apt-cache policy nginx
nginx: Installed: (none) Candidate: 1.24.0-2ubuntu7.18
sudo apt install nginx
The last lines of the output on Ubuntu 24.04:
… Setting up nginx-common (1.24.0-2ubuntu7.18) ... Setting up nginx (1.24.0-2ubuntu7.18) ...
If Nginx is already there, apt says so and changes nothing. This is Ubuntu 20.04:
Reading package lists... Done Building dependency tree Reading state information... Done nginx is already the newest version (1.18.0-0ubuntu1). 0 upgraded, 0 newly installed, 0 to remove and 226 not upgraded.
Start at boot
The package starts Nginx and enables it at boot. Enabling it again does no harm and shows that systemd knows the service; the output is from Ubuntu 20.04:
sudo systemctl enable nginx
Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable nginx
The state: systemctl status nginx, look for active (running). More about the command in systemctl.
Check that Nginx answers
nginx -v
nginx version: nginx/1.24.0 (Ubuntu)
The headers of the start page, with curl:
curl -I http://localhost/
HTTP/1.1 200 OK Server: nginx/1.24.0 (Ubuntu) Date: Mon, 26 Oct 2026 09:17:26 GMT Content-Type: text/html Content-Length: 615 Last-Modified: Mon, 26 Oct 2026 09:17:05 GMT Connection: keep-alive ETag: "6adf1a91-267" Accept-Ranges: bytes
Hide the version
The Server header above tells everyone the exact version, which helps whoever looks for servers with a known bug. The setting is in nginx.conf, commented out:
grep -n server_tokens /etc/nginx/nginx.conf
21: # server_tokens off;
Remove the #, test and reload:
sudo sed -i 's/# server_tokens off;/server_tokens off;/' /etc/nginx/nginx.conf
sudo nginx -t
sudo systemctl reload nginx
The header is then just Server: nginx; Debian 13 ships with this setting on.
Open the firewall with ufw
Ubuntu comes with UFW, switched off. The Nginx package adds three profiles:
sudo ufw app list
Available applications: Nginx Full Nginx HTTP Nginx HTTPS
That list is from a container. On a server with an SSH server installed it also has OpenSSH. Nginx HTTP is port 80, Nginx HTTPS port 443, Nginx Full both:
sudo ufw app info 'Nginx Full'
Profile: Nginx Full Title: Web Server (Nginx, HTTP + HTTPS) Description: Small, but very powerful and efficient web server Ports: 80,443/tcp
Allow SSH first, so that switching ufw on does not cut your own connection, then the web ports, then switch it on:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
Modules on 22.04 and 24.04
The two LTS releases install different sets. Ubuntu 22.04 pulls in nginx-core and six module packages, and the modules are loaded from
ls /etc/nginx/modules-enabled/
50-mod-http-geoip2.conf 50-mod-http-image-filter.conf 50-mod-http-xslt-filter.conf 50-mod-mail.conf 50-mod-stream.conf 70-mod-stream-geoip2.conf
Ubuntu 24.04 installs only nginx and nginx-common, and the folder is empty. A module you need, for example the stream module for TCP proxying, is a package of its own: sudo apt install libnginx-mod-stream.
Next steps
The configuration, the default site in /var/www/html and your own sites in sites-available and sites-enabled are explained in nginx.conf: configuration and sites.
Remove Nginx
remove keeps the configuration:
sudo apt remove nginx
purge deletes it as well:
sudo apt purge nginx nginx-common
On Ubuntu 22.04 add nginx-core and the libnginx-mod packages to the command, or run sudo apt autoremove afterwards.
Article authors: Andrei Olegovich, Klavdiya Kondakova
| Install Nginx | |
| Install Nginx on Linux | |
| Install Nginx on Debian | |
| nginx.conf: configuration and sites | |
| Ubuntu |