What is Nginx? #
Nginx (pronounced “engine-x”) is a high-performance, open-source web server and reverse proxy server that is popular for its speed and scalability.
Nginx is often used for serving static content, load balancing, caching, and as a reverse proxy for handling incoming requests to backend servers.
Prerequisites #
- Linux Server (Ubuntu, Debian, CentOS, or similar).
- Root or sudo access.
- Firewall configuration (optional but recommended to open HTTP/HTTPS ports).
Step 1: Update the System #
Ensure that your server’s package index is up-to-date:
sudo apt update && sudo apt upgrade -y # For Debian/Ubuntu systems
sudo yum update -y # For CentOS/Red Hat systems
Step 2: Install Nginx #
On Ubuntu/Debian-based Systems #
Install Nginx from the default package repository:
sudo apt install nginx -y
On CentOS/RHEL-based Systems #
To install Nginx on CentOS, you may need to enable the EPEL repository first:
sudo yum install epel-release -y
sudo yum install nginx -y
Step 3: Start and Enable Nginx #
Once installed, start the Nginx service and set it to run on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
To check if Nginx is running, you can use:
sudo systemctl status nginx
Step 4: Configure the Firewall #
If your server has a firewall enabled, allow traffic on HTTP (port 80) and HTTPS (port 443) to make Nginx accessible.
On Ubuntu/Debian (UFW firewall) #
sudo ufw allow 'Nginx Full'
sudo ufw reload
On CentOS/RHEL (firewalld) #
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
Step 5: Test Nginx Installation #
To verify that Nginx is installed and running correctly, open your web browser and visit your server’s IP address:
http://your_server_ip
You should see the default Nginx welcome page, confirming that the installation is successful.
Step 6: Basic Nginx Configuration #
Nginx configuration files are located in /etc/nginx/
:
- Main Configuration File:
/etc/nginx/nginx.conf
- Site-Specific Configurations:
/etc/nginx/sites-available/
and/etc/nginx/sites-enabled/
(Debian-based systems) - Server Block Configuration:
/etc/nginx/conf.d/
(CentOS-based systems)
Editing the Main Configuration File #
To edit the main configuration file, open it with a text editor:
sudo vi /etc/nginx/nginx.conf
Key sections of nginx.conf
include:
- worker_processes: Defines the number of worker processes.
- http { … }: Main HTTP settings such as keepalive and GZIP compression.
You can adjust these settings based on your server’s resources and traffic requirements.
Step 7: Setting Up Server Blocks (Virtual Hosts) #
A server block allows Nginx to serve multiple websites from a single server by specifying site-specific configurations.
On Ubuntu/Debian-based Systems #
- Create a new configuration file in
/etc/nginx/sites-available/
:
sudo vi /etc/nginx/sites-available/yourdomain.com
2. Add basic server block configuration:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}
3. Link the configuration file to sites-enabled
:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
4. Create the document root directory and add a test file:
sudo mkdir -p /var/www/yourdomain.com/html
echo "Welcome to yourdomain.com" | sudo tee /var/www/yourdomain.com/html/index.html
5. Adjust permissions to allow Nginx to access the directory:
sudo chown -R www-data:www-data /var/www/yourdomain.com/html
6. Test the Nginx configuration for syntax errors:
sudo nginx -t
7. Reload Nginx to apply changes:
sudo systemctl reload nginx
On CentOS/RHEL-based Systems #
- Create a new configuration file in
/etc/nginx/conf.d/
:
sudo vi /etc/nginx/conf.d/yourdomain.com.conf
2. Add server block configuration (similar to above).
3. Create the document root, set permissions, test, and reload as done in previous steps.
Step 8: Enable SSL (HTTPS) #
Securing your site with SSL is recommended, and you can use Let’s Encrypt for free SSL certificates.
- Install Certbot (Let’s Encrypt client):
sudo apt install certbot python3-certbot-nginx -y # Ubuntu/Debian
sudo yum install certbot python3-certbot-nginx -y # CentOS/RHEL
2. Obtain and install the SSL certificate:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
3. Follow the prompts to complete the installation. Certbot will automatically update your Nginx configuration to use SSL.
4. Test SSL renewal with a dry run:
sudo certbot renew --dry-run
Step 9: Manage Nginx Service #
Basic commands for managing Nginx:
- Start Nginx:
sudo systemctl start nginx
- Stop Nginx:
sudo systemctl stop nginx
- Restart Nginx:
sudo systemctl restart nginx
- Reload Nginx:
sudo systemctl reload nginx
(use after configuration changes) - Check Nginx status:
sudo systemctl status nginx
Step 10: Monitor Nginx Logs #
Nginx logs are useful for troubleshooting and monitoring.
- Access logs:
/var/log/nginx/access.log
- Error logs:
/var/log/nginx/error.log
You can use tail -f
to monitor logs in real-time:
tail -f /var/log/nginx/access.log
Conclusion #
You’ve now installed and configured Nginx as a web server. You’ve set up server blocks, configured Nginx to serve web content, and added SSL with Let’s Encrypt. Nginx’s configuration flexibility and performance make it an excellent choice for serving static content, dynamic applications, and reverse proxy setups.