Installing Nginx on Ubuntu
Installing Nginx on Ubuntu is a straightforward process that takes minutes from an empty server to a working web server. The official Ubuntu repositories carry a stable Nginx package, and the Nginx project maintains its own repository for the latest mainline or stable releases. This guide covers both paths, plus the post-install configuration steps that turn a default install into something you can actually deploy to.
More from this site
Keep reading the latest coverage
Before You Begin
You need a running Ubuntu server (22.04 LTS or 24.04 LTS are the current common targets), a user account with sudo privileges, and an open terminal. Make sure your package index is up to date before pulling in anything:
- sudo apt update
- sudo apt upgrade -y
Check whether Nginx is already present and which version: nginx -v. If that command returns a version string, you already have it and can skip ahead to configuration.
Installing Nginx from the Ubuntu Repository
The Ubuntu default repositories ship a well-tested Nginx version that receives security patches through unattended upgrades. This is the fastest route and ideal for most production workloads.
Installing the Mainline or Stable Nginx from the Official Repository
If you need a newer version than Ubuntu ships, or you want the Nginx mainline branch with the latest modules, add the Nginx signing key and repository:
- sudo curl -o /tmp/nginx_signing.key https://nginx.org/keys/nginx_signing.key
- sudo cp /tmp/nginx_signing.key /usr/share/keyrings/nginx-archive-keyring.gpg
- Add the repository with 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
- Run sudo apt update, then sudo apt install nginx.
This path gives you the upstream Nginx builds, but it bypasses Ubuntu's package manager integration for that specific package. Keep that in mind when auditing updates.
Managing the Service
Once installed, Nginx registers as a systemd service. The commands you will use most often are:
- sudo systemctl start nginx — start now
- sudo systemctl enable nginx — start on boot
- sudo systemctl restart nginx — full restart after config changes
- sudo systemctl reload nginx — apply config changes without dropping connections
Always test your configuration before reloading: sudo nginx -t. A failed syntax check will print the file and line number where the error occurred, and Nginx will refuse to reload until it is fixed.
Basic Configuration Layout
The default Nginx configuration on Ubuntu follows a modular structure:
- /etc/nginx/nginx.conf — global settings, worker processes, and include directives
- /etc/nginx/sites-available/ — per-site server blocks (default site lives here)
- /etc/nginx/sites-enabled/ — symlinks to active sites; Nginx reads everything in this directory
- /etc/nginx/conf.d/ — additional snippets, often used for load-balancer or proxy templates
- /var/www/html/ — default document root for the default server block
To add a new site, create a file in sites-available, then symlink it to sites-enabled with sudo ln -s. Remove the default site symlink if it conflicts with your configuration.
Opening the Firewall
If you are using UFW, which is common on Ubuntu, you need to allow HTTP and HTTPS traffic:
- sudo ufw allow 'Nginx Full'
- sudo ufw allow 'Nginx HTTP' (port 80 only)
- sudo ufw allow 'Nginx HTTPS' (port 443 only)
Verify the rule with sudo ufw status. If you are running a cloud provider's firewall or Security Groups, ensure those rules are also in place — UFW alone does not cover infrastructure-level access control.
Verifying the Install
The quickest verification is a local curl request: curl -I http://localhost. You should receive a 200 OK response with the Server: nginx header. If you get a connection refused error, check the service status, confirm the port is not bound by another process with sudo ss -tulnp | grep ':80', and review /var/log/nginx/error.log for startup failures.
Troubleshooting Common Issues
A few recurring problems when installing Nginx on Ubuntu:
- Port 80 already in use — Apache or another web server may be running. Stop it with sudo systemctl stop apache2 or reconfigure the conflicting service to a different port.
- Permission denied on logs — the www-data user needs write access to /var/log/nginx/. Check directory ownership with ls -ld /var/log/nginx.
- 403 Forbidden on a custom site — the document root either does not exist or lacks an index file, and directory indexing is disabled. Create an index.html or adjust the autoindex directive.
- Repository conflicts — mixing the Ubuntu package and the Nginx upstream repository can cause upgrade pinning issues. Stick to one source unless you have a specific reason to do otherwise.
After each change, run sudo nginx -t before reloading. That single habit prevents most downtime caused by bad configuration files.
Next Steps After Installation
With Nginx running, the immediate follow-ups are setting up a server block for your domain, obtaining an SSL certificate with Certbot (sudo apt install certbot python3-certbot-nginx), and configuring reverse proxy rules if you are serving an application backend. Each of those topics builds directly on the install you just completed, so confirming the default page works first is worth the few minutes it takes.