
How to Install Nginx on Ubuntu 24
Nginx is one of the most popular web servers and reverse proxies on the planet, powering over 30% of all websites. Whether you’re spinning up a new development environment, deploying a production application, or just want to learn the ropes of modern web infrastructure, knowing how to properly install and configure Nginx on Ubuntu 24 is an essential skill. This guide will walk you through the complete installation process, from basic setup to advanced configuration options, plus troubleshooting tips that’ll save you hours of debugging when things inevitably go sideways.
How Nginx Works and Why You Should Care
Before diving into the installation, let’s quickly cover what makes Nginx special. Unlike traditional web servers that spawn new processes or threads for each connection, Nginx uses an event-driven, asynchronous architecture that can handle thousands of concurrent connections with minimal resource overhead. This makes it incredibly efficient for serving static content, load balancing, and acting as a reverse proxy for application servers.
The core components you’ll be working with include:
- Master process: Manages worker processes and handles configuration reloads
- Worker processes: Handle actual client connections and requests
- Configuration files: Define server behavior, virtual hosts, and routing rules
- Modules: Extend functionality for SSL, compression, caching, and more
Step-by-Step Installation Guide
Ubuntu 24 comes with a relatively recent version of Nginx in its default repositories, but you might want the latest features and security patches. I’ll show you both the standard repository installation and how to use the official Nginx repository.
Method 1: Installing from Ubuntu Repositories
This is the quickest way to get Nginx running, though you might not get the absolute latest version:
sudo apt update
sudo apt install nginx -y
Check if Nginx is running:
sudo systemctl status nginx
Method 2: Installing from Official Nginx Repository
For the latest stable version with all the bells and whistles, use the official Nginx repository:
# Install prerequisites
sudo apt update
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y
# Import the official nginx signing key
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo gpg --dearmor -o /usr/share/keyrings/nginx-archive-keyring.gpg
# Add the nginx repository
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
# Set repository pinning
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx
# Update and install
sudo apt update
sudo apt install nginx -y
Enabling and Starting Nginx
Regardless of which method you used, make sure Nginx starts automatically on boot:
sudo systemctl enable nginx
sudo systemctl start nginx
Verify everything is working by checking the status and testing the default page:
sudo systemctl status nginx
curl http://localhost
You should see the “Welcome to nginx!” HTML response. If you’re on a remote server, you can also test by visiting the server’s IP address in your browser.
Configuration Essentials
Nginx configuration can seem intimidating at first, but once you understand the structure, it’s quite logical. The main configuration file lives at /etc/nginx/nginx.conf
, with site-specific configurations in /etc/nginx/sites-available/
.
Basic Configuration Structure
Here’s a minimal server block configuration for hosting a simple website:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Save this configuration to /etc/nginx/sites-available/your-site
, then enable it:
sudo ln -s /etc/nginx/sites-available/your-site /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Security Hardening
Always test your configuration before reloading with nginx -t
. Here are some essential security configurations to add to your server blocks:
# Hide nginx version
server_tokens off;
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
# Rate limiting
limit_req_zone $binary_remote_addr zone=login:10m rate=10r/m;
Real-World Use Cases and Examples
Let me show you some practical configurations you’ll actually use in production environments.
Reverse Proxy for Node.js Application
This is probably one of the most common setups you’ll encounter:
upstream nodejs_backend {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
server {
listen 80;
server_name myapp.com;
location / {
proxy_pass http://nodejs_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
SSL/TLS Configuration with Let’s Encrypt
For production sites, you’ll want HTTPS. Here’s how to set it up with Let’s Encrypt:
# Install certbot
sudo apt install certbot python3-certbot-nginx -y
# Get certificate
sudo certbot --nginx -d example.com -d www.example.com
# Auto-renewal (certbot usually sets this up automatically)
sudo crontab -e
# Add: 0 12 * * * /usr/bin/certbot renew --quiet
Performance Optimization
Out of the box, Nginx performs well, but you can squeeze out even more performance with these tweaks:
# In /etc/nginx/nginx.conf
worker_processes auto;
worker_connections 1024;
# Enable gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
# Browser caching for static assets
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
Comparison with Alternative Web Servers
Feature | Nginx | Apache | Caddy |
---|---|---|---|
Memory Usage | Very Low | High | Low |
Concurrent Connections | Excellent (10K+) | Limited | Good |
Configuration Complexity | Moderate | Complex | Simple |
Auto HTTPS | Manual | Manual | Built-in |
Module Ecosystem | Extensive | Massive | Growing |
Common Issues and Troubleshooting
Even with perfect documentation, things can go wrong. Here are the most common issues I’ve encountered and their solutions:
Permission Denied Errors
If you’re getting 403 Forbidden errors, check these common culprits:
# Check file permissions
ls -la /var/www/html/
# Fix ownership (replace 'www-data' with your web user)
sudo chown -R www-data:www-data /var/www/html/
# Fix permissions
sudo find /var/www/html/ -type d -exec chmod 755 {} \;
sudo find /var/www/html/ -type f -exec chmod 644 {} \;
Configuration Test Failures
Always test your configuration before reloading:
sudo nginx -t
Common syntax errors include missing semicolons, unmatched braces, and invalid directive contexts.
Port Already in Use
If Nginx won’t start because port 80 is already in use:
# Check what's using port 80
sudo netstat -tlnp | grep :80
sudo ss -tlnp | grep :80
# Kill the conflicting process or change Nginx port
# In your server block: listen 8080;
Logs Are Your Best Friend
When troubleshooting, always check the logs:
# Error logs
sudo tail -f /var/log/nginx/error.log
# Access logs
sudo tail -f /var/log/nginx/access.log
# Systemd logs
sudo journalctl -u nginx -f
Best Practices and Pro Tips
After years of working with Nginx in production environments, here are some practices that’ll save you headaches:
- Always use
nginx -t
before reloading configurations - Keep your configurations in version control
- Use descriptive server block names in sites-available
- Implement proper monitoring and log rotation
- Set up fail2ban to prevent brute force attacks
- Use separate log files for different sites
- Implement proper backup strategies for configurations
Performance Monitoring
Enable the Nginx status module to monitor performance:
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
You can then monitor key metrics like active connections, requests per second, and worker status.
Integration with MangoHost Infrastructure
If you’re running this on a VPS or dedicated server, you’ll want to consider additional configurations for production environments. This includes setting up proper firewall rules with ufw, configuring log rotation, and implementing monitoring solutions like Prometheus or New Relic.
The beauty of Nginx is that once you understand these fundamentals, you can scale from a simple static site to complex microservices architectures. The configuration syntax stays consistent, and the performance characteristics make it suitable for everything from hobby projects to enterprise applications handling millions of requests per day.
For more advanced configurations and modules, check out the official Nginx documentation, which is surprisingly well-written and includes plenty of real-world examples.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.