How to install Nginx on Ubuntu

Install Nginx, serve your first site from its own folder, and learn the three commands you will use every time you change its configuration.

1–2 minutes
Inside a rack server with expansion cards

Nginx is a fast, widely used web server. This guide installs it on Ubuntu 24.04 and sets up one site in its own folder. The same steps work on Debian 12.

1. Install

sudo apt update
sudo apt install nginx

Nginx starts automatically. Check it:

systemctl status nginx

2. Open the firewall

If you use UFW (see firewall setup), allow web traffic:

sudo ufw allow 'Nginx Full'

Visit http://your-server-ip and you should see the Nginx welcome page.

3. Create a folder for your site

sudo mkdir -p /var/www/example.com/public
echo '<h1>It works</h1>' | sudo tee /var/www/example.com/public/index.html

4. Add a server block

Create /etc/nginx/sites-available/example.com:

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    root /var/www/example.com/public;
    index index.html index.php;

    location / {
        try_files $uri $uri/ =404;
    }
}

Enable it by linking it into sites-enabled:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

5. Test and reload

These are the commands you will use after every configuration change:

sudo nginx -t
sudo systemctl reload nginx

nginx -t checks the configuration for mistakes. Only reload when it reports syntax is ok and test is successful. A reload applies the change without dropping visitors.

Once your domain points to the server, visit it to see your page.

Where things are

PathWhat it holds
/etc/nginx/nginx.confMain settings
/etc/nginx/sites-available/One file per site
/etc/nginx/sites-enabled/Links to the sites that are switched on
/var/log/nginx/access.logEvery request
/var/log/nginx/error.logProblems, the first place to look

Next

Add HTTPS with a free Let’s Encrypt certificate. To run PHP or an app behind Nginx, see reverse proxies.

Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.