Nginx is a high-performance web server and reverse proxy used by millions of websites worldwide. This guide will walk you through installing Nginx on a Linux-based server using common distributions such as CentOS, Ubuntu, or Debian.
Requirements
- Root or sudo access to the server
- A Linux distribution (Ubuntu, Debian, CentOS, AlmaLinux, Rocky Linux, etc.)
- Internet connection
Installation Steps
For Ubuntu/Debian:
- Update package lists
sudo apt update - Install Nginx
sudo apt install nginx -y - Enable and start the Nginx service
sudo systemctl enable nginx
sudo systemctl start nginx - Verify Nginx is running
sudo systemctl status nginx - Test in browser
Visit your server's IP address. You should see the Nginx welcome page.
For CentOS/RHEL/AlmaLinux/Rocky Linux:
- Install EPEL repository
sudo yum install epel-release -y - Install Nginx
sudo yum install nginx -y - Enable and start the Nginx service
sudo systemctl enable nginx
sudo systemctl start nginx - Check Nginx status
sudo systemctl status nginx
Common Nginx Commands
- Restart Nginx:
sudo systemctl restart nginx - Reload configuration without downtime:
sudo nginx -s reload - Test Nginx configuration:
sudo nginx -t
Default File Locations
- Configuration file:
/etc/nginx/nginx.conf - Server blocks (Ubuntu/Debian):
/etc/nginx/sites-available/and/etc/nginx/sites-enabled/ - Web root directory:
/var/www/html
Opening Firewall (Optional)
If using UFW (Ubuntu):sudo ufw allow 'Nginx Full'
If using firewalld (CentOS/RHEL):sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload