Install and Compile “Nginx 1.10.0” (Stable Release) from Sources in RHEL/CentOS 7.0

Nginx is the most fast growing Webserver today on public internet facing servers due to its free open source modular model, high-performance, stability, simple configurations files, asynchronous architecture (event-driven) and low resources needed to run.

Requirements

For RHEL 7.0
  1. Minimal Installation of RHEL 7.0
  2. Active RedHat Subscription and Repositories on RHEL 7.0
For CentOS 7.0
  1. Minimal Installation of CentOS 7.0
Configure Static IP Address
  1. Set Static IP Address on RHEL/CentOS 7.0

This tutorial will guide you on installing latest stable version of Nginx 1.10.0 on Red Hat Enterprise or CentOS 7 from sources, because official RHEL/CentOS 7 repositories mirrors doesn’t provide a binary package. If you want to avoid sources installation you can add official Nginx repository and install the binary package (available versions is 1.9.x) with the help of Yum Package Manager as shown:

To enable nginx official yum repository for RHEL/CentOS 7, create a file /etc/yum.repos.d/nginx.repo with the following contents:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

Replace “centos” with “rhel”, depending on the distribution you’re using and install nginx with yum package manager as shown:

# yum install nginx


Important: Please note, following above official nginx yum repositories will give you older version of nginx, if you really want to build most recent version of Nginx, then i suggest you to follow source installation as shown below.

Using sources compilation and installation has some benefits, due to the fact that you can install the latest version available, you can tweak Nginx configuration by adding or removing modules, change installation system path, or other important settings, in other words, you have a complete control over installation process.

Step 1: Download, Compile and Install Nginx

1. Before starting Nginx compilation and installation process make sure that you have C / C++ compiler, PCRE (Perl Compatible Regular Expressions), Zlib Compression Library and OpenSSL (if you intend to run Nxing with SSL support) packages installed on your machine by issuing the following command.

# yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel

Install GCC and C++ Compiler

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile * base: centos.mirror.net.in * extras: centos.mirror.net.in * updates: centos.mirror.net.in
Package 1:make-3.82-21.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package gcc.x86_64 0:4.8.5-4.el7 will be installed
--> Processing Dependency: libgomp = 4.8.5-4.el7 for package: gcc-4.8.5-4.el7.x86_64
--> Processing Dependency: cpp = 4.8.5-4.el7 for package: gcc-4.8.5-4.el7.x86_64
--> Processing Dependency: libgcc >= 4.8.5-4.el7 for package: gcc-4.8.5-4.el7.x86_64
--> Processing Dependency: glibc-devel >= 2.2.90-12 for package: gcc-4.8.5-4.el7.x86_64
--> Processing Dependency: libmpfr.so.4()(64bit) for package: gcc-4.8.5-4.el7.x86_64
--> Processing Dependency: libmpc.so.3()(64bit) for package: gcc-4.8.5-4.el7.x86_64
---> Package gcc-c++.x86_64 0:4.8.5-4.el7 will be installed
--> Processing Dependency: libstdc++-devel = 4.8.5-4.el7 for package: gcc-c++-4.8.5-4.el7.x86_64
--> Processing Dependency: libstdc++ = 4.8.5-4.el7 for package: gcc-c++-4.8.5-4.el7.x86_64
---> Package openssl-devel.x86_64 1:1.0.1e-51.el7_2.4 will be installed
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.1e-51.el7_2.4 for package: 1:openssl-devel-1.0.1e-51.el7_2.4.x86_64
--> Processing Dependency: krb5-devel(x86-64) for package: 1:openssl-devel-1.0.1e-51.el7_2.4.x86_64
---> Package pcre-devel.x86_64 0:8.32-15.el7 will be installed
--> Processing Dependency: pcre(x86-64) = 8.32-15.el7 for package: pcre-devel-8.32-15.el7.x86_64
---> Package zlib-devel.x86_64 0:1.2.7-15.el7 will be installed
--> Processing Dependency: zlib = 1.2.7-15.el7 for package: zlib-devel-1.2.7-15.el7.x86_64
...

