Nginx Installation Guide: Step-by-Step for Beginners
Master the initial setup of your high-speed web server! This comprehensive guide walks beginners through the precise, step-by-step installation of Nginx on both Debian/Ubuntu and CentOS/RHEL systems. Learn essential commands for starting, stopping, and verifying the service, plus crucial firewall configuration to ensure immediate accessibility. Get your Nginx web server up and running in minutes.
Nginx Installation Guide: Step-by-Step for Beginners
You need Nginx installed, running, and reachable before you can use it as a web server, reverse proxy, load balancer, or cache. This Nginx installation guide walks through the basic package-manager setup on Debian/Ubuntu and RHEL-style Linux systems, then shows you how to verify the service and open the firewall.
The commands below assume a systemd-based server and a user with sudo access.
Prerequisites
Before starting, make sure you have:
- A server or virtual machine running Ubuntu, Debian, RHEL, CentOS Stream, Rocky Linux, AlmaLinux, or a similar distribution.
- A non-root user with
sudoprivileges. - Shell access over SSH or a local terminal.
Installation on Debian/Ubuntu Systems
On Debian-based systems, Nginx is available from the default repositories. Update the package index first so apt sees the current package metadata.
Step 1: Update Package Lists
Execute the following commands to update your local package index:
sudo apt update
Step 2: Install Nginx
Use the apt install command to download and install Nginx and its dependencies:
sudo apt install nginx
Step 3: Verify Installation Status
Once the installation completes, the service is usually started automatically. You can verify its status using systemctl:
sudo systemctl status nginx
You should see output indicating that the service is active (running).
Installation on CentOS/RHEL Systems
On RHEL-style systems, use dnf on current releases and yum on older ones. Some distributions include Nginx in their own repositories; others may need EPEL or an official vendor repository. If dnf install nginx cannot find the package, enable EPEL or check your distribution's Nginx packaging notes.
Step 1: Install EPEL Repository
If your system needs EPEL, install it first.
For CentOS/RHEL 7:
sudo yum install epel-release
For CentOS/RHEL 8/9 (using DNF):
sudo dnf install epel-release
Step 2: Install Nginx
Now, install Nginx using yum or dnf:
# Using yum (older systems)
sudo yum install nginx
# OR using dnf (newer systems)
sudo dnf install nginx
Step 3: Start and Enable Nginx Service
Unlike Debian/Ubuntu, Nginx might not start automatically after installation on RHEL-based systems. You must manually start it and enable it to run on system boot:
# Start the service immediately
sudo systemctl start nginx
# Ensure it starts on reboot
sudo systemctl enable nginx
# Check status
sudo systemctl status nginx
Firewall Configuration: Allow HTTP and HTTPS
After installation, make sure your firewall allows web traffic. If the service is running but port 80 or 443 is blocked, your browser check will fail from outside the server.
Using UFW on Ubuntu/Debian
Ubuntu's Nginx package usually registers UFW application profiles. Open both HTTP and HTTPS if you plan to add TLS soon.
# List available application profiles
sudo ufw app list
# Allow standard web traffic (HTTP/HTTPS)
sudo ufw allow 'Nginx Full'
# Reload firewall to apply changes
sudo ufw reload
Using Firewalld on RHEL-Style Systems
Firewalld uses zones to manage rules. These commands add the standard http and https services to the active default zone.
# Allow HTTP traffic (port 80)
sudo firewall-cmd --permanent --add-service=http
# Allow HTTPS traffic (port 443)
sudo firewall-cmd --permanent --add-service=https
# Reload the firewall rules
sudo firewall-cmd --reload
Verify Nginx Is Working
With the package installed and the firewall open, confirm that Nginx is serving the default page.
Method 1: Web Browser Check
Open your preferred web browser and navigate to your server's IP address or domain name:
http://YOUR_SERVER_IP_ADDRESS
If successful, you should see the default Nginx welcome page. The exact text and file path vary by distribution.
Method 2: Command Line Check
Use curl from the server itself to check the local listener:
curl http://localhost
This command should return HTML. If it fails locally, check systemctl status nginx and /var/log/nginx/error.log before troubleshooting DNS or cloud firewall rules.
Essential Nginx Management Commands
Once installed, you will usually manage Nginx with systemctl:
| Action | Command |
|---|---|
| Start the server | sudo systemctl start nginx |
| Stop the server | sudo systemctl stop nginx |
| Restart (Stop then Start) | sudo systemctl restart nginx |
| Reload (Apply config changes without downtime) | sudo systemctl reload nginx |
| Check Status | sudo systemctl status nginx |
| Disable auto-start on boot | sudo systemctl disable nginx |
Test configuration syntax before every reload:
sudo nginx -t
sudo systemctl reload nginx
That habit catches missing semicolons, bad include paths, and duplicate listen conflicts before they interrupt traffic.
Next Step: Create a Server Block
The default configuration serves a package-provided welcome page. On Debian/Ubuntu, web files commonly live under /var/www/html. On RHEL-style systems, the default root is often /usr/share/nginx/html.
Your next step is to create a server block for your domain. Debian/Ubuntu layouts often use /etc/nginx/sites-available/ with symlinks into /etc/nginx/sites-enabled/. RHEL-style layouts more often use files under /etc/nginx/conf.d/.
After that, add TLS, point your domain's DNS record at the server, and run sudo nginx -t before reloading. At that point your Nginx installation is ready for real site configuration.