Normally you will install Nginx using apt-get install nginx, but in some scenarios we may need to install Nginx from source on Ubuntu. The reason could be we want to either install nginx with new custom nginx module which is not there in base nginx version or we don't want to use all the modules available by default in nginx. In this article we are going to delve into it. Please remember if we go this route of installing from source, going forward you need to do compile only if you need to add one more package or upgrade nginx.

Follow the given below steps to install Nginx from source code on Ubuntu 14.04 LTS server.

Step-1

login as SUDO User

login as super user on system.

Plain text
1#sudo -i

Step-2

Download the Nginx source code from NGINX official site.

Always use latest available stable version. You can download the nginx source code from Nginx.org download page.

Here we are using curl command to download the current available latest package.

curl -O http://nginx.org/download/nginx-1.6.2.tar.gz

Step-3

Add nginx user to run NGINX demon in server.

Command to add nginx user with nologin shell.

Plain text
1#useradd -s /sbin/nologin nginx

Step-4

Install dependency packages

Installing  all the dependency packages and NGINX modules.

apt-get install build-essential libc6 libpcre3 libpcre3-dev libpcrecpp0 libssl0.9.8 libssl-dev zlib1g zlib1g-dev lsb-base openssl libssl-dev  libgeoip1 libgeoip-dev  google-perftools libgoogle-perftools-dev libperl-dev  libgd2-xpm-dev libatomic-ops-dev libxml2-dev libxslt1-dev python-dev

Step-5

Extract NGINX source code

User tar command to extract the downloaded nginx source code.

Plain text
1#tar -xvzf nginx-1.6.2.tar.gz

It will extract out the Nginx directory.

Compiling NGINX source code

First change to directory extracted out from nginx tar ball.

Plain text
1#cd nginx-1.6.2/

Step-6

Now compile the Nginx source code.

#./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 --pid-path=/var/run/nginx.pid  --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"

Install NGINX with below command

#make && make install

The nginx installation is successfully competed.

Now we have to create nginx daemon file which will be used for nginx service start,stop,status,reload,restart .

Step-7

Create Nginx Daemon file

Create a new file called nginx inside /etc/init.d

Plain text
1#vi /etc/init.d/nginx

And paste the below given code

