LinuxLoad BalancerNginx
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.
To add NGINX yum repository, create a file named /etc/yum.repos.d/nginx.repo and paste one of the configurations below:
1 2 3 4 5 6 7 8 9 10 11 12 |
<strong>CentOS:</strong> [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 <strong>RHEL:</strong> [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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[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 <span style="font-family: helvetica, sans-serif; font-size: 11.5pt;">....</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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 |
Installing Nginx from Source:
first of all download the source package from here
Now unzip the package with the following command.
1 |
[root@app-node1 ~]# tar -xvf nginx-1.2.4.tar.gz |
/usr/local/nginx is the default installation directory of nginx.
Now let’s get inside the directory which we unzipped.
1 2 3 |
[root@app-node1 nginx-1.2.4]# ls auto CHANGES.ru configure html man src CHANGES conf contrib LICENSE README |
–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.
1 2 |
[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.
1 |
[root@app-node1 nginx-1.2.4]# make |
Now let’s install it.
1 |
[root@app-node1 nginx-1.2.4]# make install |
That’s it you have installed nginx from source package. Now let’s start nginx with the below command.
1 |
[root@app-node1 ~]# /home/sshinde/sbin/nginx |
1 2 3 |
[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.
1 2 3 |
[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.
1 2 |
[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:
1 |
[root@app-node1 ~]# sudo service nginx stop |
To start the web server when it is stopped, type:
1 |
[root@app-node1 ~]# sudo service nginx start |
To stop and then start the service again, type:
1 |
[root@app-node1 ~]# sudo service nginx restart |
good explanation