How to install Nginx from source on CentOS 6
To install Nginx from source on CentOS 6
Nginx, pronounced as " engine x," is an open source application which serves primarily as a reverse proxy server for HTTP/HTTPS and also for mail protocols such as SMTP, POP3, and IMAP. Also, it is an essential application for caching, load balancing, media streaming, and more. It is written using C, and is known for being light-weight and fast. Contrary to popular belief, it is so simple to install Nginx from source on CentOS 6, and in this tutorial, you will get to learn about it.
Installing Nginx
Installing and compiling Nginx from the source code requires the addition of certain dependencies in your system, so run the following command which installs them on your system.
[root@linuxhelp ~]# yum groupinstall " Development Tools" -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Group Process
Determining fastest mirrors
* base: mirror.vbctv.in
* extras: mirror.vbctv.in
* updates: mirror.vbctv.in
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 4.7 MB 00:00
Package 1:make-3.81-23.el6.x86_64 already installed and latest version
Package patch-2.6-6.el6.x86_64 already installed and latest version
Package 1:pkgconfig-0.23-9.1.el6.x86_64 already installed and latest version
Package gettext-0.17-18.el6.x86_64 already installed and latest version
Package elfutils-0.164-2.el6.x86_64 already installed and latest version
Package cvs-1.11.23-16.el6.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package autoconf.noarch 0:2.63-5.1.el6 will be installed
---> Package automake.noarch 0:1.11.1-4.el6 will be installed
---> Package binutils.x86_64 0:2.20.51.0.2-5.46.el6 will be updated
---> Package binutils.x86_64 0:2.20.51.0.2-5.47.el6_9.1 will be an update
---> Package bison.x86_64 0:2.4.1-5.el6 will be installed
.
.
.
Updated:
binutils.x86_64 0:2.20.51.0.2-5.47.el6_9.1
Complete!
Also, you should install other packages as well. So run the following command to complete this action.
[root@linuxhelp ~]# yum install zlib-devel pcre-devel openssl-devel -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirror.vbctv.in
* extras: mirror.vbctv.in
* updates: mirror.vbctv.in
Resolving Dependencies
--> Running transaction check
---> Package openssl-devel.x86_64 0:1.0.1e-57.el6 will be installed
--> Processing Dependency: krb5-devel for package: openssl-devel-1.0.1e-57.el6.x86_64
---> Package pcre-devel.x86_64 0:7.8-7.el6 will be installed
---> Package zlib-devel.x86_64 0:1.2.3-29.el6 will be installed
--> Running transaction check
---> Package krb5-devel.x86_64 0:1.10.3-65.el6 will be installed
.
.
.
Dependency Installed:
keyutils-libs-devel.x86_64 0:1.4-5.el6 krb5-devel.x86_64 0:1.10.3-65.el6 libcom_err-devel.x86_64 0:1.41.12-23.el6 libselinux-devel.x86_64 0:2.0.94-7.el6 libsepol-devel.x86_64 0:2.0.41-4.el6
Complete!
Once all the above steps are completed, you can download Nginx source from the following link:
If you want to download the source from your terminal, you can simply run the following command.
[root@linuxhelp ~]# wget http://nginx.org/download/nginx-1.9.9.tar.gz
--2017-10-17 23:11:26-- http://nginx.org/download/nginx-1.9.9.tar.gz
Resolving nginx.org... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3, ...
Connecting to nginx.org|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 887908 (867K) [application/octet-stream]
Saving to: “ nginx-1.9.9.tar.gz”
100%[==========================================================================================================================================================================> ] 887,908 --.-K/s in 0.02s
2017-10-17 23:11:46 (41.1 MB/s) - “ nginx-1.9.9.tar.gz” saved [887908/887908]
After the download is completed, extract the package as follows.
[root@linuxhelp ~]# tar -xzf nginx-1.9.9.tar.gz
Now we can compile and install Nginx, for that you should go to nginx directory, and configure the script file in it. It should be run before installing nginx which will configure the installation with some options we need to mention. To see all the options, use the command given below.
[root@linuxhelp nginx-1.9.9]# ./configure &ndash help
Now, you can install nginx on default location /usr/local/nginx, for that use the following command or enter your own directory path.
[root@linuxhelp nginx-1.9.9]# ./configure --prefix=/usr/local/nginx
checking for OS
+ Linux 2.6.32-696.el6.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
checking for gcc -pipe switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... 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 zlib library ... found
creating objs/Makefile
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: " /usr/local/nginx"
nginx binary file: " /usr/local/nginx/sbin/nginx"
nginx configuration prefix: " /usr/local/nginx/conf"
nginx configuration file: " /usr/local/nginx/conf/nginx.conf"
nginx pid file: " /usr/local/nginx/logs/nginx.pid"
nginx error log file: " /usr/local/nginx/logs/error.log"
nginx http access log file: " /usr/local/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
Once the configuration is finished run make command as follows.
[root@linuxhelp nginx-1.9.9]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.9.9'
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
.
.
.
make[1]: Leaving directory `/root/nginx-1.9.9'
make -f objs/Makefile manpage
make[1]: Entering directory `/root/nginx-1.9.9'
sed -e " s|%%PREFIX%%|/usr/local/nginx|"
-e " s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|"
-e " s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|"
-e " s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|"
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/root/nginx-1.9.9'
Later, run make install command to finish nginx installation.
[root@linuxhelp nginx-1.9.9]# make install
make -f objs/Makefile install
make[1]: Entering directory `/root/nginx-1.9.9'
test -d ' /usr/local/nginx' || mkdir -p ' /usr/local/nginx'
test -d ' /usr/local/nginx/sbin' || mkdir -p ' /usr/local/nginx/sbin'
test ! -f ' /usr/local/nginx/sbin/nginx' || mv ' /usr/local/nginx/sbin/nginx' ' /usr/local/nginx/sbin/nginx.old'
cp objs/nginx ' /usr/local/nginx/sbin/nginx'
test -d ' /usr/local/nginx/conf' || mkdir -p ' /usr/local/nginx/conf'
cp conf/koi-win ' /usr/local/nginx/conf'
cp conf/koi-utf ' /usr/local/nginx/conf'
cp conf/win-utf ' /usr/local/nginx/conf'
test -f ' /usr/local/nginx/conf/mime.types'
.
.
.
test -f ' /usr/local/nginx/conf/nginx.conf' || cp conf/nginx.conf ' /usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf ' /usr/local/nginx/conf/nginx.conf.default'
test -d ' /usr/local/nginx/logs' || mkdir -p ' /usr/local/nginx/logs'
test -d ' /usr/local/nginx/logs' || mkdir -p ' /usr/local/nginx/logs'
test -d ' /usr/local/nginx/html' || cp -R html ' /usr/local/nginx'
test -d ' /usr/local/nginx/logs' || mkdir -p ' /usr/local/nginx/logs'
make[1]: Leaving directory `/root/nginx-1.9.9'
After finishing the installation, you can find nginx in /usr/local/nginx/, there you can find configuration file, document root etc. You can now create a user for nginx as follows.
[root@linuxhelp ~]# useradd -r nginx
To manage nginx service with a command, we need to configure service daemon for nginx. So in CentOS 6 distro, we need to create a init script for nginx in /etc/init.d/ directory.
[root@linuxhelp ~]# vim /etc/init.d/nginx
Once the init file is created, make the following changes in it.
Entry:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# 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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE=" /usr/local/nginx/conf/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
create above entry in /etc/init.d/nginx file which is simply a bash script to manage nginx service. You need to change the daemon file location and configuration file location according to where you have installed nginx on your machine.
nginx=" /usr/local/nginx/sbin/nginx" NGINX_CONF_FILE=" /usr/local/nginx/conf/nginx.conf"
Now, you need to edit the path of above two lines in that script file in case the installation location has changed. Now set execute file permission for nginx init script.
[root@linuxhelp ~]# chmod +x /etc/init.d/nginx
Once it is done, start nginx service.
[root@linuxhelp ~]# service nginx start Starting nginx: [ OK ] [root@linuxhelp ~]# chkconfig nginx on
Finally, open the browser and see the output.

With this, the installation of Nginx with source code on CentOS comes to an end.
Comments ( 1 )
/etc/init.d/nginx: line 30: syntax error near unexpected token `&'
/etc/init.d/nginx: line 30: `[" $NETWORKING" = "no"] & & exit 0'