Install nginx from source code

Beginner 1

Introduction

This guide is about setting up a Nginx server from the source code. Nginx has two options, the mainline and stable. The stable will lack some features, but it will have all the bugfixes and is the right one for production. The mainline options is realiable enough and has more features than stable, however it could contain new bugs.

Step 1: Requirements

apt-get build-dep nginx

Step 2: Installation

We are going to download the stable version of nginx 1.10.2.

wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar zxf nginx-1.10.2.tar.gz
cd nginx-1.10.2

Step 3: Prepare source code for compilation

Next we are going to configure the source code with common options. In some cases here we could add third party modules to nginx or more options like "--with-threads" to enable thread pools usage.

./configure --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-http_ssl_module \
    --with-stream \
    --with-mail=dynamic

If everything was ok, next steps are to compile the code.

make
sudo make install

Step 4: User and boot script

To have a secure setting we need to execute the nginx process with a non-root user. In this case we are going to use the "nginx" user. When the user is correcly created, we will use the /etc/init.d/nginx script to have nginx running at boot.

sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx

The following is the context that we are going to use in /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/nginx:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/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 /opt/nginx/logs/$NAME.pid \
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \
                --exec $DAEMON
        echo "$NAME."
        ;;
  restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
                /opt/nginx/logs/$NAME.pid --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile \
                /opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  reload)
          echo -n "Reloading $DESC configuration: "
          start-stop-daemon --stop --signal HUP --quiet --pidfile     /opt/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

Let's make the script execute and add install it to the System-V style init script.

sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults

Step 5: add sites directories

Create the virtualhost directories

mkdir -p /etc/nginx/sites-enabled
mkdir -p /etc/nginx/sites-available

edit /etc/nginx/nginx.conf and the line at the end of the http block:

include /etc/nginx/sites-enabled/*;

Step 6: Start nginx!

Now we are ready to start nginx.

sudo service nginx start

Make sure nginx is running with:

ps ax | grep nginx

Or use a browser to the server IP address.

Appendix

ERROR: You must put some 'source' URIs in your sources.list

You need to add to you /etc/apt/sources.list a source URI. Open the sources.list file and edit all the lines that start with deb-src and then execute another sudo apt-get update.