How to install Nginx from Source Code on CentOS 7

To install Nginx from Source Code on CentOS 7

In our previous article, we have seen about the installation procedure of PHP from Source Code on CentOS 6. In this tutorial we will be briefed about the installation of Nginx from Source Code on CentOS 7. Nginx is an open-source, high-performance web server that delivers static content by using the system resources. It also hosts several highest traffic internet sites.

Installation procedure

Download the source code from Nginx official website using yum command and then compile the packages to install it. Press y to continue with the installation procedure.

[root@linuxhelp ~]# yum groupinstall " Development Tools"  -y
Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
base                                                                                                                                                                | 3.6 kB  00:00:00     
extras                                                                                                                                                              | 3.4 kB  00:00:00     
updates                                                                                                                                                             | 3.4 kB  00:00:00     
(1/2): extras/7/x86_64/primary_db                                                                                                                                   | 129 kB  00:00:00     
(2/2): updates/7/x86_64/primary_db                                                                                                                                  | 3.6 MB  00:00:13     
Determining fastest mirrors
 * base: centos.myfahim.com
 * extras: centos.myfahim.com
 * updates: centos.myfahim.com
Resolving Dependencies
-->  Running transaction check
--->  Package autoconf.noarch 0:2.69-11.el7 will be installed
-->  Processing Dependency: m4 > = 1.4.14 for package: autoconf-2.69-11.el7.noarch
-->  Processing Dependency: perl(Data::Dumper) for package: autoconf-2.69-11.el7.noarch
--->  Package automake.noarch 0:1.13.4-3.el7 will be installed
-->  Processing Dependency: perl(Thread::Queue) for package: automake-1.13.4-3.el7.noarch
-->  Processing Dependency: perl(TAP::Parser) for package: automake-1.13.4-3.el7.noarch
--->  Package bison.x86_64 0:3.0.4-1.el7 will be installed
.
.
.
pendency Updated:
  boost-system.x86_64 0:1.53.0-27.el7      dyninst.x86_64 0:9.3.1-1.el7       gettext.x86_64 0:0.19.8.1-2.el7      gettext-libs.x86_64 0:0.19.8.1-2.el7    glibc.x86_64 0:2.17-196.el7   
  glibc-common.x86_64 0:2.17-196.el7       libgcc.x86_64 0:4.8.5-16.el7       libgomp.x86_64 0:4.8.5-16.el7        libstdc++.x86_64 0:4.8.5-16.el7         rpm.x86_64 0:4.11.3-25.el7    
  rpm-build-libs.x86_64 0:4.11.3-25.el7    rpm-libs.x86_64 0:4.11.3-25.el7    rpm-python.x86_64 0:4.11.3-25.el7    systemtap-runtime.x86_64 0:3.1-3.el7   

Complete!

Now install the below mentioned packages to compile Nginx from its source.

[root@linuxhelp ~]# yum install zlib-devel pcre-devel openssl-devel -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.myfahim.com
 * extras: centos.myfahim.com
 * updates: centos.myfahim.com
