Install and Configure Nginx on CentOS
This guide provides a streamlined method to install Nginx on CentOS and set up a basic web server for production environments.
1. Update System Packages
sudo yum update -y
Ensure all existing packages are up-to-date before proceeding.
2. Install Nginx via EPEL Repository
sudo yum install epel-release -y
sudo yum install nginx -y
Enable the EPEL repository to access the latest Nginx package.
3. Configure Firewall Rules
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Allow HTTP/HTTPS traffic through the system firewall.
4. Start and Enable Nginx Service
sudo systemctl start nginx
sudo systemctl enable nginx
Activate Nginx and configure automatic startup on system boot.
5. Verify Installation
curl -I 127.0.0.1
Check for a 200 OK HTTP status code to confirm successful installation.
6. Nginx Configuration Basics
Main configuration file location:
/etc/nginx/nginx.conf
Test configuration changes before applying:
sudo nginx -t
Reload Nginx after modifying configurations:
sudo systemctl reload nginx
Key Considerations
- Default web root directory:
/usr/share/nginx/html
- Access logs:
/var/log/nginx/access.log
- Error logs:
/var/log/nginx/error.log
- SSL certificate path:
/etc/nginx/ssl/