Nginx Installation Guide: Step-by-Step for Beginners
Welcome to the world of high-performance web serving! Nginx (pronounced "engine-x") has become the de facto standard for serving static content, acting as a reverse proxy, load balancer, and HTTP cache. For beginners, getting the initial installation right is the crucial first step toward leveraging its speed and efficiency.
This guide provides clear, actionable, step-by-step instructions for installing Nginx on the most common Linux distributions (Debian/Ubuntu and CentOS/RHEL). We will also cover essential post-installation verification steps to ensure your web server is running correctly and ready to handle traffic.
Prerequisites
Before starting the installation process, ensure you have:
- A server or virtual machine running a supported Linux distribution (Ubuntu/Debian or CentOS/RHEL).
- A non-root user with
sudoprivileges configured for administrative tasks. - Basic familiarity with the Linux command line interface (CLI).
Installation on Debian/Ubuntu Systems
On Debian-based systems, Nginx is readily available in the default package repositories. We recommend updating the package list first to ensure you pull the latest available version.
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 Red Hat Enterprise Linux (RHEL) derivatives like CentOS or Fedora, Nginx is typically installed using the yum or dnf package manager. Since Nginx may not be in the default base repository, installing from the official EPEL (Extra Packages for Enterprise Linux) repository is often recommended for the latest stable version.
Step 1: Install EPEL Repository
First, install the EPEL repository, which contains many commonly used, high-quality packages not included in the standard repositories.
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: Allowing HTTP/HTTPS Traffic
A crucial step after installation is ensuring your firewall permits external connections to reach the web server. If you skip this, external users will not be able to see your site.
Using UFW (Uncomplicated Firewall - Common on Ubuntu/Debian)
Nginx registers specific profiles with UFW during installation. We recommend opening port 80 (HTTP) and optionally port 443 (HTTPS).
# 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 (Common on CentOS/RHEL)
Firewalld uses zones to manage rules. We generally add services to the public 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
Verification: Confirming Nginx is Operational
With the installation complete and the firewall configured, you can confirm that Nginx is serving content correctly.
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, which typically reads: "Welcome to nginx!"
Method 2: Command Line Check
Use the curl command to retrieve the default index page content directly from the server's localhost:
curl http://localhost
This command should return the HTML source code for the welcome page.
Essential Nginx Management Commands
Once installed, you will frequently use systemctl to manage the Nginx process. Here are the most common commands:
| 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 |
Best Practice Tip: When making changes to configuration files (like
/etc/nginx/nginx.conf), always test the syntax before reloading. Usesudo nginx -tto test for errors. If the test passes, then usesudo systemctl reload nginxto apply the changes gracefully.
Where to Go Next: Initial Configuration
Congratulations, Nginx is installed and running! The default configuration serves content from /var/www/html/index.nginx-debian.html (on Debian/Ubuntu) or /usr/share/nginx/html/index.html (on CentOS/RHEL).
Your next steps involve creating a Server Block (often called a Virtual Host) to define where your actual website files are located and how Nginx should handle requests for your domain. Configuration files for these blocks are usually stored in /etc/nginx/sites-available/ and symlinked to /etc/nginx/sites-enabled/.
This initial installation provides the foundation upon which you can build high-performance hosting environments, load balancing solutions, and secure proxy setups.