How to Install Nginx on Ubuntu & Debian (Complete Guide)

Nginx is one of the world's most widely used web servers, known for its speed, low resource usage, and ability to handle thousands of concurrent connections. It's commonly used to serve websites directly, or as a reverse proxy in front of applications and Docker containers.

Prerequisites

  • Ubuntu 22.04/24.04 or Debian 11/12 VPS
  • Root or sudo access

Step 1 — Update the System

sudo apt update
sudo apt upgrade -y

Step 2 — Install Nginx

sudo apt install nginx -y

Step 3 — Enable and Start Nginx

sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx

Step 4 — Allow HTTP/HTTPS Through the Firewall

sudo ufw allow 'Nginx Full'
sudo ufw status

'Nginx Full' opens both port 80 (HTTP) and 443 (HTTPS) in one step.

Step 5 — Test in a Browser

http://YOUR_SERVER_IP

You should see the default "Welcome to nginx!" page.

Step 6 — Check the Version

nginx -v

Key File Locations

PathPurpose
/etc/nginx/nginx.confMain configuration file
/etc/nginx/sites-available/Virtual host definitions
/etc/nginx/sites-enabled/Symlinked active sites
/var/www/html/Default website root
/var/log/nginx/Access and error logs

Essential Nginx Commands

sudo systemctl restart nginx   # full restart
sudo systemctl reload nginx    # apply config changes without dropping connections
sudo nginx -t                  # test configuration syntax before reloading
sudo journalctl -u nginx       # view service logs

Always Test Before Reloading

sudo nginx -t && sudo systemctl reload nginx

This one-line habit prevents a typo in your config from taking down your entire web server.

Common Errors

Port 80 already in use — another web server (often Apache) is already running:

sudo ss -tulpn | grep :80

nginx: [emerg] ... test failed — a syntax error in your config; nginx -t reports the exact file and line.

Best Practices

  • Always run nginx -t before every reload
  • Keep Nginx updated with regular system updates
  • Enable HTTPS for every production site — see How to Install Let's Encrypt SSL with Certbot

FAQ

Is Nginx free?
Yes, Nginx Open Source is free; this guide covers that edition.

Can I run Nginx and Apache on the same VPS?
Yes, but not both listening on the same port — a common pattern is Nginx as a reverse proxy on port 80/443, forwarding to Apache on an internal port.

Related Articles

  • Nginx Virtual Hosts: Hosting Multiple Websites
  • How to Install Let's Encrypt SSL with Certbot (Nginx & Apache)
  • How to Fix Common Nginx Errors (502/504/403)
  • nginx, install nginx, web server, ubuntu, debian
  • 0 Корисниците го најдоа ова како корисно
Дали Ви помогна овој одговор?

Понудени резултати

How to Install Apache on Ubuntu & Debian (Complete Guide)

Apache HTTP Server is one of the most widely used web servers, valued for its stability, flexible...

Nginx Virtual Hosts (Server Blocks): Hosting Multiple Websites on One VPS

Nginx "server blocks" (the equivalent of Apache's virtual hosts) let you host multiple...

Apache Virtual Hosts: Hosting Multiple Websites on One VPS

Apache Virtual Hosts allow a single server to host multiple independent websites, each identified...

How to Configure Nginx as a Reverse Proxy for Node.js/Docker Apps

A reverse proxy sits in front of your application, handling incoming traffic on standard ports...

How to Install OpenLiteSpeed on a Linux VPS

OpenLiteSpeed is a high-performance, open-source web server known for excellent PHP performance...