Compiling NginX on Debian / Ubuntu

on September 7, 2009 in Linux with 3 comments by

I really like the efficiency and simplicity of the NginX web server (pron.: “Engine X”). In one of my previous blurbs, “NginX and Apache, but no memcached”, I gave some numbers on how well it performed on a no-frills server.

For Debian and Ubuntu users installing NginX couldn’t be easier. Simply issue the command  apt-get install nginx command and do some basic configuration.

However, the version availble in the Debian and Ubuntu’s package repositories has not been compiled with IPv6 support. If you need this, or if you prefer to use bleeding-edge technology, then compiling NginX is the solution.

Personally I prefer to install NginX from the Debian/Ubuntu package repository before I compile the source code from NginX; this so that I have the proper directory layouts and an official init script. If you prefer a pure source code install, then not to worry as this will cover both.

Required library packages

Other than the compiler you also need a few libraries installed, such as the zlib and libssl. The required libraries and the compiler can be obtained with the following command:

aptitude -y install build-essential libc6 libpcre3 libpcre3-dev libpcrecpp0 libssl0.9.8 libssl-dev zlib1g zlib1g-dev lsb-base

Obtain the latest source code

Be sure to check NginX’s official website for the latest stable version  of NginX, currently 0.7.61. Download it using the following command:

wget http://sysoev.ru/nginx/nginx-0.7.61.tar.gz -P /usr/src

This will place the file in the /usr/src directory. Let’s untar it:

cd /usr/src
tar -xvf nginx-0.7.61.tar.gz
cd nginx-0.7.61

Configure NginX compilate-time options

To see which compile-time options are available, type

./configure --help

The configuration I prefer to use keeps in line with Debian / Ubuntu’s default directory layouts, provides support for IPv6, SSL (https://), WebDAV and Streaming FLV:

./configure --sbin-path=/usr/sbin --conf-path=/etc/nginx/nginx.conf
 --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid
 --lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log
 --http-client-body-temp-path=/var/lib/nginx/body
 --http-proxy-temp-path=/var/lib/nginx/proxy
 --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug
 --with-http_stub_status_module --with-http_flv_module --with-http_ssl_module
 --with-http_dav_module --with-ipv6

This will display various “checking” messages, but the important bit is that it reaches the following summary:

Configuration summary
 + using system PCRE library
 + using system OpenSSL library
 + md5: using OpenSSL library
 + sha1 library is not used
 + using system zlib library

 nginx path prefix: "/usr/local/nginx"
 nginx binary file: "/usr/sbin"
 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: "/var/lib/nginx/body"
 nginx http proxy temporary files: "/var/lib/nginx/proxy"
 nginx http fastcgi temporary files: "/var/lib/nginx/fastcgi"

Compile and install NginX

You are now ready to compile and install NginX by issuing the following commands:

make && make install

If all everything compiled without error, you should be able to type the command:

nginx -v

And the result would be:

nginx version: nginx/0.7.61

If you have installed NginX through the Debian or Ubuntu package repositories prior, you will need to restart NginX:

/etc/init.d/nginx restart

However, if you are using a source-only install then you would likely need the init script as well. Following is the init script courtesy Debian, which you need to create at /etc/init.d/nginx :

#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi

set -e

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid
                --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid
                --exec $DAEMON || true
        echo "$NAME."
        ;;
  restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile
                /var/run/$NAME.pid --exec $DAEMON || true
        sleep 1
        start-stop-daemon --start --quiet --pidfile
                /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
        ;;
  reload)
        echo -n "Reloading $DESC configuration: "
        start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid
            --exec $DAEMON || true
        echo "$NAME."
        ;;
  configtest)
        echo -n "Testing $DESC configuration: "
        if nginx -t > /dev/null 2>&1
        then
          echo "$NAME."
        else
          exit $?
        fi
        ;;
  *)
        echo "Usage: $NAME {start|stop|restart|reload|force-reload|configtest}" >&2
        exit 1
        ;;
esac

exit 0

Remember to make the init script executable:

chmod +x /etc/init/nginx

And if you wish to start NginX automatically at boot time:

update-rc.d nginx defaults

All that’s left is configuring NginX in the /etc/nginx/ directory. You can refer to the NginX Wiki for further assistance. Enjoy!

3 comments

  1. posted on Jul 06, 2011 at 8:31 PM  |  reply

    Thanks, this was useful.

    Slight typo at “chmod +x /etc/init/nginx”, should be “…init.d/…”

  2. posted on Mar 22, 2010 at 10:19 AM  |  reply

    […] you’ll want to compile it yourself, not to worry, it’s very straightforward thanks to Myatu’s excellent tutorial. I’d highly reccomend installing the Debian Nginx v6 package first in order to get the init […]

  3. posted on Mar 20, 2010 at 10:15 AM  |  reply

    Thank you :)

    That was soooo easy!

Join the discussion

Your email address will not be published. Required fields are marked *