Install Nginx on Ubuntu/Debian (Official Stable)
From installation to production-ready deployment.
Applies to: Ubuntu 20.04/22.04/24.04, Debian 11/12
Goal: Install the latest stable Nginx from the official repository and configure firewall rules.
1. Update System and Install Prerequisites
sudo apt update sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring
2. Add Official Nginx Repository
echo "deb http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/nginx.gpg > /dev/null
3. Install Nginx
sudo apt update sudo apt install -y nginx nginx -v
4. Start and Enable Nginx
sudo systemctl enable --now nginx sudo systemctl status nginx --no-pager
5. Configure Firewall (UFW)
sudo ufw allow 'Nginx Full' sudo ufw reload sudo ufw status
6. Basic Availability Check
curl -I http://127.0.0.1 sudo ss -tulpen | grep ':80'
Visit http://your-server-ip in browser. If you see the Nginx welcome page, installation succeeded.
7. Common Management Commands
sudo nginx -t sudo systemctl reload nginx sudo systemctl restart nginx sudo systemctl stop nginx
8. Important Paths
/etc/nginx/nginx.conf: Main configuration file/etc/nginx/sites-available/: Site configurations/etc/nginx/sites-enabled/: Enabled sites (symlinks)/var/www/html: Default web root/var/log/nginx/: Access and error logs