Back to Blog

How to Install Nginx on Ubuntu

How to Install Nginx on Ubuntu
Command of the Day
sudo nginx -t

What it does: Tests the syntax and integrity of all Nginx configuration files without interrupting or reloading active web server processes.

Nginx is one of the most popular and efficient web servers in the world. Its lightweight resource usage and unmatched static content delivery make it the top choice for developers hosting on Linux VPS servers.

In this beginner-friendly tutorial, you will learn how to install and configure Nginx on Ubuntu 22.04 or Ubuntu 24.04 LTS on your RivinCloud VPS.


1. Step 1: Update System Repositories

Before installing any new software, ensure your local package index is up to date:

sudo apt update && sudo apt upgrade -y

2. Step 2: Install Nginx via APT

Install the Nginx package from the official Ubuntu repositories:

sudo apt install nginx -y

Once installation finishes, Ubuntu automatically starts the Nginx service and enables it to launch on system boot. Verify its status:

systemctl status nginx

(You should see Active: active (running) in green).


3. Step 3: Adjust the Firewall (UFW)

Before external visitors can access your web server, you must allow HTTP and HTTPS traffic through the UFW firewall:

# View available application profiles
sudo ufw app list

# Allow both Port 80 (HTTP) and Port 443 (HTTPS)
sudo ufw allow 'Nginx Full'

# Verify firewall status
sudo ufw status

4. Step 4: Verify Nginx in Your Web Browser

Open your web browser and navigate to your server's public IP address:

http://your_server_ip

You should see the default "Welcome to nginx!" landing page, confirming that the web server is successfully installed and accepting network connections.


5. Step 5: Managing the Nginx Process

Get comfortable with the core systemd commands used to control Nginx:

  • Check status: sudo systemctl status nginx
  • Stop Nginx: sudo systemctl stop nginx
  • Start Nginx: sudo systemctl start nginx
  • Restart Nginx (Full reboot): sudo systemctl restart nginx
  • Reload Nginx (Zero-downtime config refresh): sudo systemctl reload nginx
  • Disable auto-start on boot: sudo systemctl disable nginx
  • Enable auto-start on boot: sudo systemctl enable nginx

6. Important Nginx Files and Directories

Familiarize yourself with where Nginx stores its content and settings:

  • Web Content: /var/www/html contains the default web files served to visitors.
  • Server Configuration: /etc/nginx is the root configuration directory.
  • Main Config File: /etc/nginx/nginx.conf contains global settings (worker processes, gzip, logging formats).
  • Available Sites: /etc/nginx/sites-available/ is where you create virtual host configuration files for individual domain names.
  • Enabled Sites: /etc/nginx/sites-enabled/ holds symlinks pointing to active configurations in sites-available/.
  • Access Logs: /var/log/nginx/access.log records every HTTP request received.
  • Error Logs: /var/log/nginx/error.log records server errors and configuration issues.

Common Mistakes

  • Forgetting sudo nginx -t before reloading: Always test your configuration syntax before restarting Nginx. A syntax error can take down all hosted websites on your server!
  • Not opening firewall ports: If the page times out, verify that UFW allows 'Nginx Full'.

Troubleshooting

  • "nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)": Another web server (such as Apache) is running on port 80. Stop it using sudo systemctl stop apache2 && sudo systemctl disable apache2.

Best Practices

  • Always keep /etc/nginx/nginx.conf clean and place domain-specific configurations into separate files in /etc/nginx/sites-available/.
  • Use sudo systemctl reload nginx instead of restart to prevent active connection drops.

Frequently Asked Questions

Which version of Nginx is installed from Ubuntu repositories?

Ubuntu provides the stable branch of Nginx. If you need the mainline version, you can add the official Nginx PPA (ppa:nginx/stable or ppa:nginx/development).


Conclusion

Installing Nginx on Ubuntu takes less than two minutes. With Nginx running on your RivinCloud VPS, you are now ready to set up custom domain virtual hosts and secure your sites with SSL!