Install Nginx on CentOS
If you’re looking for a high-performance web server, Nginx is a great choice. Nginx is a lightweight and efficient web server that can handle high traffic websites with ease. In this article, we’ll walk you through the steps to install Nginx on CentOS.
Nginx is a web server that was created in 2002 with a focus on high performance and low resource usage. It’s a popular choice for serving static content and reverse proxying to application servers like Apache or Node.js. CentOS is a popular Linux distribution based on Red Hat Enterprise Linux (RHEL). It’s known for its stability and security features, making it a good choice for server environments.
Checking System Requirements for Nginx Installation
Before installing Nginx on CentOS, it’s important to ensure that your system meets the minimum requirements. Nginx requires a Linux-based operating system with a minimum of 256MB of RAM. Additionally, you should have root access to your server to install Nginx.
Updating CentOS and Installing Required Packages
The first step to install Nginx on CentOS is to ensure that your system is up to date and that you have the required packages installed. You can update CentOS using the following command:
sudo yum update
Next, you’ll need to install the packages required for Nginx. These packages include the EPEL repository, which provides additional packages for CentOS, and the Nginx package itself. You can install these packages using the following command:
sudo yum install epel-releasesudo yum install nginx
Download and Install Nginx on CentOS
Once you’ve installed the required packages, you can proceed with downloading and installing Nginx on CentOS. You can download the latest version of Nginx from the official website using the following command:
wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.0-1.el7.ngx.x86_64.rpm
After downloading the package, you can install Nginx using the following command:
sudo yum install nginx-1.20.0-1.el7.ngx.x86_64.rpm
Configuring Nginx Server Blocks
Now that you’ve installed Nginx on CentOS, it’s time to configure it to serve your website. Nginx uses server blocks to define how it handles incoming requests. By default, Nginx serves the files in the /usr/share/nginx/html
directory. You can create a new server block to serve your website by creating a new configuration file in the /etc/nginx/conf.d
directory. For example, if your website is example.com, you can create a configuration file named example.com.conf
with the following content:
server { listen 80; server_name example.com; root /var/www/example.com; index index.html; location / { try_files $uri $uri/ /index.html; }}
/var/www/example.com
directory and uses the index.html
file as the default index file. The location /
block defines how Nginx handles requests for URLs that don’t match an existing file.Setting up Firewall for Nginx on CentOS
By default, CentOS comes with a firewall called firewalld. If you want to allow traffic to your Nginx server, you’ll need to add rules to the firewall. You can allow traffic on the default HTTP and HTTPS ports (80 and 443) using
the following commands:
sudo firewall-cmd --zone=public --permanent --add-service=httpsudo firewall-cmd --zone=public --permanent --add-service=httpssudo firewall-cmd --reload
This will add rules to allow traffic on ports 80 and 443 and reload the firewall to apply the changes.
Creating SSL Certificates for HTTPS traffic
If you want to serve your website over HTTPS, you’ll need to create an SSL certificate. You can create a self-signed SSL certificate for testing purposes using the following commands:
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/pki/nginx/example.com.key -out /etc/pki/nginx/example.com.crt
/etc/pki/nginx
directory.Testing Nginx Configuration and Starting the Server
Before starting Nginx, it’s important to ensure that your configuration is valid. You can test the configuration using the following command:
sudo nginx -t
If the configuration is valid, you’ll see a message indicating that the test was successful. You can start Nginx using the following command:
sudo systemctl start nginx
You can check the status of Nginx using the following command:
sudo systemctl status nginx
Securing Nginx Installation with Basic Authentication
If you want to restrict access to your website, you can use basic authentication to require a username and password to access it. To enable basic authentication, you’ll need to create a password file and update your server block configuration to require authentication. You can create a password file using the following command:
sudo htpasswd -c /etc/nginx/.htpasswd exampleuser
This will create a password file named .htpasswd
in the /etc/nginx
directory and prompt you to enter a password for the user exampleuser
.
To require authentication for your website, you can update your server block configuration with the following content:
server { listen 80; server_name example.com; root /var/www/example.com; index index.html; location / { auth_basic Restricted Content; auth_basic_user_file /etc/nginx/.htpasswd; try_files $uri $uri/ /index.html; }}
Monitoring Nginx Server with Prometheus and Grafana
If you want to monitor your Nginx server, you can use Prometheus and Grafana to collect and visualize metrics. You can install Prometheus and Grafana using the following commands:
sudo yum install prometheussudo yum install grafana
After installing Prometheus and Grafana, you can configure Nginx to export metrics to Prometheus. You can update your server block configuration with the following content:
server { listen 80; server_name example.com; root /var/www/example.com; index index.html; location / { auth_basic Restricted Content; auth_basic_user_file /etc/nginx/.htpasswd; try_files $uri $uri/ /index.html; } location /metrics { stub_status on; access_log off; allow 127.0.0.1; deny all; }}
/metrics
URL. You can then configure Prometheus to scrape metrics from Nginx and Grafana to visualize the metrics.Troubleshooting Common Nginx Installation Issues on CentOS
If you encounter issues during the installation process, here are some common problems and their solutions:
Error starting Nginx
If you encounter an error when starting Nginx, you can check the error log located at /var/log/nginx/error.log for more information. This may indicate an issue with your configuration or conflicting ports.
Firewall blocking Nginx traffic
If you’re unable to access your website, check that the firewall is allowing traffic on ports 80 and 443.
Nginx returning 404 Not Found
If you encounter a 404 error when accessing your website, check that your server block configuration is correct and that the root directory is pointing to the correct location.
Nginx returning 502 Bad Gateway
If you encounter a 502 error when accessing your website, check that your upstream server is running and that the correct port is being used.
Conclusion: Final Thoughts on Installing Nginx on CentOS
In this article, we’ve covered how to install Nginx on CentOS. We started by checking system requirements and updating CentOS, before downloading and installing Nginx. We then covered how to configure server blocks, set up a firewall, and create SSL certificates for HTTPS traffic. We also covered how to secure the installation with basic authentication and monitor the server with Prometheus and Grafana. Finally, we discussed common issues and how to troubleshoot them.