Setup Nginx [Recommended] Print

  • 0

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:

  1. Update package lists
    sudo apt update
  2. Install Nginx
    sudo apt install nginx -y
  3. Enable and start the Nginx service
    sudo systemctl enable nginx
    sudo systemctl start nginx
  4. Verify Nginx is running
    sudo systemctl status nginx
  5. Test in browser
    Visit your server's IP address. You should see the Nginx welcome page.

For CentOS/RHEL/AlmaLinux/Rocky Linux:

  1. Install EPEL repository
    sudo yum install epel-release -y
  2. Install Nginx
    sudo yum install nginx -y
  3. Enable and start the Nginx service
    sudo systemctl enable nginx
    sudo systemctl start nginx
  4. 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


Was this answer helpful?

« Back