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
-
Update package lists
Open a terminal and run:
sudo apt updateThis refreshes the list of available packages from the repositories. It’s good practice before installing anything new.
-
Install Nginx
Run the installation command:
sudo apt install nginx -yThe
-yflag automatically confirms the prompt. Ubuntu’s default repositories include Nginx, so you don’t need to add a third-party repository. -
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 -
Check the service status
Verify that Nginx is running:
sudo systemctl status nginxYou should see
active (running)in the output. If it’s not running, check the troubleshooting section below. -
Adjust the firewall (if enabled)
If you use UFW (Uncomplicated Firewall), allow Nginx traffic. First, list the available application profiles:
sudo ufw app listYou’ll see profiles like
Nginx Full,Nginx HTTP, andNginx HTTPS. Allow both HTTP and HTTPS with:sudo ufw allow 'Nginx Full'Then enable UFW if it isn’t already:
sudo ufw enableIf UFW is already active, just reload it:
sudo ufw reload -
Verify the web server
Open a web browser and go to your server’s IP address (or
http://localhostif 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 showLook for the address under
eth0orens3(usually starts with192.168.or10.). If you’re on a cloud server, use the public IP provided by your hosting provider. -
Test the configuration (optional but recommended)
After making any changes to Nginx config files, test them with:
sudo nginx -tIf the syntax is correct, you’ll see
syntax is okandtest is successful. -
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 Fullor at leastNginx HTTP. - Using the wrong IP address: If you’re testing from the same machine, use
localhostor127.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.confor a site file can cause Nginx to fail to restart. Always runsudo nginx -tbefore restarting.
Tips
- Use the default site as a test: The default configuration in
/etc/nginx/sites-available/defaultis 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 withsudo chown -R www-data:www-data /var/www/html. - Default page shows, but your site doesn’t: Make sure your server block’s
rootdirective points to the correct directory and that the file exists. Test withsudo nginx -tand 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
- Gmail Gemini Privacy: Data Access & Limit Settings
- Fix Outlook Inbox Corruption with DISM in Windows
- How to Set Up Apple Pay on iPhone
- How to Choose a Refurbished iPhone With Years of Support
- Voice Dictation in Google Docs: How to Use It
- Turn Off Message Notifications on Mac