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.
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.
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.
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.
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
1#vi /etc/init.d/nginx
And paste the below given code
1#! /bin/bash2. /lib/lsb/init-functions3#------------------------------------------------------------------------------4# Consts5#------------------------------------------------------------------------------6PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/sbin/7DAEMON=/usr/sbin/nginx8PS="nginx"9PIDNAME="nginx" #lets you do $PS-slave10PIDFILE=$PIDNAME.pid #pid file11PIDSPATH=/var/run12DESCRIPTION="Nginx Server version 1.6.2"13RUNAS=root #user to run as14SCRIPT_OK=0 #ala error codes15SCRIPT_ERROR=1 #ala error codes16TRUE=1 #boolean17FALSE=0 #boolean18lockfile=/var/lock/subsys/nginx19NGINX_CONF_FILE="/etc/nginx/nginx.conf"20#------------------------------------------------------------------------------21# Simple Tests22#------------------------------------------------------------------------------23#test if nginx is a file and executable24test -x $DAEMON || exit 025# Include nginx defaults if available26if [ -f /etc/default/nginx ] ; then27 . /etc/default/nginx28fi29#set exit condition30#set -e31#------------------------------------------------------------------------------32# Functions33#------------------------------------------------------------------------------34setFilePerms(){35 if [ -f $PIDSPATH/$PIDFILE ]; then36 chmod 400 $PIDSPATH/$PIDFILE37 fi38}39configtest() {40 $DAEMON -t -c $NGINX_CONF_FILE41}42getPSCount() {43 return `pgrep -f $PS | wc -l`44}45isRunning() {46 if [ $1 ]; then47 pidof_daemon $148 PID=$?49 if [ $PID -gt 0 ]; then50 return 151 else52 return 053 fi54 else55 pidof_daemon56 PID=$?57 if [ $PID -gt 0 ]; then58 return 159 else60 return 061 fi62 fi63}64#courtesy of php-fpm65wait_for_pid () {66 try=067 while test $try -lt 35 ; do68 case "$1" in69 'created')70 if [ -f "$2" ] ; then71 try=''72 break73 fi74 ;;75 'removed')76 if [ ! -f "$2" ] ; then77 try=''78 break79 fi80 ;;81 esac82 #echo -n .83 try=`expr $try + 1`84 sleep 185 done86}87status(){88 isRunning89 isAlive=$?90 if [ "${isAlive}" -eq $TRUE ]; then91 echo "$PIDNAME found running with processes: `pidof $PS`"92 else93 echo "$PIDNAME is NOT running."94 fi95}96removePIDFile(){97 if [ $1 ]; then98 if [ -f $1 ]; then99 rm -f $1100 fi101 else102 #Do default removal103 if [ -f $PIDSPATH/$PIDFILE ]; then104 rm -f $PIDSPATH/$PIDFILE105 fi106 fi107}108start() {109 log_daemon_msg "Starting $DESCRIPTION"110 isRunning111 isAlive=$?112 if [ "${isAlive}" -eq $TRUE ]; then113 log_end_msg $SCRIPT_ERROR114 else115 start-stop-daemon --start --quiet --chuid $RUNAS --pidfile $PIDSPATH/$PIDFILE --exec $DAEMON \116 -- -c $NGINX_CONF_FILE117 setFilePerms118 log_end_msg $SCRIPT_OK119 fi120}121stop() {122 log_daemon_msg "Stopping $DESCRIPTION"123 isRunning124 isAlive=$?125 if [ "${isAlive}" -eq $TRUE ]; then126 start-stop-daemon --stop --quiet --pidfile $PIDSPATH/$PIDFILE127 wait_for_pid 'removed' $PIDSPATH/$PIDFILE128 if [ -n "$try" ] ; then129 log_end_msg $SCRIPT_ERROR130 else131 removePIDFile132 log_end_msg $SCRIPT_OK133 fi134 else135 log_end_msg $SCRIPT_ERROR136 fi137}138reload() {139 configtest || return $?140 log_daemon_msg "Reloading (via HUP) $DESCRIPTION"141 isRunning142 if [ $? -eq $TRUE ]; then143 `killall -HUP $PS` #to be safe144 log_end_msg $SCRIPT_OK145 else146 log_end_msg $SCRIPT_ERROR147 fi148}149quietupgrade() {150 log_daemon_msg "Peforming Quiet Upgrade $DESCRIPTION"151 isRunning152 isAlive=$?153 if [ "${isAlive}" -eq $TRUE ]; then154 kill -USR2 `cat $PIDSPATH/$PIDFILE`155 kill -WINCH `cat $PIDSPATH/$PIDFILE.oldbin`156 isRunning157 isAlive=$?158 if [ "${isAlive}" -eq $TRUE ]; then159 kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`160 wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin161 removePIDFile $PIDSPATH/$PIDFILE.oldbin162 log_end_msg $SCRIPT_OK163 else164 log_end_msg $SCRIPT_ERROR165 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.oldbin170 removePIDFile $PIDSPATH/$PIDFILE.oldbin171 log_end_msg $SCRIPT_ok172 fi173 else174 log_end_msg $SCRIPT_ERROR175 fi176}177terminate() {178 log_daemon_msg "Force terminating (via KILL) $DESCRIPTION"179 PIDS=`pidof $PS` || true180 [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`181 for i in $PIDS; do182 if [ "$i" = "$PIDS2" ]; then183 kill $i184 wait_for_pid 'removed' $PIDSPATH/$PIDFILE185 removePIDFile186 fi187 done188 log_end_msg $SCRIPT_OK189}190destroy() {191 log_daemon_msg "Force terminating and may include self (via KILLALL) $DESCRIPTION"192 killall $PS -q >> /dev/null 2>&1193 log_end_msg $SCRIPT_OK194}195pidof_daemon() {196 PIDS=`pidof $PS` || true197 [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`198 for i in $PIDS; do199 if [ "$i" = "$PIDS2" ]; then200 return 1201 fi202 done203 return 0204}205case "$1" in206 start)207 start208 ;;209 stop)210 stop211 ;;212 restart|force-reload)213 stop214 sleep 1215 start216 ;;217 reload)218 $1219 ;;220 status)221 status222 ;;223 configtest)224 $1225 ;;226 quietupgrade)227 $1228 ;;229 terminate)230 $1231 ;;232 destroy)233 $1234 ;;235 *)236 FULLPATH=/etc/init.d/$PS237 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 1240 ;;241esac242exit 0
Save and exit from file.
To make it executable, give the executable permission to nginx daemon file.
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 –
1#/etc/init.d/nginx start
To stop the nginx service, use the command –
1#/etc/init.d/nginx stop
To check status the nginx service, use the command –
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.
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.



