Learn how to install and configure Nginx on AlmaLinux to set up a web server or reverse proxy and host your web content efficiently and securely.
Prerequisites
Before installing Nginx, ensure you have:
- AlmaLinux 8 or 9 installed
- Root or sudo access
- Basic knowledge of Linux commands
Step 1: Update the System
First, update your system packages:
sudo dnf update -y
Step 2: Install Nginx
Install Nginx using the DNF package manager:
sudo dnf install nginx -y
Step 3: Start and Enable Nginx
Start the Nginx service and enable it to start on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
Verify the service status:
sudo systemctl status nginx
Step 4: Configure 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
Step 5: Test Nginx
Open your web browser and navigate to your server’s IP address:
http://your_server_ip
You should see the default Nginx welcome page.
Step 6: Configure Nginx
Main Configuration File
The main Nginx configuration file is located at:
/etc/nginx/nginx.conf
Create a Server Block
Create a new server block configuration:
sudo nano /etc/nginx/conf.d/example.com.conf
Add the following configuration:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Create Document Root
Create the directory for your website:
sudo mkdir -p /var/www/example.com
sudo chown -R nginx:nginx /var/www/example.com
Create a Test Page
Create a simple HTML file:
sudo nano /var/www/example.com/index.html
Add the following content:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Example.com</title>
</head>
<body>
<h1>Success! Nginx is working on AlmaLinux!</h1>
</body>
</html>
Step 7: Test Configuration
Test the Nginx configuration:
sudo nginx -t
If the test is successful, reload Nginx:
sudo systemctl reload nginx
Step 8: Set Up SSL/TLS (Optional)
Install Certbot for Let’s Encrypt certificates:
sudo dnf install epel-release -y
sudo dnf install certbot python3-certbot-nginx -y
Obtain an SSL certificate:
sudo certbot --nginx -d example.com -d www.example.com
Step 9: Security Best Practices
Hide Nginx Version
Edit the main configuration file:
sudo nano /etc/nginx/nginx.conf
Add in the http
block:
server_tokens off;
Configure Security Headers
Add security headers to your server block:
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
Step 10: Performance Optimization
Enable Gzip Compression
Add to the http
block in nginx.conf:
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
Configure Worker Processes
Adjust worker processes based on CPU cores:
worker_processes auto;
worker_connections 1024;
Troubleshooting
Check Error Logs
sudo tail -f /var/log/nginx/error.log
Check Access Logs
sudo tail -f /var/log/nginx/access.log
Common Issues
- Permission Denied: Ensure proper ownership and SELinux contexts
- Port Already in Use: Check if another service is using port 80
- Configuration Errors: Always test with
nginx -t
before reloading
Conclusion
You have successfully installed and configured Nginx on AlmaLinux. Your web server is now ready to serve static content or act as a reverse proxy for your applications. Remember to keep Nginx updated and monitor your logs regularly for security and performance optimization.