Debian Nginx Module Development

Written by Peter Davies on .

Install the base Nginx package from source:

mkdir sources
cd sources
wget http://nginx.org/download/nginx-1.5.0.tar.gz
tar -zxvf nginx-1.5.0.tar.gz
cd nginx-1.5.0

You will also need the tools to compile source code:

apt-get install build-essential gcc make

You will also probably need PCRE:

apt-get install libpcre3 libpcre3-dev

And for SSL:

apt-get install libssl-dev

And finally:

apt-get install libgd2-noxpm libjpeg8 libxslt1.1

On debian nginx usually sits in the /usr/sbin/nginx ans we should really put this in the local folder:

./configure --sbin-path=/usr/local/sbin --with-http_ssl_module

Which eventually results in:

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: "/usr/local/nginx"
  nginx binary file: "/usr/local/sbin"
  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"

Now you can compile the Nginx source code:

make && make install

Create the init script (this was taken from a live packaged debian install, modified to match the new "user" locations):

#!/bin/sh

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

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

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

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

test_nginx_config() {
	if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
		return 0
	else
		$DAEMON -t $DAEMON_OPTS
		return $?
	fi
}

case "$1" in
	start)
		echo -n "Starting $DESC: "
		test_nginx_config
		# Check if the ULIMIT is set in /etc/default/nginx
		if [ -n "$ULIMIT" ]; then
			# Set the ulimits
			ulimit $ULIMIT
		fi
		start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
		    --exec $DAEMON -- $DAEMON_OPTS || true
		echo "$NAME."
		;;

	stop)
		echo -n "Stopping $DESC: "
		start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
		    --exec $DAEMON || true
		echo "$NAME."
		;;

	restart|force-reload)
		echo -n "Restarting $DESC: "
		start-stop-daemon --stop --quiet --pidfile \
		    /usr/local/nginx/logs/$NAME.pid --exec $DAEMON || true
		sleep 1
		test_nginx_config
		# Check if the ULIMIT is set in /etc/default/nginx
		if [ -n "$ULIMIT" ]; then
			# Set the ulimits
			ulimit $ULIMIT
		fi
		start-stop-daemon --start --quiet --pidfile \
		    /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
		echo "$NAME."
		;;

	reload)
		echo -n "Reloading $DESC configuration: "
		test_nginx_config
		start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
		    --exec $DAEMON || true
		echo "$NAME."
		;;

	configtest|testconfig)
		echo -n "Testing $DESC configuration: "
		if test_nginx_config; then
			echo "$NAME."
		else
			exit $?
		fi
		;;

	status)
		status_of_proc -p /usr/local/nginx/logs/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
		;;
	*)
		echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
		exit 1
		;;
esac

exit 0

Add to the start-up processes:

/usr/sbin/update-rc.d -f nginx defaults

Start Nginx by runnning:

/etc/init.d/nginx start

You can also check that Nginx is active by checking open ports:

netstat -tulpn

Now that we have a base Nginx application operational we need to compile our source code and append it to the build of the Nginx application. So we now run configure with the new --add-module parameter:

./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --add-module=/home/juicymedia/nginxhash

Now you need to recompile the Nginx source code:

make && make install

And restart Nginx: /etc/init.d/nginx restart

When you now visit the URL /md5/test you should see the output "this is a test" returned. We're all done! The next step of this tutorial will be to examine what's required to process passed parameters and interpret the data to output MD5 hash results.