How to Configure Nginx on Red Hat Linux

How to Configure Nginx on Red Hat Linux

 Here’s a step-by-step guide to configure Nginx on Red Hat Linux. I’ll outline the steps clearly and provide detailed instructions. You can use these instructions to create a detailed post for your website.

How to Configure Nginx on Red Hat Linux

1. Install Nginx

Step 1: Update the System

Ensure your system is up-to-date:

sudo yum update -y

Step 2: Install Nginx

Install the EPEL repository, which contains Nginx:

sudo yum install nginx -y

Step 3: Start and Enable Nginx

Start Nginx and enable it to run at startup:

sudo systemctl start nginx sudo systemctl enable nginx

Step 4: Check Nginx Status

Verify that Nginx is running:

sudo systemctl status nginx

2. Configure the Firewall

Allow HTTP and HTTPS traffic through the firewall:

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

3. Test Nginx Installation

Step 1: Access the Default Page

Open a web browser and visit:

http://<your-server-ip>

You should see the default Nginx welcome page.

4. Configure Nginx Server Blocks (Virtual Hosts)

Step 1: Create a New Configuration File

Create a configuration file for your website:

sudo nano /etc/nginx/conf.d/yourdomain.conf

Step 2: Add the Server Block Configuration

Replace yourdomain.com with your domain name:

server { listen 80; server_name yourdomain.com www.yourdomain.com; root /var/www/yourdomain; index index.html; location / { try_files $uri $uri/ =404; } }

Step 3: Create the Web Root Directory

Create a directory to hold your website files:

sudo mkdir -p /var/www/yourdomain

Step 4: Set Permissions

Give ownership of the directory to the Nginx user:

sudo chown -R nginx:nginx /var/www/yourdomain sudo chmod -R 755 /var/www/yourdomain

Step 5: Add a Test HTML File

Create a sample index.html file:

echo "<h1>Welcome to YourDomain!</h1>" | sudo tee /var/www/yourdomain/index.html

Step 6: Test and Reload Nginx

Check the configuration for syntax errors:

sudo nginx -t

Reload Nginx to apply changes:

sudo systemctl reload nginx

5. Enable SSL for Your Site

Step 1: Install Certbot

Install Certbot and the Nginx plugin:

sudo yum install certbot python3-certbot-nginx -y

Step 2: Obtain an SSL Certificate

Run Certbot to obtain and configure SSL automatically:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Step 3: Test Auto-Renewal

Check Certbot's renewal process:

sudo certbot renew --dry-run

6. Set Up Automatic Nginx Service Restarts

Ensure Nginx restarts automatically in case of server reboots:

sudo systemctl enable nginx

7. Monitor Nginx Logs

Keep track of Nginx logs for debugging:

  • Access Log: /var/log/nginx/access.log
  • Error Log: /var/log/nginx/error.log

To view logs in real-time:

tail -f /var/log/nginx/access.log /var/log/nginx/error.log

Conclusion

Your Nginx server is now configured to host your website on Red Hat Linux. You’ve set up the firewall, server blocks, and SSL for secure access. Test thoroughly and deploy additional configurations as needed.

If you'd like, I can help you format this as a blog post for your website with proper headings, images, and explanations. Let me know!


Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close