Installing and Configuring Nginx on CentOS 7
Nginx is a high-performance web server and reverse proxy server that is widely used for serving static content, handling concurrent connections, and load balancing. This guide will walk you through the installation and configuration of Nginx on a CentOS 7 server.
1. Update Your System
Before installing Nginx, ensure your system is up to date. Open the terminal and run the following commands:
sudo yum update2. Install Nginx
To install Nginx, you can use the default package manager yum:
sudo yum install epel-releasesudo yum install nginxThis command installs Nginx and any required dependencies.
3. Start and Enable Nginx
After installation, start the Nginx service and enable it to start automatically on boot:
sudo systemctl start nginxsudo systemctl enable nginx4. Configure the Firewall
To allow web traffic to your server, you need to configure the firewall to allow HTTP and HTTPS traffic. Use the following commands to open the necessary ports:
sudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --permanent --add-service=httpssudo firewall-cmd --reload5. Verify the Installation
To verify that Nginx is installed and running, open your web browser and navigate to your server’s IP address:
http://your_server_ipYou should see the default Nginx welcome page, which indicates that the installation was successful.
6. Configuring Nginx
Nginx configuration files are located in /etc/nginx/. The main configuration file is nginx.conf, and server blocks (similar to virtual hosts in Apache) are defined in the conf.d directory.
Step 1: Create a New Server Block
To create a new server block for your website, create a new configuration file in the /etc/nginx/conf.d/ directory. For example, create a file named example.com.conf:
sudo nano /etc/nginx/conf.d/example.com.confAdd 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;
} }Replace example.com with your domain name and adjust the root directory to match where your website files will be located.
Step 2: Create the Document Root
Next, create the document root directory for your website:
sudo mkdir -p /var/www/example.com/htmlYou can also create an example index.html file to test:
echo "<h1>Welcome to Example.com!</h1>" | sudo tee /var/www/example.com/html/index.html7. Test Nginx Configuration
Before applying the changes, test the Nginx configuration for any syntax errors:
sudo nginx -tIf the output shows that the configuration is successful, proceed to restart Nginx to apply the changes:
sudo systemctl restart nginx8. Setting Up HTTPS with Let’s Encrypt (Optional)
To secure your website with SSL, you can use Let’s Encrypt to obtain a free SSL certificate. First, install Certbot:
sudo yum install certbot python2-certbot-nginxThen run Certbot to obtain and install the SSL certificate:
sudo certbot --nginx -d example.com -d www.example.comFollow the prompts to complete the installation. Certbot will automatically configure Nginx to use SSL.
9. Automatic Certificate Renewal
Let’s Encrypt certificates are valid for 90 days. To set up automatic renewal, add a cron job:
sudo crontab -eAdd the following line to check and renew certificates daily:
0 0 * * * /usr/bin/certbot renew --quiet10. Conclusion
You have successfully installed and configured Nginx on CentOS 7, and it is now ready to serve your website. You also have the option to secure your site with SSL using Let’s Encrypt, which provides free and trusted encryption for safer connections. Regularly monitoring your server and keeping Nginx up to date is essential to maintain both performance and security.
Make sure to replace example.com with your actual domain name and adjust the root directory to match the location of your website files. Proper configuration of the server block and file permissions will ensure smooth operation and accessibility for your visitors. By taking these steps, you can provide a reliable, fast, and secure web experience for everyone accessing your site.