Resolving Dependencies
-->  Running transaction check
--->  Package openssl-devel.x86_64 1:1.0.2k-8.el7 will be installed
-->  Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-8.el7 for package: 1:openssl-devel-1.0.2k-8.el7.x86_64
-->  Processing Dependency: krb5-devel(x86-64) for package: 1:openssl-devel-1.0.2k-8.el7.x86_64
--->  Package pcre-devel.x86_64 0:8.32-17.el7 will be installed
-->  Processing Dependency: pcre(x86-64) = 8.32-17.el7 for package: pcre-devel-8.32-17.el7.x86_64
--->  Package zlib-devel.x86_64 0:1.2.7-17.el7 will be installed
.
.
.
Dependency Updated:
  dracut-config-rescue.x86_64 0:033-502.el7        dracut-network.x86_64 0:033-502.el7         e2fsprogs.x86_64 0:1.42.9-10.el7              e2fsprogs-libs.x86_64 0:1.42.9-10.el7       
  glib2.x86_64 0:2.50.3-3.el7                      kmod.x86_64 0:20-15.el7_4.4                 krb5-libs.x86_64 0:1.15.1-8.el7               libcom_err.x86_64 0:1.42.9-10.el7           
  libgudev1.x86_64 0:219-42.el7_4.4                libselinux.x86_64 0:2.5-11.el7              libselinux-python.x86_64 0:2.5-11.el7         libselinux-utils.x86_64 0:2.5-11.el7        
  libsepol.x86_64 0:2.5-6.el7                      libss.x86_64 0:1.42.9-10.el7                openssl.x86_64 1:1.0.2k-8.el7                 openssl-libs.x86_64 1:1.0.2k-8.el7          
  pcre.x86_64 0:8.32-17.el7                        systemd-libs.x86_64 0:219-42.el7_4.4        systemd-python.x86_64 0:219-42.el7_4.4        systemd-sysv.x86_64 0:219-42.el7_4.4        
  zlib.x86_64 0:1.2.7-17.el7                      

Complete!

Install the epel repository to the target system and press y to continue with the installation procedure.

[root@linuxhelp ~]# yum install epel-release -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.myfahim.com
 * extras: centos.myfahim.com
 * updates: centos.myfahim.com
Resolving Dependencies
-->  Running transaction check
--->  Package epel-release.noarch 0:7-9 will be installed
-->  Finished Dependency Resolution
.
.
.
Transaction test succeeded
Running transaction
  Installing : epel-release-7-9.noarch                                                                                                                                                 1/1 
  Verifying  : epel-release-7-9.noarch                                                                                                                                                 1/1 

Installed:
  epel-release.noarch 0:7-9                                                                                                                                                                

Complete!

Next install the below mentioned dependent packages for the Nginx to run in the target system.

[root@linuxhelp ~]# yum install perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.myfahim.com
 * epel: epel.mirror.net.in
 * extras: centos.myfahim.com
 * updates: centos.myfahim.com
epel/x86_64/primary_db                                                                                                                                              | 6.1 MB  00:00:14     
Package libxslt-1.1.28-5.el7.x86_64 already installed and latest version
Package gd-2.0.35-26.el7.x86_64 already installed and latest version
Resolving Dependencies
-->  Running transaction check
--->  Package GeoIP.x86_64 0:1.5.0-11.el7 will be installed
--->  Package GeoIP-devel.x86_64 0:1.5.0-11.el7 will be installed
--->  Package gd-devel.x86_64 0:2.0.35-26.el7 will be installed
-->  Processing Dependency: libpng-devel for package: gd-devel-2.0.35-26.el7.x86_64
.
.
.
Dependency Updated:
  expat.x86_64 0:2.1.0-10.el7_3   fontconfig.x86_64 0:2.10.95-11.el7        freetype.x86_64 0:2.4.11-15.el7      libX11.x86_64 0:1.6.5-1.el7       libX11-common.noarch 0:1.6.5-1.el7  
  libXpm.x86_64 0:3.5.12-1.el7    libdb.x86_64 0:5.3.21-20.el7              libdb-utils.x86_64 0:5.3.21-20.el7   libgcrypt.x86_64 0:1.5.3-14.el7   libpng.x86_64 2:1.5.13-7.el7_2      
  libxcb.x86_64 0:1.12-1.el7      libxml2-python.x86_64 0:2.9.1-6.el7_2.3   perl-libs.x86_64 4:5.16.3-292.el7    xz.x86_64 0:5.2.2-1.el7           xz-libs.x86_64 0:5.2.2-1.el7        

Complete!

Download the Nginx source code from Nginx' s offical website or just run the below wget command to install Nginx version 1.13.6.

