Nginx (pronounced "engine-ex") is the world’s most popular open-source, high-performance web server and reverse proxy. Known for its asynchronous, event-driven architecture, Nginx effortlessly handles thousands of concurrent connections with minimal memory footprint, making it the ideal web server for any Linux Virtual Private Server (VPS).

Whether you are deploying on Debian / Ubuntu (Ubuntu 26.04, 24.04, 22.04, Debian 12/11) or enterprise RHEL-based distributions such as AlmaLinux, Rocky Linux, RHEL, or CentOS Stream, this comprehensive tutorial will guide you through installing, configuring, and securing Nginx across all major Linux distributions.


Prerequisites


Step 1: Install Nginx on Your Linux Distribution

Choose the command corresponding to your Linux distribution:

Option A: Ubuntu & Debian (APT Package Manager)

# Update package lists
sudo apt update

# Install Nginx
sudo apt install nginx -y

# Enable and start Nginx service
sudo systemctl enable nginx
sudo systemctl start nginx

Option B: AlmaLinux, Rocky Linux & RHEL (DNF / YUM Package Manager)

# Update package lists and install EPEL repository (if required)
sudo dnf update -y
sudo dnf install epel-release -y

# Install Nginx
sudo dnf install nginx -y

# Enable and start Nginx service
sudo systemctl enable nginx
sudo systemctl start nginx

Step 2: Adjust Firewall Rules

Ensure your firewall allows web traffic to reach your web server:

For Ubuntu & Debian (UFW Firewall):

sudo ufw allow 'Nginx Full'
# OR explicitly:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

For AlmaLinux & Rocky Linux (Firewalld):

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Step 3: Verify Nginx is Running

Check the status of your Nginx system service:

sudo systemctl status nginx

You should see Active: active (running). Open your web browser and navigate to http://YOUR_SERVER_IP. You will see the default Nginx welcome landing page.


Step 4: Configure Virtual Hosts / Server Blocks for Your Domain

To host your actual website or application, create a custom server block configuration:

1. Create Website Root Directory:

sudo mkdir -p /var/www/domain.com/html
sudo chown -R $USER:$USER /var/www/domain.com/html
sudo chmod -R 755 /var/www/domain.com

2. Create a Sample Index HTML Page:

echo "<h1>Welcome to domain.com hosted on Aveshost Linux VPS!</h1>" > /var/www/domain.com/html/index.html

3. Create the Nginx Server Block:

  • On Ubuntu & Debian:
    sudo nano /etc/nginx/sites-available/domain.com
  • On AlmaLinux & Rocky Linux:
    sudo nano /etc/nginx/conf.d/domain.com.conf

Paste the following server block configuration (replace domain.com with your actual domain):

server {
    listen 80;
    listen [::]:80;

    server_name domain.com www.domain.com;
    root /var/www/domain.com/html;
    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    access_log /var/log/nginx/domain.com.access.log;
    error_log /var/log/nginx/domain.com.error.log;
}

4. Enable the Site (Ubuntu & Debian only):

sudo ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/

5. Test Syntax and Reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

Step 5: SELinux Configuration (AlmaLinux, Rocky Linux & RHEL)

If you are using AlmaLinux or Rocky Linux with SELinux enabled in Enforcing mode, ensure Nginx has permission to read the web directory and connect to network services:

# Allow Nginx to read /var/www files
sudo chcon -Rt httpd_sys_content_t /var/www/domain.com

# Allow Nginx to connect to network sockets (for reverse proxying Node.js/Python)
sudo setsebool -P httpd_can_network_connect 1

Common Nginx Management Commands

Action Command
Test Configuration Syntax sudo nginx -t
Gracefully Reload Configuration sudo systemctl reload nginx
Restart Nginx Service sudo systemctl restart nginx
Check Service Status sudo systemctl status nginx

Frequently Asked Questions & Troubleshooting

Q: Why do I get a "403 Forbidden" error when visiting my domain?
A: This typically happens when directory permissions are too restrictive or SELinux is blocking Nginx. Ensure permissions are set with chmod -R 755 /var/www/domain.com and on RHEL/AlmaLinux run restorecon -Rv /var/www/domain.com.

Q: How do I secure Nginx with a free SSL certificate?
A: Follow our step-by-step tutorial on How to Install Let's Encrypt SSL on Linux VPS.


Need Further Assistance?

If you need assistance configuring Nginx server blocks, resolving reverse proxy errors, or setting up web applications on your Aveshost Linux VPS, our technical support team is available 24/7. Feel free to submit a support ticket for prompt assistance.

Bu cevap yeterince yardımcı oldu mu? 0 Bu dökümanı faydalı bulan kullanıcılar: (0 Oy)