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
| Path | Purpose |
|---|---|
/etc/nginx/nginx.conf | Main 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 -tbefore 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)
