How to install Nginx in RHEL and CentOS

In this article, we’ll explain how to install Nginx in RHEL and CentOS. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. In this blog, we explain how to install Nginx in RHEL and CentOS. NGINX is a free, open-source, high-performance HTTP server and reverses proxy, as well as an IMAP/POP3 proxy server. 
How to install Nginx in RHEL and CentOS
To add NGINX yum repository, create a file named /etc/yum.repos.d/nginx.repo and paste one of the configurations below:
CentOS:
            [nginx]
            name=nginx repo
            baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
            gpgcheck=0
            enabled=1
RHEL:
            [nginx]
            name=nginx repo
            baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/
            gpgcheck=0
            enabled=1
Due to differences between how CentOS, RHEL, and Scientific Linux populate the $releasever variable, it is necessary to manually replace $releasever with either 5 (for 5.x) or 6 (for 6.x), depending upon your OS version.
Installing Nginx through yum is just a command away. and can be done as below:
[root@app-node1 ~]# yum install nginx                                                                             
Loaded plugins: rhnplugin, security
This system is not registered with RHN.
RHN support will be disabled.
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package nginx.i386 0:0.8.55-2.el5 set to be updated
--> Processing Dependency: libGeoIP.so.1 for package: nginx
--> Running transaction check
---> Package GeoIP.i386 0:1.4.8-1.el5 set to be updated
--> Finished Dependency Resolution
....
Dependencies Resolved
================================================================================
 Package          Arch            Version                 Repository       Size
================================================================================
Installing:
 nginx            i386            0.8.55-2.el5            epel            390 k
Installing for dependencies:
 GeoIP            i386            1.4.8-1.el5             epel            781 k

Transaction Summary
================================================================================
Install      2 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

Total download size: 1.1 M
Is this ok [y/N]: Y

If you encounter dependency issues while installing through yum you can solve that by searching the dependency package or library file(with .so extension) in pkgs.org and then downloading and installing that package for your respective version of OS, by rpm command.

Installing Nginx from Source

first of all download the source package from here

Now unzip the package with the following command.
[root@app-node1 ~]# tar -xvf nginx-1.2.4.tar.gz

Now you will have a directory named nginx-1.2.4.tar.gz in the location where you unzipped the .gz package file.

/usr/local/nginx is the default installation directory of nginx.
Now let’s get inside the directory which we unzipped.
[root@app-node1 nginx-1.2.4]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README

Let’s configure our Nginx installation with ./configure command. There is any number of options available with this ./configure command, like the following.

–prefix  to override the default installation path of /usr/local/nginx
–sbin-path this option can be used to specify the Nginx command path.
–pid-path    PID file path; like the files we have in /var/run/
–http-log-path you can specify the log file path with this option.
with these above-mentioned options, I can both mention the path of the files, and also the file names.
[root@app-node1 nginx-1.2.4]# ./configure --prefix=/home/sshinde/ --pid-path=/etc/nginx/nginx.pid --http-log-path=/var/log/nginx_access_log --with-pcre=/etc/nginx/pcre --with-zlib=/etc/nginx/zlib
 [root@app-node1 nginx-1.2.4]# ./configure --prefix=/home/sshinde/ --pid-path=/etc/nginx/nginx.pid --http-log-path=/var/log/nginx_access_log --without-http_rewrite_module --without-http_gzip_module
Now we will compile our selected options. by make command.
[root@app-node1 nginx-1.2.4]# make
Now let’s install it.
[root@app-node1 nginx-1.2.4]# make install
That’s it you have installed Nginx from the source package.  Now let’s start Nginx with the below command.
[root@app-node1 ~]# /home/sshinde/sbin/nginx

I have installed it in /home/sshinde which is the reason I am using this path to start Nginx. Let‘s confirm whether our Nginx is running by the following command.

[root@app-node1 ~]# lsof -i :80
COMMAND  PID   USER   FD   TYPE DEVICE SIZE NODE NAME
nginx   5850   root    6u  IPv4  52188       TCP *:http (LISTEN)
and also from the below command.
[root@app-node1 ~]# ps aux | grep nginx
root      5850  0.0  0.1   2600   984 ?        Ss   10:00   0:00 nginx: master process /home/sshinde/sbin/nginx
nobody    5890  0.0  0.1   2748   896 ?        S    10:01   0:00 nginx: worker process
Now let’s see and compare if that’s the pid written to the file we mentioned during our installation configuration option –pid-path option.
[root@app-node1 ~]# cat /etc/nginx/nginx.pid
5850
Manage the Nginx Process
Now that you have your web server up and running, we can go over some basic management commands.

Command to start/stop/restart NGINX Service in Linux

To stop your web server, you can type:
[root@app-node1 ~]# sudo service nginx stop
To start the webserver when it is stopped, type:
[root@app-node1 ~]# sudo service nginx start
To stop and then start the service again, type:
[root@app-node1 ~]# sudo service nginx restart

this is the end of tutorials, we explained How to install Nginx in RHEL and CentOS.

Thanks for reading these articles and you’ll also like.

Steps to Manage logs in Linux using Logrotate

Step By Step Method for installing Nagios in Amazon Linux

How to Install and Configure Cacti on Ubuntu 18 LTS

How to Install and Configure HAProxy on CentOS or Amazon Linux

What is Linux Software yum and rpm

Step to Install Docker on Amazon Linux or CentOS Linux







1 thought on “How to install Nginx in RHEL and CentOS”

Comments are closed.