Last updated
How To Install Nginx on Ubuntu 24.04
Nginx is a fast web server and reverse proxy. Install it from Ubuntu's packages, open the firewall, check it works, and set up your first site with a server block.
Related guide: Install NGINX in 60 seconds (with HTTPS)
1. Install Nginx
Installs Nginx and starts it right away.
sudo apt update
sudo apt install nginx
2. Open the firewall
If UFW is on, allow web traffic. Nginx Full opens 80 and 443.
sudo ufw app list
sudo ufw allow 'Nginx Full'
3. Check it is running
Visit http://your_server_ip in a browser to see the Welcome to nginx page.
systemctl status nginx
curl -I http://localhost
4. Manage the service
Reload applies config changes without dropping connections; always test the config first.
| Command | What it does |
|---|---|
| sudo nginx -t | Test the configuration |
| sudo systemctl reload nginx | Apply config changes without downtime |
| sudo systemctl restart nginx | Full restart |
| sudo systemctl stop nginx | Stop Nginx |
| sudo systemctl enable nginx | Start at boot (default) |
5. Add a server block for your site
Create a folder for the site and a config file, enable it, test and reload.
sudo mkdir -p /var/www/example.com/html
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;
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
6. Important files
Where Nginx keeps things on Ubuntu.
| Path | What it is |
|---|---|
| /etc/nginx/nginx.conf | Main configuration |
| /etc/nginx/sites-available/ | Site configs (server blocks) |
| /etc/nginx/sites-enabled/ | Enabled sites (symlinks) |
| /var/www/html | Default web root |
| /var/log/nginx/access.log | Every request |
| /var/log/nginx/error.log | Errors |
FAQ
How do I add HTTPS?
Install Certbot and run sudo certbot –nginx -d example.com. See our Let's Encrypt guide for the full steps.
I see the default page instead of my site. Why?
Check server_name matches your domain, the site is linked into sites-enabled, and run sudo nginx -t then reload.