Installing Nginx on Ubuntu
Nginx (pronounced “engine-x”) is one of the most popular web servers in the world. A web server is a program that listens for requests from browsers and sends back web pages, images, or data. Nginx is also commonly used as a reverse proxy (a server that sits in front of your application and forwards incoming requests to it), a load balancer, and a cache. In this guide you will install Nginx on Ubuntu, start it as a background service, open the firewall, and confirm it actually works.
What you need before you start
This guide targets Ubuntu 22.04 LTS and 24.04 LTS (LTS means “Long Term Support” — versions that get security updates for years). You need:
- A user account that can run
sudo(the command that runs a single command as the administrator, “root”). - A terminal — either directly on the machine or over SSH (Secure Shell, an encrypted remote login).
- Nothing else already listening on port 80, the standard port for HTTP web traffic.
If a service like Apache is already installed and using port 80, Nginx will fail to start with an “address already in use” error. Stop the other server first with
sudo systemctl stop apache2before continuing.
Install Nginx with apt
apt is Ubuntu’s package manager — the tool that downloads and installs software from official, trusted repositories. Always refresh the package list first so you get the latest version, then install.
sudo apt update
sudo apt install nginx -y
The -y flag automatically answers “yes” to the confirmation prompt. The nginx package is the stable, well-tested version maintained by Ubuntu.
Output:
Reading package lists... Done
Building dependency tree... Done
The following NEW packages will be installed:
nginx nginx-common nginx-core
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Setting up nginx (1.24.0-2ubuntu7) ...
Processing triggers for man-db (2.12.0-4build2) ...
On Ubuntu, the installer also starts Nginx automatically and sets it to launch on boot, so it may already be running. The next steps make sure of that.
When to use the apt version (and when not)
| Source | When to use | When not to |
|---|---|---|
Ubuntu apt repository | Almost always — simplest, auto-patched with security updates | When you need a feature only in the newest release |
| Official nginx.org repository | You need the very latest version or extra modules | If you are a beginner — it adds maintenance work |
| Compile from source | You need a custom-built module | Day-to-day server work — avoid unless required |
For most people, the apt version is the right choice. Stick with it.
Start and enable the service with systemd
systemd is the part of Ubuntu that manages background services (called “daemons”). You control it with systemctl. There are two separate ideas:
- start — turn the service on right now.
- enable — make it start automatically every time the server boots.
You almost always want both.
sudo systemctl start nginx
sudo systemctl enable nginx
Output:
Synchronizing state of nginx.service with SysV service script...
Executing: /usr/lib/systemd/systemd-sysv-install enable nginx
Check the service status
Confirm Nginx is running and healthy:
sudo systemctl status nginx
Output:
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Mon 2026-06-15 09:12:44 UTC; 2min 3s ago
Docs: man:nginx(8)
Main PID: 1432 (nginx)
Tasks: 3 (limit: 4613)
Memory: 4.1M
CPU: 38ms
CGroup: /system.slice/nginx.service
├─1432 "nginx: master process /usr/sbin/nginx"
├─1433 "nginx: worker process"
└─1434 "nginx: worker process"
The two key words to look for are active (running) (it is up) and enabled (it will come back after a reboot). Press q to exit the status view.
Allow Nginx through the firewall
ufw (“Uncomplicated Firewall”) is Ubuntu’s simple firewall — a gatekeeper that decides which network ports are allowed. If ufw is active and you have not allowed web traffic, visitors cannot reach your site even though Nginx is running.
When you install Nginx it registers handy named profiles with ufw. List them:
sudo ufw app list
Output:
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
Here is what each profile opens:
| Profile | Ports opened | When to use |
|---|---|---|
| Nginx HTTP | 80 (HTTP) | Plain, unencrypted traffic only — testing or before TLS |
| Nginx HTTPS | 443 (HTTPS) | Encrypted traffic only, once you have a certificate |
| Nginx Full | 80 and 443 | The usual choice — covers both HTTP and HTTPS |
Allow Nginx Full so you are ready for both now and later:
sudo ufw allow 'Nginx Full'
sudo ufw reload
Output:
Rule added
Rule added (v6)
Firewall reloaded
Before you enable
ufwfor the first time on a remote server, make sure OpenSSH is allowed:sudo ufw allow OpenSSH. If you forget, the firewall can lock you out of your own SSH session.
Check the active rules:
sudo ufw status
Output:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx Full ALLOW Anywhere
Nginx Full (v6) ALLOW Anywhere
Verify it actually works
Now confirm the server is serving pages. The fastest test is curl, a command-line tool that fetches a URL — no browser needed. localhost means “this same machine.”
curl -I http://localhost
The -I flag asks only for the response headers (the metadata, not the full page body).
Output:
HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Date: Mon, 15 Jun 2026 09:15:10 GMT
Content-Type: text/html
Content-Length: 615
Connection: keep-alive
HTTP/1.1 200 OK is the success signal — 200 means “everything worked.” You should also see Server: nginx, confirming Nginx answered.
To see the default page in a browser, open http://YOUR_SERVER_IP (find your IP with hostname -I). You will see the “Welcome to nginx!” page. That default page lives at /var/www/html/index.nginx-debian.html, and the configuration that serves it is in /etc/nginx/sites-available/default.
If something looks wrong, two log files tell you why. Logs are text files where programs record what happened.
sudo tail -n 20 /var/log/nginx/error.log
sudo tail -n 20 /var/log/nginx/access.log
error.log records problems; access.log records every request that comes in.
Best practices
- Run
sudo apt update && sudo apt upgraderegularly so Nginx receives security patches. - Always
enablethe service, not juststartit, so the site survives reboots. - Use the
Nginx Fullprofile inufwso you do not have to reopen the firewall when you add HTTPS later. - Test your config with
sudo nginx -tbefore every reload — it catches syntax errors before they take the site down. - Reload, do not restart, after config changes:
sudo systemctl reload nginxapplies changes with zero downtime. - Keep an eye on
/var/log/nginx/error.log— it is the first place to look when a site misbehaves.