Installing and Configuring Nginx Mainline Branch on CentOS 7

Screenshot From 2020 05 21 20 09 53

CentOS (Community ENTerprise Operating System) is the most widely used Linux distribution in almost all web hosting server environments around the world. CentOS is known for its reliability, long-term support, and of course it’s a completely free and community-driven version of Red Hat Enterprise Linux (RHEL).

Well, to build high-performance Nginx web server on CentOS 7 machine is very easy. Simply follow this tutorial…!!! We usually prefer the Nginx mainline branch instead of the stable branch. Since the mainline branch is more stable, more reliable, more bug-free, and of course it gets more updates, improvements and enhanced features.

1.) Install Some Packages and Prerequisites

$ yum install nano wget telnet screen curl yum-utils

2.) Add Nginx Repository

First of all, you need to add Nginx repository to yum.repos.d directory. Simply create a new file called “nginx.repo”. Make sure to set “enabled=1” on nginx.repo.

$ nano /etc/yum.repos.d/nginx.repo
name=nginx mainline repo

However, if you want to configure Nginx stable branch, you can follow official documentation on Nginx website.

3.) Run System Update and Let’s Install Nginx Mainline Branch

$ yum update
$ yum install nginx

4.) Enable Nginx Service to Start Automatically from Boot

$ systemctl enable nginx.service
$ systemctl start nginx

5.) Configuring Your Firts ServerBlock on Nginx

Nginx has its own virtual host called ServerBlock. If you currently run Apache web server on your virtual machine, you should be familiar with vhost, right?

$ mkdir -p /home/something/public_html
$ chown -R nginx:nginx /home/something/public_html
$ nano /etc/nginx/conf.d/
# ServerBlock Configuration for Domain
server {
listen 80; ## Listen for IPv4 Address;
#listen [::]:80 Default IPv6only=on;

root /home/something/public_html/;
index index.htm index.php index.html;

access_log /var/log/nginx/;
error_log /var/log/nginx/;

location / {
try_files $uri $uri/ /index.php?$args;

location ~ \.php$ {
try_files $uri =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param HTTP_PROXY "";
#include fastcgi.conf;
# fastcgi_intercept_errors on;

location = /robots.txt { access_log off; log_not_found off; }
location ~ /\. { deny all; access_log off; log_not_found off; }

#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;

Please make sure to replace with your actual domain name.

6.) Check Your ServerBlock and Restart Nginx

$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ service nginx restart
Redirecting to /bin/systemctl restart nginx.service