[root@linuxhelp ~]# wget https://nginx.org/download/nginx-1.13.6.tar.gz
--2017-10-28 11:08:28--  https://nginx.org/download/nginx-1.13.6.tar.gz
Resolving nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...
Connecting to nginx.org (nginx.org)|206.251.255.63|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 989760 (967K) [application/octet-stream]
Saving to: ‘ nginx-1.13.6.tar.gz’ 

100%[=================================================================================================================================================> ] 989,760     46.7KB/s   in 21s    

2017-10-28 11:08:51 (46.7 KB/s) - ‘ nginx-1.13.6.tar.gz’  saved [989760/989760]

Extract the downloaded package using tar command and move into the extracted directory to compile Nginx from source. List the files as follows.

[root@linuxhelp ~]# tar -xzf nginx-1.13.6.tar.gz 
[root@linuxhelp ~]# cd nginx-1.13.6/
[root@linuxhelp nginx-1.13.6]# ll
total 720
drwxr-xr-x. 6 1001 1001   4096 Oct 28 11:09 auto
-rw-r--r--. 1 1001 1001 282456 Oct 10 20:52 CHANGES
-rw-r--r--. 1 1001 1001 430416 Oct 10 20:52 CHANGES.ru
drwxr-xr-x. 2 1001 1001   4096 Oct 28 11:09 conf
-rwxr-xr-x. 1 1001 1001   2502 Oct 10 20:52 configure
drwxr-xr-x. 4 1001 1001     68 Oct 28 11:09 contrib
drwxr-xr-x. 2 1001 1001     38 Oct 28 11:09 html
-rw-r--r--. 1 1001 1001   1397 Oct 10 20:52 LICENSE
drwxr-xr-x. 2 1001 1001     20 Oct 28 11:09 man
-rw-r--r--. 1 1001 1001     49 Oct 10 20:52 README
drwxr-xr-x. 9 1001 1001     84 Oct 28 11:09 src

Now to configure Nginx installation procedure, run the configuration script command with some options that will be required for installation. To view all the options, simply run the below script command with help options and check the options that should be configured for Nginx installation. Or you can also install the default configuration by running the configure script command without any options followed by it.

[root@linuxhelp nginx-1.13.6]# ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

.
.
.
--with-zlib=DIR                    set path to zlib library sources
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  --with-zlib-asm=CPU                use zlib assembler sources optimized
                                     for the specified CPU, valid values:
                                     pentium, pentiumpro

  --with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources

  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

  --with-debug                       enable debug logging

Now run the configure script command followed by the required options.

[root@linuxhelp nginx-1.13.6]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx
checking for OS
 + Linux 3.10.0-123.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
.
.
.
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: " /etc/nginx" 
  nginx binary file: " /usr/sbin/nginx" 
  nginx modules path: " /etc/nginx/modules" 
  nginx configuration prefix: " /etc/nginx/conf" 
  nginx configuration file: " /etc/nginx/conf/nginx.conf" 
  nginx pid file: " /var/run/nginx.pid" 
  nginx error log file: " /var/log/nginx/error.log" 
  nginx http access log file: " /etc/nginx/logs/access.log" 
  nginx http client request body temporary files: " client_body_temp" 
  nginx http proxy temporary files: " proxy_temp" 
  nginx http fastcgi temporary files: " fastcgi_temp" 
  nginx http uwsgi temporary files: " uwsgi_temp" 
  nginx http scgi temporary files: " scgi_temp" 

Execute the make command for configuration process.

[root@linuxhelp nginx-1.13.6]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.13.6' 
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs 
    -o objs/src/core/nginx.o 
    src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs 
    -o objs/src/core/ngx_log.o 
    src/core/ngx_log.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs 
    -o objs/src/core/ngx_palloc.o 
    src/core/ngx_palloc.c
