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
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

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/knot35.com.conf
# 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;

server_name knot35.com www.knot35.com;
access_log /var/log/nginx/knot35.com.access.log;
error_log /var/log/nginx/knot35.com.error.log;

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

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
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 knot35.com 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
Previous articleSetting Up PHP 7.4 on CentOS 7 Server Edition
Next articleInstalling and Configuring MariaDB 10.4 on CentOS 8
I'm just an freelancer and part-time worker at several freelance sites. Here at knot35.com we will share everything knowledge about Linux, Unix-Like, and open-source softwares.

LEAVE A REPLY

Please enter your comment!
Please enter your name here