How to Install and Configure Nginx on CentOS 7
Nginx is a powerful and high-performance web server used to serve static and dynamic content efficiently. This guide will walk you through installing and configuring Nginx on CentOS 7.
Step 1: Update the System
Before installing any new software, update your system packages to ensure you get the latest versions.
sudo yum update -y
Step 2: Install Nginx
Nginx is not included in the default CentOS repositories, so you need to enable the EPEL (Extra Packages for Enterprise Linux) repository first.
sudo yum install epel-release -y
Then, install Nginx:
sudo yum install nginx -y
Once installed, verify the installation:
nginx -v
Step 3: Start and Enable Nginx
After installation, start the Nginx service and enable it to start automatically on boot.
sudo systemctl start nginx
sudo systemctl enable nginx
To check if Nginx is running, use:
sudo systemctl status nginx
Step 4: Configure Firewall
By default, Nginx runs on port 80 (HTTP) and 443 (HTTPS). If your firewall is enabled, allow HTTP and HTTPS traffic.
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Step 5: Test Nginx
To verify that Nginx is working correctly, open a web browser and navigate to your server’s IP address:
http://your_server_ip
You should see the default Nginx welcome page.
Alternatively, use the command:
curl -I http://localhost
Step 6: Configure Nginx Server Blocks (Virtual Hosts)
Nginx uses server blocks (similar to Apache’s virtual hosts) to host multiple websites on a single server.
-
Create a new configuration file:
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/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
-
Create the website directory:
sudo mkdir -p /var/www/example.com/html
-
Set permissions:
sudo chown -R nginx:nginx /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com
-
Create a test HTML file:
echo "<h1>Welcome to example.com</h1>" | sudo tee /var/www/example.com/html/index.html
-
Restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 7: Enable SSL with Let’s Encrypt (Optional)
To secure your website with SSL/TLS, install Certbot and obtain a free SSL certificate:
sudo yum install certbot python2-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
Follow the prompts, and Certbot will configure SSL for you.
To auto-renew your SSL certificate, set up a cron job:
sudo crontab -e
Add this line:
0 3 * * * certbot renew --quiet
Step 8: Manage Nginx
Use the following commands to manage Nginx:
-
Restart Nginx:
sudo systemctl restart nginx
-
Stop Nginx:
sudo systemctl stop nginx
-
Start Nginx:
sudo systemctl start nginx
-
Reload configuration:
sudo systemctl reload nginx
Conclusion
You have successfully installed and configured Nginx on CentOS 7. You can now host websites, serve applications, and optimize your server for performance. If you have any questions, feel free to comment below!