How to Install Nginx on Ubuntu 24.04: Step-by-Step Guide
Introduction to Nginx on Ubuntu 24.04
What is Nginx?
Nginx (pronounced “engine‑x”) is an open‑source web server, reverse proxy, and load balancer. It was created in 2004 to handle massive numbers of simultaneous connections while using minimal resources. Today it powers a large portion of the web, from small blogs to high‑traffic APIs.
Why Choose Nginx for Ubuntu 24.04?
Ubuntu 24.04 (Noble Numbat) ships with Nginx in its default APT repositories, making installation straightforward and fully integrated with systemd, AppArmor, and the new systemd-resolved DNS resolver. The combination offers a secure, high‑performance stack that works out of the box on the latest LTS release.
Prerequisites for Installation
System Requirements
- Ubuntu 24.04 installed on a physical server, VM, or cloud instance.
- At least 512 MiB of RAM (1 GiB recommended for production).
- Network connectivity for package downloads.
Updating Your Package Index
Before pulling any packages, refresh the local index so you get the newest versions.
sudo apt update
Root Access and Sudo Privileges
The commands that follow need administrative rights. Either log in as root or prefix each command with sudo. The examples below assume a regular user with sudo access.
Step-by-Step Nginx Installation Guide
Installing Nginx via APT Repository
The recommended method for production servers is the APT package. It respects Ubuntu’s directory layout, integrates with systemd, and works seamlessly with tools like Certbot.
sudo apt install nginx
If you need the very latest stable release, add the official Nginx PPA first:
sudo add-apt-repository ppa:nginx/stable
sudo apt update
sudo apt install nginx
Verifying the Installation Status
Check that the service started correctly and is listening on port 80.
sudo systemctl status nginx
sudo ss -tlnp | grep nginx
You should see something similar to:
LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=1234,fd=6))
Configuring the UFW Firewall for HTTP and HTTPS
Ubuntu’s default firewall, UFW, blocks all incoming traffic unless you open specific ports. Use the predefined application profiles that ship with the ufw package.
| Profile | Ports Opened |
|---|---|
| Nginx HTTP | 80/tcp |
| Nginx HTTPS | 443/tcp |
| Nginx Full | 80/tcp, 443/tcp |
sudo ufw allow 'Nginx Full'
sudo ufw enable # skip if already active
sudo ufw status verbose
After enabling the rules, re‑run curl -I http://localhost to confirm you receive a 200 OK response.
Managing the Nginx Process
Starting, Stopping, and Restarting Nginx
sudo systemctl start nginx # launch now
sudo systemctl stop nginx # halt service
sudo systemctl restart nginx # full restart
sudo systemctl reload nginx # reload config without dropping connections
Enabling Nginx to Start on Boot
sudo systemctl enable nginx
This creates the necessary symlinks so that systemd brings Nginx up automatically after a reboot.
Checking Nginx Service Logs
Ubuntu stores logs in /var/log/nginx. For real‑time inspection, use journalctl which reads the systemd journal.
sudo journalctl -u nginx -f
Basic Configuration and Best Practices
Understanding the Nginx File Structure
/etc/nginx/nginx.conf– main configuration file./etc/nginx/conf.d/– drop‑in snippets loaded byinclude./etc/nginx/sites-available/– place one file per website (server block)./etc/nginx/sites-enabled/– symlinks to enabled sites./var/www/html/– default document root./var/log/nginx/– access and error logs.
Setting Up Your First Server Block
Create a new file in sites-available. Replace example.com with your domain.
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
Enable the site and test the syntax:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Optimizing Nginx for Performance
- Turn off server tokens to hide version details:
server_tokens off;innginx.conf. - Enable caching for static assets:
expires 30d;inside alocationblock. - Adjust worker processes to match CPU cores:
worker_processes auto;. - Use
gzipcompression for text files. Add inside thehttpblock:gzip on; gzip_types text/plain text/css application/json application/javascript;
Common Installation Mistakes & Troubleshooting
Fixing “Address Already in Use” Errors
This usually means another service occupies port 80 or 443. Identify the culprit with:
sudo ss -tlnp | grep ':80\|:443'
Stop or reconfigure the conflicting service, then restart Nginx.
Resolving Permission Issues with Web Root
Web files must be readable by the www-data user. Apply proper ownership and permissions:
sudo chown -R www-data:www-data /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com/html
Testing Nginx Configuration Syntax
Always run the syntax check before reloading:
sudo nginx -t
If you see syntax is ok and test is successful, proceed with sudo systemctl reload nginx.
AppArmor Profile Adjustments
Ubuntu 24.04 enables AppArmor by default and applies a strict profile to Nginx. If you need to serve files from a custom directory, add a rule to the profile:
sudo aa-complain /etc/apparmor.d/usr.sbin.nginx
# or edit /etc/apparmor.d/local/usr.sbin.nginx and add:
/var/www/example.com/html/** r,
/var/www/example.com/html/** rwk,
sudo systemctl restart apparmor
Switching to complain mode helps debug permission denials without blocking traffic.
Snap vs. APT Installation
The Snap version runs in a confined sandbox, stores files under /var/snap/nginx, and may conflict with Certbot or custom modules. For production, stick with the APT package shown earlier.
Post‑Installation Hardening (OpenSSL 3.0)
Ubuntu 24.04 ships OpenSSL 3.0, which supports modern TLS settings. Edit the ssl.conf snippet (or create one) to enforce strong ciphers:
sudo nano /etc/nginx/conf.d/ssl.conf
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_ciphers 'TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256';
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1h;
Include this file in nginx.conf with include /etc/nginx/conf.d/*.conf;.
Log Rotation with systemd‑timers
Ubuntu 24.04 uses a systemd timer for logrotate. Verify it’s enabled:
systemctl list-timers | grep logrotate
If you need a custom rotation for a site’s logs, add a file under /etc/logrotate.d/:
sudo nano /etc/logrotate.d/example.com
/var/log/nginx/example.com.access.log /var/log/nginx/example.com.error.log {
weekly
missingok
rotate 12
compress
delaycompress
notifempty
create 640 www-data adm
sharedscripts
postrotate
systemctl reload nginx > /dev/null 2>/dev/null || true
endscript
}