.
.
.
objs/src/http/modules/ngx_http_upstream_ip_hash_module.o 
objs/src/http/modules/ngx_http_upstream_least_conn_module.o 
objs/src/http/modules/ngx_http_upstream_keepalive_module.o 
objs/src/http/modules/ngx_http_upstream_zone_module.o 
objs/ngx_modules.o 
-ldl -lpthread -lcrypt -lpcre -lz 
-Wl,-E
sed -e " s|%%PREFIX%%|/etc/nginx|"  
    -e " s|%%PID_PATH%%|/var/run/nginx.pid|"  
    -e " s|%%CONF_PATH%%|/etc/nginx/conf/nginx.conf|"  
    -e " s|%%ERROR_LOG_PATH%%|/var/log/nginx/error.log|"  
    <  man/nginx.8 >  objs/nginx.8
make[1]: Leaving directory `/root/nginx-1.13.6' 

Now install the Nginx with make install command as follows.

[root@linuxhelp nginx-1.13.6]# make install
make -f objs/Makefile install
make[1]: Entering directory `/root/nginx-1.13.6' 
test -d ' /etc/nginx'  || mkdir -p ' /etc/nginx' 
test -d ' /usr/sbin'  
    || mkdir -p ' /usr/sbin' 
test ! -f ' /usr/sbin/nginx'  
    || mv ' /usr/sbin/nginx'  
        ' /usr/sbin/nginx.old' 
cp objs/nginx ' /usr/sbin/nginx' 
test -d ' /etc/nginx/conf'  
    || mkdir -p ' /etc/nginx/conf' 
cp conf/koi-win ' /etc/nginx/conf' 
cp conf/koi-utf ' /etc/nginx/conf' 
.
.
.
test -d ' /etc/nginx/logs'  
    || mkdir -p ' /etc/nginx/logs' 
test -d ' /etc/nginx/html'  
    || cp -R html ' /etc/nginx' 
test -d ' /var/log/nginx'  
    || mkdir -p ' /var/log/nginx' 
make[1]: Leaving directory `/root/nginx-1.13.6' 

Check the version of the Nginx by executing the following command.

[root@linuxhelp ~]# nginx -v
nginx version: nginx/1.13.6

Create an user for Nginx and set the proper ownership for Nginx installation directory by running the following command.

[root@linuxhelp ~]# useradd nginx
[root@linuxhelp ~]# chown -R nginx:nginx /etc/nginx/

Configure the service daemon for Nginx by using the vim editor and enter the following content in the file. Save and exit the file.

[root@linuxhelp ~]# vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/conf/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

Now start and enable Nginx service by running the following command.

[root@linuxhelp ~]# systemctl start nginx
[root@linuxhelp ~]# systemctl enable nginx

Switch over to the browser and enter the machine' s IP. The Nginx default webpage is displayed in the browser as shown below.

Thus we conclude the installation procedure of Nginx from Source Code on CentOS 7.

Tag : Nginx CentOS
FAQ
Q
Does NGINX Plus support load balancing for mail protocols?
A
Nginx support load balancing for HTTP/HTTPS/FastCGI/uwsgi/SCGI/memcache only. Nginx proxy and route SMTP/POP3/IMAP traffic to a single upstream rather than load-balancing it.
Q
What is thePurpose of using the Nginx?
A
Nginx is an open source high performance Load balancer and web server software. It supports HTTP, HTTPS, SMTP, POP3, and IMAP protocols as a reverse proxy server.
Q
How to setup odoo on IPv6 port 80? Is it possible to use nginx to do the same?
A
Why do you want to setup Odoo to listen on IPv6 on localhost? Use nginx frontend to be accessed from IPv6 with the below configuration: listen [::]:80;
Q
Is it safe to use the development branch in production?
A
In general, all releases (development or otherwise) are quite stable. This site runs the latest development version at all times. Many NGINX users tend to represent an “early adopter” crowd,
Q
Can I deploy NGINX Plus for HTTP video?
A
Yes, NGINX Plus is a very popular solution for HTTP video distribution, and a great, high-performance, highly scalable, extremely cost-efficient alternative to Wowza or Adobe products.