JavaScript
1#! /bin/bash
2. /lib/lsb/init-functions
3#------------------------------------------------------------------------------
4# Consts
5#------------------------------------------------------------------------------
6PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/sbin/
7DAEMON=/usr/sbin/nginx
8PS="nginx"
9PIDNAME="nginx" #lets you do $PS-slave
10PIDFILE=$PIDNAME.pid #pid file
11PIDSPATH=/var/run
12DESCRIPTION="Nginx Server version 1.6.2"
13RUNAS=root #user to run as
14SCRIPT_OK=0 #ala error codes
15SCRIPT_ERROR=1 #ala error codes
16TRUE=1 #boolean
17FALSE=0 #boolean
18lockfile=/var/lock/subsys/nginx
19NGINX_CONF_FILE="/etc/nginx/nginx.conf"
20#------------------------------------------------------------------------------
21# Simple Tests
22#------------------------------------------------------------------------------
23#test if nginx is a file and executable
24test -x $DAEMON || exit 0
25# Include nginx defaults if available
26if [ -f /etc/default/nginx ] ; then
27 . /etc/default/nginx
28fi
29#set exit condition
30#set -e
31#------------------------------------------------------------------------------
32# Functions
33#------------------------------------------------------------------------------
34setFilePerms(){
35 if [ -f $PIDSPATH/$PIDFILE ]; then
36 chmod 400 $PIDSPATH/$PIDFILE
37 fi
38}
39configtest() {
40 $DAEMON -t -c $NGINX_CONF_FILE
41}
42getPSCount() {
43 return `pgrep -f $PS | wc -l`
44}
45isRunning() {
46 if [ $1 ]; then
47 pidof_daemon $1
48 PID=$?
49 if [ $PID -gt 0 ]; then
50 return 1
51 else
52 return 0
53 fi
54 else
55 pidof_daemon
56 PID=$?
57 if [ $PID -gt 0 ]; then
58 return 1
59 else
60 return 0
61 fi
62 fi
63}
64#courtesy of php-fpm
65wait_for_pid () {
66 try=0
67 while test $try -lt 35 ; do
68 case "$1" in
69 'created')
70 if [ -f "$2" ] ; then
71 try=''
72 break
73 fi
74 ;;
75 'removed')
76 if [ ! -f "$2" ] ; then
77 try=''
78 break
79 fi
80 ;;
81 esac
82 #echo -n .
83 try=`expr $try + 1`
84 sleep 1
85 done
86}
87status(){
88 isRunning
89 isAlive=$?
90 if [ "${isAlive}" -eq $TRUE ]; then
91 echo "$PIDNAME found running with processes: `pidof $PS`"
92 else
93 echo "$PIDNAME is NOT running."
94 fi
95}
96removePIDFile(){
97 if [ $1 ]; then
98 if [ -f $1 ]; then
99 rm -f $1
100 fi
101 else
102 #Do default removal
103 if [ -f $PIDSPATH/$PIDFILE ]; then
104 rm -f $PIDSPATH/$PIDFILE
105 fi
106 fi
107}
108start() {
109 log_daemon_msg "Starting $DESCRIPTION"
110 isRunning
111 isAlive=$?
112 if [ "${isAlive}" -eq $TRUE ]; then
113 log_end_msg $SCRIPT_ERROR
114 else
115 start-stop-daemon --start --quiet --chuid $RUNAS --pidfile $PIDSPATH/$PIDFILE --exec $DAEMON \
116 -- -c $NGINX_CONF_FILE
117 setFilePerms
118 log_end_msg $SCRIPT_OK
119 fi
120}
121stop() {
122 log_daemon_msg "Stopping $DESCRIPTION"
123 isRunning
124 isAlive=$?
125 if [ "${isAlive}" -eq $TRUE ]; then
126 start-stop-daemon --stop --quiet --pidfile $PIDSPATH/$PIDFILE
127 wait_for_pid 'removed' $PIDSPATH/$PIDFILE
128 if [ -n "$try" ] ; then
129 log_end_msg $SCRIPT_ERROR
130 else
131 removePIDFile
132 log_end_msg $SCRIPT_OK
133 fi
134 else
135 log_end_msg $SCRIPT_ERROR
136 fi
137}
138reload() {
139 configtest || return $?
140 log_daemon_msg "Reloading (via HUP) $DESCRIPTION"
141 isRunning
142 if [ $? -eq $TRUE ]; then
143 `killall -HUP $PS` #to be safe
144 log_end_msg $SCRIPT_OK
145 else
146 log_end_msg $SCRIPT_ERROR
147 fi
148}
149quietupgrade() {
150 log_daemon_msg "Peforming Quiet Upgrade $DESCRIPTION"
151 isRunning
152 isAlive=$?
153 if [ "${isAlive}" -eq $TRUE ]; then
154 kill -USR2 `cat $PIDSPATH/$PIDFILE`
155 kill -WINCH `cat $PIDSPATH/$PIDFILE.oldbin`
156 isRunning
157 isAlive=$?
158 if [ "${isAlive}" -eq $TRUE ]; then
159 kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`
160 wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
161 removePIDFile $PIDSPATH/$PIDFILE.oldbin
162 log_end_msg $SCRIPT_OK
163 else
164 log_end_msg $SCRIPT_ERROR
165 log_daemon_msg "ERROR! Reverting back to original $DESCRIPTION"
166 kill -HUP `cat $PIDSPATH/$PIDFILE`
167 kill -TERM `cat $PIDSPATH/$PIDFILE.oldbin`
168 kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`
169 wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
170 removePIDFile $PIDSPATH/$PIDFILE.oldbin
171 log_end_msg $SCRIPT_ok
172 fi
173 else
174 log_end_msg $SCRIPT_ERROR
175 fi
176}
177terminate() {
178 log_daemon_msg "Force terminating (via KILL) $DESCRIPTION"
179 PIDS=`pidof $PS` || true
180 [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`
181 for i in $PIDS; do
182 if [ "$i" = "$PIDS2" ]; then
183 kill $i
184 wait_for_pid 'removed' $PIDSPATH/$PIDFILE
185 removePIDFile
186 fi
187 done
188 log_end_msg $SCRIPT_OK
189}
190destroy() {
191 log_daemon_msg "Force terminating and may include self (via KILLALL) $DESCRIPTION"
192 killall $PS -q >> /dev/null 2>&1
193 log_end_msg $SCRIPT_OK
194}
195pidof_daemon() {
196 PIDS=`pidof $PS` || true
197 [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`
198 for i in $PIDS; do
199 if [ "$i" = "$PIDS2" ]; then
200 return 1
201 fi
202 done
203 return 0
204}
205case "$1" in
206 start)
207 start
208 ;;
209 stop)
210 stop
211 ;;
212 restart|force-reload)
213 stop
214 sleep 1
215 start
216 ;;
217 reload)
218 $1
219 ;;
220 status)
221 status
222 ;;
223 configtest)
224 $1
225 ;;
226 quietupgrade)
227 $1
228 ;;
229 terminate)
230 $1
231 ;;
232 destroy)
233 $1
234 ;;
235 *)
236 FULLPATH=/etc/init.d/$PS
237 echo "Usage: $FULLPATH {start|stop|restart|force-reload|status|configtest|quietupgrade|terminate|destroy}"
238 echo " The 'destroy' command should only be used as a last resort."
239 exit 1
240 ;;
241esac
242exit 0

Save and exit from file.

To make it executable, give the executable permission to nginx daemon file.

Plain text
1#chmod +x /etc/init.d/nginx

Step-8

How to start/stop/status of Nginx service

To start the nginx service, use the command –

Plain text
1#/etc/init.d/nginx start

To stop the nginx service, use the command –

Plain text
1#/etc/init.d/nginx stop

To check status the nginx service, use the command –

Plain text
1#/etc/init.d/nginx status

Use –help argument , to know about more usage of nginx daemon script . Given below is reference from our system.

Plain text
1# /etc/init.d/nginx --help

Usage: /etc/init.d/nginx {start|stop|restart|force-reload|status|configtest|quietupgrade|terminate|destroy}

Step-9

Check Web Server

To check web server accessible on port 80, open the web browser and type the ip address or hostname of web server in URL field.

http://IPADDRESS_OF_NGINX_SERVER

It will display the default WELCOME page.

That's it, we are done with NGINX source code installation with custom modules.