How to install nginx from source on CentOS 7

In this tutorial we will learn how to install nginx from source on CentOS 7 . Nginx has ability to handle more than 10,000 simultaneous connections with nearly 2.5 Mb of memory usage.
In our previous post on installing nginx from source on CentOS/RHEL , we have already mention brief intro on Nginx.

Why we prefer installation of Nginx from source code

The only reason which we found that some module can only be installed with compilation method.

Follow the given below steps to install Nginx from source on CentOS 7

The steps are also applicable to RHEL 7 version.

Note: Login as root user in Server

Details :
Nginx Version = 1.6.0 stable release
Operating System = CentOS 7
O.S Arch = x86_64

Install EPEL repo

We will first install EPEL repo. At the time of writing, EPEL has version 7(beta) available. Hence, we are using the same repo

We suggest you to check once on EPEL repo site i.e http://dl.fedoraproject.org/pub/epel/

Install EPEL repo with below given command

rpm -ivh http://dl.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm

Install prerequisite packages

Install prerequisite packages require for Nginx installation

yum install gc gcc gcc-c++ pcre-devel zlib-devel make wget openssl-devel libxml2-devel libxslt-devel gd-devel perl-ExtUtils-Embed GeoIP-devel gperftools gperftools-devel libatomic_ops-devel perl-ExtUtils-Embed

Download Nginx source code

Download the latest available nginx source code from Nginx website

At the time of writing, Nginx version 1.6.0 was stable release. We suggest to always go for stable latest release.

Download by using wget command

wget nginx.org/download/nginx-1.6.0.tar.gz

Create nginx user

Create nginx user with nologin shell. This user will be used by Nginx Web Server .

useradd nginx
usermod -s /sbin/nologin nginx

Compiling and installing Nginx

In this section, we will compile and install nginx from its source code.
First, untar or extract the downloaded Nginx source package

tar -xvzf nginx-1.6.0.tar.gz

Change to extracted directory .In our case , nginx-1.6.0 directory extracted out.

cd nginx-1.6.0

You can find many available options and about their details by using command . We suggest you to read about all these options and run below given command . (It is part of learning)

./configure --help

Now we are using available options with configure script of Nginx . We will enable some modules at the time of compiling Nginx.
Note: We highly recommend you to use ./configure --help command. It helps you a lot.

We have tried to enable almost all modules. You can also remove any module by removing it from this list.

./configure --user=nginx --group=nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-rtsig_module --with-select_module --with-poll_module --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-cpp_test_module  --with-cpu-opt=CPU --with-pcre  --with-pcre-jit  --with-md5-asm  --with-sha1-asm  --with-zlib-asm=CPU --with-libatomic --with-debug --with-ld-opt="-Wl,-E"

Now run

make && make install

Start Nginx service

After completion of compiling and installation. Now we will start nginx service by using given below command

/usr/sbin/nginx -c /etc/nginx/nginx.conf

Check status of Nginx

We can use ps command, it will show running process of Nginx

ps -ef|grep nginx

Stop Nginx

To stop nginx we will first get the PID of running nginx service by using command ps -ef|grep nginx. By using kill command we will stop the process of nginx

kill -9 PID-Of-Nginx

See the below given reference for Start/Status/Stop method for Nginx service

[root@localhost ~]# /usr/sbin/nginx -c /etc/nginx/nginx.conf
[root@localhost ~]# 
[root@localhost ~]# ps -ef|grep nginx
root      5071     1  0 07:13 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     5072  5071  0 07:13 ?        00:00:00 nginx: worker process
root      5074  1879  0 07:13 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# kill -9 5071 5072
[root@localhost ~]#

In above section, we can see PID no. 5071 and 5072 are running . Hence we killed it by using command kill -9 5071 5072

Add iptables rule for Nginx

Because it is webserver , by default it will run on port no. 80 i.e HTTP protocol. Hence, we will permit port no. 80 to be access from all.

Temporary adding iptable rule for HTTP

iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

Permanently adding iptable rule for HTTP

Edit the file, /etc/sysconfig/iptables by using your favorite file editor. We use vi .

vi /etc/sysconfig/iptables

Now add the below given line at INPUT section of iptables

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

See below given screenshot, check the position of iptable rule for HTTP(port 80)
Nginx iptables

Now restart the iptables service

systemctl restart iptables

Check the all applied iptables rule list by using command

 iptables -nL 

Access Website from webbrowser

Now open the webbrowser (chrome,firefox,etc) and type the IP address of your Nginx web server.
In my case, our ipaddress is 192.168.56.102

See the below given screenshot, it shows the default welcome page of Nginx
nginx

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.