2. Now go to Nginx official page and grab the latest Stable version (nginx 1.10.0) available using wget command, extract the TAR archive and enter Nginx extracted directory, using the following commands sequence.

# wget http://nginx.org/download/nginx-1.10.0.tar.gz
# tar xfz nginx-1.10.0.tar.gz
# cd nginx-1.10.0/
# ls -all

Download Nginx Source

--2016-03-21 09:30:15-- http://nginx.org/download/nginx-1.10.0.tar.gz
Resolving nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 908954 (888K) [application/octet-stream]
Saving to: ‘nginx-1.10.0.tar.gz’ 100%[=====================================================================================================================================================>] 9,08,954 81.0KB/s in 11s 2016-03-21 09:30:27 (77.4 KB/s) - ‘nginx-1.10.0.tar.gz’ saved [908954/908954]

3. The next step is customize Nginx installation process. Use the configure file to visualize configuration options and modules needed for your compilation process using the following command and make sure that you are in nginx-1.6.0/ path.

# ./configure --help

Nginx 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 --build=NAME set build name --builddir=DIR set build directory --with-select_module enable select module --without-select_module disable select module --with-poll_module enable poll module --without-poll_module disable poll module --with-threads enable thread pool support --with-file-aio enable file AIO support --with-ipv6 enable IPv6 support --with-http_ssl_module enable ngx_http_ssl_module --with-http_v2_module enable ngx_http_v2_module
...

4. Now it’s time to compile Nginx with your specific configurations and enabled or disabled modules. For this tutorial the following modules and specifications where used, but you can tweak the compilation to whatever suits your needs.

  1. –user=nginx –group=nginx => system user and group that Nginx will run as.
  2. –prefix=/etc/nginx => directory for server files (nginx.conf file and other configuration files) – default is /usr/local/nginx directory.
  3. –sbin-path=/usr/sbin/nginx => Nginx executable file location.
  4. –conf-path=/etc/nginx/nginx.conf => sets the name for the nginx.conf configuration file – you can change it.
  5. –error-log-path=/var/log/nginx/error.log => sets Nginx error log file location.
  6. –http-log-path=/var/log/nginx/access.log => sets Nginx access log file location.
  7. –pid-path=/var/run/nginx.pid => sets the name for main process ID file.
  8. –lock-path=/var/run/nginx.lock => sets the name for Nginx lock file.
  9. –with-http_ssl_module => enables building the HTTPS module – not built by default and requires OpenSSL library.
  10. –with-pcre => sets the path to the sources of the PCRE library – not built by default and requires PCRE library.

To view a list of all Nginx modules visit Nginx Wiki web page at http://wiki.nginx.org/Modules.

If you don’t need a specific module installed on Nginx you can disable it using the following command.

--without-module_name

Now start to compile Nginx by issuing the following command, which will use all the configurations and modules discussed above (make sure the command stays on a single line).

# ./configure --user=nginx --group=nginx --prefix=/etc/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 --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-pcre

Configure Nginx With Modules

