sodcalculator

10 Tech How-To

How to Install Nginx on Ubuntu 20.04 LTS

Guide 10-20647 · Updated · Step by step

This guide walks you through installing Nginx on Ubuntu 20.04 LTS, a popular open-source web server used to host websites and applications. You’ll start with a fresh installation, configure the firewall, and verify that the server is running. By the end, you’ll have a working Nginx server ready to serve your content, with steps that are safe to follow on a production system.

What You’ll Need

  • An Ubuntu 20.04 LTS server or desktop with sudo access.
  • An active internet connection to download packages.
  • Optional: a domain name pointing to your server’s IP address if you plan to host a public site.

Step-by-Step

  1. Update package lists

    Open a terminal and run:

    sudo apt update

    This refreshes the list of available packages from the repositories. It’s good practice before installing anything new.

  2. Install Nginx

    Run the installation command:

    sudo apt install nginx -y

    The -y flag automatically confirms the prompt. Ubuntu’s default repositories include Nginx, so you don’t need to add a third-party repository.

  3. Start and enable Nginx

    After installation, Nginx may not start automatically. Start it and enable it to launch on boot:

    sudo systemctl start nginx
    sudo systemctl enable nginx
  4. Check the service status

    Verify that Nginx is running:

    sudo systemctl status nginx

    You should see active (running) in the output. If it’s not running, check the troubleshooting section below.

  5. Adjust the firewall (if enabled)

    If you use UFW (Uncomplicated Firewall), allow Nginx traffic. First, list the available application profiles:

    sudo ufw app list

    You’ll see profiles like Nginx Full, Nginx HTTP, and Nginx HTTPS. Allow both HTTP and HTTPS with:

    sudo ufw allow 'Nginx Full'

    Then enable UFW if it isn’t already:

    sudo ufw enable

    If UFW is already active, just reload it:

    sudo ufw reload
  6. Verify the web server

    Open a web browser and go to your server’s IP address (or http://localhost if you’re on the machine itself). You should see the default Nginx welcome page, which confirms the installation worked.

    To find your server’s IP address, run:

    ip addr show

    Look for the address under eth0 or ens3 (usually starts with 192.168. or 10.). If you’re on a cloud server, use the public IP provided by your hosting provider.

  7. Test the configuration (optional but recommended)

    After making any changes to Nginx config files, test them with:

    sudo nginx -t

    If the syntax is correct, you’ll see syntax is ok and test is successful.

  8. Restart Nginx to apply changes

    Whenever you edit configuration files, restart the service:

    sudo systemctl restart nginx

Common Mistakes

  • Forgetting to open the firewall: Nginx can be installed but unreachable if UFW blocks port 80. Always allow Nginx Full or at least Nginx HTTP.
  • Using the wrong IP address: If you’re testing from the same machine, use localhost or 127.0.0.1. From another computer, use the server’s actual IP.
  • Not enabling the service: If you restart the server and Nginx doesn’t come back, you probably didn’t run sudo systemctl enable nginx.
  • Editing config files without testing: A typo in nginx.conf or a site file can cause Nginx to fail to restart. Always run sudo nginx -t before restarting.

Tips

  • Use the default site as a test: The default configuration in /etc/nginx/sites-available/default is a good starting point. Copy it to create your own site rather than editing it directly.
  • Keep logs handy: Nginx logs are in /var/log/nginx/. The access log and error log help diagnose issues.
  • Set up a server block early: Even if you only have one site, create a dedicated server block in /etc/nginx/sites-available/ and symlink it to /etc/nginx/sites-enabled/. This keeps things organized.
  • Use HTTPS from the start: Consider installing Certbot to get a free SSL certificate after Nginx is running. It’s easier to set up HTTPS before you have lots of traffic.

Troubleshooting

  • Nginx won’t start: Check the error log: sudo tail -f /var/log/nginx/error.log. Common causes are port 80 already in use (e.g., Apache) or a syntax error in a config file. Stop the conflicting service or fix the config.
  • Page not loading from another device: Verify the firewall allows port 80: sudo ufw status. Also confirm the server’s IP is correct and that the device is on the same network (or the port is forwarded).
  • Nginx starts but then stops: Check the systemd journal: sudo journalctl -u nginx. This often shows a configuration error or a missing document root.
  • Permission denied errors: If your web root files are owned by another user, Nginx (running as www-data) may not read them. Fix with sudo chown -R www-data:www-data /var/www/html.
  • Default page shows, but your site doesn’t: Make sure your server block’s root directive points to the correct directory and that the file exists. Test with sudo nginx -t and restart.

Frequently Asked Questions

How do I uninstall Nginx?

Run sudo apt remove nginx to remove the package but keep config files. Use sudo apt purge nginx to remove everything including configs.

Can I install Nginx on Ubuntu 20.04 using Docker?

Yes, but this guide covers the native installation. With Docker, you’d pull the nginx image and run a container, which is a different setup.

How do I change the default web root?

Edit the server block file (e.g., /etc/nginx/sites-available/default) and change the root directive to your desired directory. Then test and reload Nginx.

Does Nginx work with PHP?

Yes, but you need to install PHP-FPM and configure Nginx to pass requests to it. This is common for WordPress and other PHP apps.

How do I set up multiple websites on one server?

Create separate server block files in /etc/nginx/sites-available/, each with its own server_name and root. Enable them by symlinking to /etc/nginx/sites-enabled/ and reload Nginx.

Why is my Nginx version older than the latest?

Ubuntu’s repositories may not have the newest release. To get the latest stable version, you can add the official Nginx repository, but the default version is usually sufficient for most uses.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →