How To Compile Nginx From Source and Install on Raspbian Jessie

In this tutorial we are going to look at how to compile Nginx mainline version (1.9.12 at the time of this writting) on Raspbian Jessie OS.

Compile Nginx from Source

Download Nginx mainline version using wget

wget http://nginx.org/download/nginx-1.9.12.tar.gz

Extract the tar archive.

tar xvf nginx-1.9.12.tar.gz

cd to the newly-created nginx directory.

cd nginx-1.9.12/

In this directory there’s a shell script named configure. You can use file command to determine the type of a file like this:

file configure

Compile Nginx From Source and Install on Raspbian Jessie

Or you can use a text editor to open this configure file and check out what it has to offer.

Compile Nginx From Source and Install on Raspbian Jessie

You can see clearly that this is a shell script because the first line is #! /bin/sh.

Before the compilation, we need to install the utils and libraries needed for compilation and some extra Nginx modules.

sudo apt-get install -y curl build-essential make gcc libpcre3 libpcre3-dev libpcre++-dev zlib1g-dev libbz2-dev libxslt1-dev libxml2-dev libgd2-xpm-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libssl-dev libcurl4-openssl-dev

Now start the configuration. Here’s a common configuration arguments for Nginx mainline version.

./configure \
--prefix=/etc/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                                      \
--lock-path=/var/run/nginx.lock                                    \
--http-client-body-temp-path=/var/cache/nginx/client_temp          \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp                 \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp             \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp                 \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp                   \
--user=nginx                                                       \
--group=nginx                                                      \
--with-http_ssl_module                                             \
--with-http_realip_module                                          \
--with-http_addition_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_random_index_module                                    \
--with-http_secure_link_module                                     \
--with-http_stub_status_module                                     \
--with-http_auth_request_module                                    \
--with-mail                                                        \
--with-mail_ssl_module                                             \
--with-file-aio                                                    \
--with-http_v2_module                                              \
--with-ipv6                                                        \
--with-threads                                                     \
--with-stream                                                      \
--with-stream_ssl_module                                           \
--with-http_slice_module

Copy the above code in its entirety and paste it in the raspbian terminal, then hit Enter to start the configuration.

When the configuration process is finished, there will a new file named Makefile and a new directory named objs.

Now let’s compile it with make.

make

Then install it to your Raspberry Pi.

sudo make install

Create nginx user and group

sudo useradd -r nginx

Start Nginx Web server.

sudo nginx

If you see this error

nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)

Then manually create the file /var/cache/nginx/client_temp.

sudo mkdir /var/cache/nginx

sudo touch /var/cache/nginx/client_temp

And start it again. Naviagate your browser to the IP address of your Raspberry Pi. You should see the following.

Compile Nginx From Source and Install on Raspbian Jessie

The document root directory is found at /etc/nginx/html/.

Integrate with Systemd

Create a systemd service file for nginx

sudo vi /lib/systemd/system/nginx.service

and paste the following text into the file

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Save and close the file. Kill the nginx process.

sudo pkill nginx

Now start it with systemctl

sudo systemctl start nginx

Check status

sudo systmctl status nginx

Compile Nginx From Source and Install on Raspbian Jessie

Enable auto-start when system boots up.

sudo systemctl enable nginx

Congrats! You have successfully compiled Nginx from source, installed it on Raspbian Jessie and got it up and running with Systemd.

Rate this tutorial
[Total: 7 Average: 4.2]
  • Mauricio

    Awesome!!! This is a really excellent tutorial. It worked like a charm.
    I compiled & installed nginx v1.11.3 on my Raspberry Pi (Raspbian 8 jessie – armv6l Linux 4.4.34+) with the help of your guide.
    The only thing I could add is I needed to reboot before systemd could manage to start the nginx daemon.
    Now I’ll test nginx as a reverse proxy.
    Thank you very much for your help!