Monday 25 June 2012

Compiling Nginx on Ubuntu (Lucid)

There is a faster way to get the latest versions of Nginx by using apt-get so if that’s all your trying to do then that may be a better route. However, if you want total control over your nginx instance, the ability to compile in third party modules and be 100% sure that you’re bang up to date with security updates then this is the way to go.

Build Essential

You’re going to need to install make & all her friends. The quickest route is to install via apt

 sudo apt-get install build-essential libssl-dev zlib1g-dev 

PCRE

You’re going to need to download and install the PCRE (perl compatible regular expressions) library. We’re going to do this into the /usr/local/ folder because we’re not using apt.

At the time of writing the latest version of the PCRE library was 8.30, so do make sure that this the latest if you’re reading this from the future –

 cd ~/src #make this if it doesn't exist wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz tar -xzvf pcre-8.30.tar.gz cd pcre-8.30/ ./configure # /usr/local is the default so no need to prefix make sudo make install sudo ldconfig # this is important otherwise nginx will compile but fail to load 

Nginx

Now onto the main event, go and grab yourself the latest stable (or development if that’s your thing) version from Nginx download page.

At the time of writing, the latest stable was 1.2.1 so download that unarchive it and get into the directory. As with PCRE if you’re reading this from the future then double check and make sure that you’re installing the latest version –

 cd ~/src wget http://nginx.org/download/nginx-1.2.1.tar.gz tar -xvzf nginx-1.2.1.tar.gz  cd nginx-1.2.1 

Now it’s time to configure. I’m going to do this with a minimal set of config flags: –

 ./configure --with-http_flv_module \ --with-http_ssl_module \ --with-http_gzip_static_module  

Now we can make and make install

 make sudo make install 

Now you’ll need to add the path to the nginx binary to your path in your .bashrc file (or whatever shell runtime config file you’re using) –

 export PATH="/usr/local/nginx/sbin:$PATH" 

Now reload your shell & start nginx.

 source ~/.bashrc sudo nginx 

If you go to http://yourmachine.co.uk you should now see the familiar “Welcome to Nginx” message. Now that you know it’s working stop nginx (because we’re going to control it via an init script) –

 sudo nginx -s stop 

We need to get nginx to load when the system boots. To to this we’re going to use an init script. This one was based on one from the fine humans at Linode. You can download this nginx init script directly and here it’s in all it’s glory –

 #! /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/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/local/nginx/sbin/nginx NAME=nginx DESC=nginx  test -x $DAEMON || exit 0  set -e  case "$1" in   start)         echo -n "Starting $DESC: "         start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \                 --exec $DAEMON -- $DAEMON_OPTS         echo "$NAME."         ;;   stop)         echo -n "Stopping $DESC: "         start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \                 --exec $DAEMON         echo "$NAME."         ;;   restart|force-reload)         echo -n "Restarting $DESC: "         start-stop-daemon --stop --quiet --pidfile \                 /usr/local/nginx/logs/$NAME.pid --exec $DAEMON         sleep 1         start-stop-daemon --start --quiet --pidfile \                 /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS         echo "$NAME."         ;;   reload)           echo -n "Reloading $DESC configuration: "           start-stop-daemon --stop --signal HUP --quiet --pidfile     /usr/local/nginx/logs/$NAME.pid \               --exec $DAEMON           echo "$NAME."           ;;       *)             N=/etc/init.d/$NAME             echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2             exit 1             ;;     esac      exit 0 

To get this init script installed and working you put it at /etc/init.d/nginx and then –

 # make it executable sudo chmod +x /etc/init.d/nginx # update the system rc daemon sudo /usr/sbin/update-rc.d -f nginx defaults 

Now you can start and stop nginx like any other apt installed daemon –

 # start it sudo /etc/init.d/nginx start # stop sudo /etc/init.d/nginx stop # restart sudo /etc/init.d/nginx restart # reload sudo /etc/init.d/nginx reload 

Assuming you’ve got no errors I’ll leave the actual configuration to your own needs.

That, Ladies and Gentleman, is how you compile and install Nginx on Ubuntu.