checking for OS + Linux 3.10.0-229.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-4) (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
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for sched_setaffinity() ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for poll() ... found
checking for /dev/poll ... not found
...

5. After the compilation process verifies all system required utilities like GNU C compiler, PCRE and OpenSSL libraries, it creates the make.conf file and outputs a summary of all configurations.

Nginx Compilation Summary

Configuration summary + using system PCRE library + using system OpenSSL library + md5: using OpenSSL library + sha1: using OpenSSL library + 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" nginx configuration file: "/etc/nginx/nginx.conf" nginx pid file: "/var/run/nginx.pid" nginx error log file: "/var/log/nginx/error.log" nginx http access log file: "/var/log/nginx/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"

6. The last step is to build the binaries using make command, which can take some time to finish depending on your machine resources, and install Nginx on your system with make install command.

Be careful that make install requires root privileges to perform the installation, so if you’re not logged in with root account use a privilege user with sudo.

# make
# make install

Run Nginx Make Command

make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.10.0'
make[1]: Warning: File `src/core/nginx.h' has modification time 3110036 s in the future
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
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_array.o \ src/core/ngx_array.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_list.o \ src/core/ngx_list.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_hash.o \ src/core/ngx_hash.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_buf.o \ src/core/ngx_buf.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_queue.o \
...

Run Nginx Make Install Command

make -f objs/Makefile install
make[1]: Entering directory `/root/nginx-1.10.0'
make[1]: Warning: File `src/core/nginx.h' has modification time 3109935 s in the future
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
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_array.o \ src/core/ngx_array.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_list.o \ src/core/ngx_list.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_hash.o \ src/core/ngx_hash.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_buf.o \ src/core/ngx_buf.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_queue.o \
...

Step 2: Tweak Nginx and Create INIT Script

7. After the installation process has finished with success add nginx system user (with /etc/nginx/ as his home directory and with no valid shell), the user that Nginx will run as by issuing the following command.

# useradd -d /etc/nginx/ -s /sbin/nologin nginx
Add Nginx User

Add Nginx User

8. Because on compilation process we had specified that Nginx will run from nginx system user, open nginx.conf file and change the user statement to nginx.

# nano /etc/nginx/nginx.conf

Here locate and change user and, also, document root location statements, with the following options.

user nginx;
location / { root /srv/www/html; autoindex on; index index.html index.htm;
Run Nginx as User

Run Nginx as User

Enable Nginx DocumentRoot

Enable Nginx DocumentRoot

9. Before starting Nginx, assure that you have created the web document root path, then start nginx using the following command.

# mkdir -p /srv/www/html
# /usr/sbin/nginx

If you want to check if Nginx is running using your shell prompt, run netstat command to verify listen connections.

# netstat -tulpn | grep nginx
Create Nginx DocumentRoot

Create Nginx DocumentRoot

10. To verify it from an remote system, add a Firewall rule to open connection to outside on Port 80, open a browser and direct URL to your server IP Address at http://server_IP.

# firewall-cmd --add-service=http ## For on-fly rule
# firewall-cmd --permanent --add-service=http ## For permanent rule
# systemctl restart firewalld
Verify Nginx Installation

Verify Nginx Installation

11. To manage Nginx process use the following commands.

  1. nginx -V = displays Nginx modules and configurations
  2. nginx -h = help options
  3. nginx = start Nginx process
  4. nginx -s stop = stop Nginx process
  5. nginx -s reload = reload Nginx process
# nginx -V

Check Nginx Installed Version

nginx version: nginx/1.10.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/etc/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 --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-pcre

12. If you need to manage Nginx daemon process through a init RHEL/CentOS script, create the following nginx file on /etc/init.d/ system path, and, then, you can use service or systemctl commands to manage the process.

# nano /etc/init.d/nginx

Add the following file content.

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
# # chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# pidfile: /var/run/nginx.pid
# user: nginx # Source function library.
. /etc/rc.d/init.d/functions # Source networking configuration.
. /etc/sysconfig/network # Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx"
prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf"
lockfile=/var/run/nginx.lock start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval
} stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval
} restart() { configtest || return $? stop start
} reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo
} force_reload() { restart
} configtest() { $nginx -t -c $NGINX_CONF_FILE
} rh_status() { status $prog
} rh_status_q() { rh_status >/dev/null 2>&1
} case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2
esac
Add Nginx Init Script

Add Nginx Init Script

13. After the Nginx init file is created, append executions permissions and manage the daemon using the below command options.

# chmod +x /etc/init.d/nginx
# service nginx start|stop|restart|reload|force_reload|configtest|condrestart
# systemctl start|stop|restart nginx
Start Nginx Server

Start Nginx Server

14. If you need to enable Nginx system-wide use the following command to start at boot time.

# chkconfig nginx on OR # systemctl enable nginx

That’s all! Now you have latest version of Nginx installed on your RHEL/CentOS 7 system. On the next tutorial I will discuss how to install and enable PHP-FPM process manager though Nginx FastCGI Gateway.

Read Also: The Ultimate Guide to Secure, Harden and Improve Performance of Nginx Web Server

Posted by Web Monkey