Skip to content

Downloading and Installing Nginx

Posted on:July 5, 2015

The first in a series of Nginx posts. The information here is heavily inspired from my reading of Clément Nedelcu’s excellent book Nginx HTTP Server and can be considered as my own personal reference.

There are five main steps involved in installing Nginx, as outlined below:

  1. Install dependencies
  2. Download
  3. Configure
  4. Compile
  5. Install

Nginx should be compiled from source, because:

  1. It may not be available in the enabled repositories of your Linux distro,
  2. Even if it is, it’s often an outdated version, and
  3. certain options and flags can only be configured at compile time.

1. Install dependencies

Nginx dependencies vary, according to the modules you require. Core dependencies include:

GNU Compiler Collection (GCC)

Nginx is written in C, so we need a C compiler.

It’s quite likely that you already have GCC installed on your system. Test if gcc is installed:

$ gcc

Success: gcc: no input files Failure: gcc: command not found

To install it:

$ apt-get install build-essential

Perl Compatible Regular Expression (PCRE)

The Rewrite and HTTP Core modules need PCRE for parsing their regular expressions. We need to install both the library and its source: pcre and pcre-devel:

To install it:

$ apt-get install libpcre3 libpcre3-dev

zlib

The zlib library contains compression algorithms and is required in Nginx for gzip compression. Like PCRE, we need both the library and its source.

To install it:

$ apt-get install zlib1g zlib1g-dev

OpenSSL

Nginx relies on OpenSSL to serve secure pages.

To install it:

$ apt-get install openssl libssl-dev

2. Download

Nginx has three main branches: stable, mainline and legacy. It’s generally fine to use the mainline branch.

Download and extract Nginx onto a directory of your choice:

$ wget http://nginx.org/download/nginx-1.9.2.tar.gz
$ tar zxf nginx-1.9.2.tar.gz

3. Configure

The configuration process consists of appending switches to the ./configure command. Some of these options affect the project binaries and cannot be changed post-compilation.

objs/autoconf.err contains configuration error logs.

Below is a brief overview of the available configuration switches.

Path Options

Configuration that tells Nginx where to put things.

--prefix=

Default: /usr/local/nginx

Specify an absolute path on which to install Nginx to. If a relative path is specified, it will be taken as relative to /usr/local/nginx.

Paths can also be specified for the Nginx binary (--sbin-path=), the main configuration file (--conf-path=), the pid file1 (--pid-path=), the lock file2 (--lock-path=), fallback log files (--http-log-path=, --error-log-path=), as well as paths where Nginx should look for its dependencies.

Since the default --prefix path does not include a version number, upgrading Nginx will override the existing directory. It is recommended to override --prefix to include a version number, like this: --prefix=/usr/local/nginx-1.9.2, then create a symbolic link /usr/local/nginx to point to the latest versioned Nginx folder.

Make sure the path specified by --prefix= exists and is read/writable by the user running the configuration and compilation.

Prerequisites Options

Configuration that tells Nginx where to look for dependencies and how to build them. You will need to configure this if you install dependencies in non-standard locations.

For example, to configure an alternative path for the C compiler: --with-cc=, or to specify options for building zlib: --with-zlib-opt=.

Module Options

Configuration that tells Nginx which modules to include and exclude. Nginx has a modular architecture, which allows you to pick which functionalities you need easily.

Here, I split Nginx modules by whether they are enabled or disabled by default.

Modules Enabled By Default

To disable these modules, the switch is of the syntax --without-http_<MODULE_NAME>_module, for example --without-http_rewrite_module.

Modules Disabled By Default

To enable these modules, the switch is of the syntax --with-http_<MODULE_NAME>_module, for example --with-http_xslt_module.

There are also third-party modules can be added on. --add-module=path will compile the module located at path into the binary.

Miscellaneous Options

There are some options that don’t fall in the above categories, mostly concerning mail server proxy or event management behavior.

The most notable options that fall under this category are --user= and --group=, which specifies the user and group for starting Nginx worker processes.

Running a ./configure --with-http_ssl_module --with-http_gzip_static_module produces some output that looks like this:

$ ./configure --with-http_ssl_module --with-http_gzip_static_module

checking for OS
 + Linux 3.13.0-52-generic x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04)
checking for gcc -pipe switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found

...(redacted)-...

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/nginx/sbin/nginx"
  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"

4. Compile

Once the ./configure command is executed successfully, a makefile is generated in the same directory, which can be used to compile Nginx by running make:

$ make

Success: make[1]: leaving directory...

Any errors that occur here may be due to incorrect folder permissions or missing/too-recent/too-old dependencies.

5. Install

$ make install

The command will copy the compiled files as well as other resources to the installation directory specified by --prefix. This may require root privileges, depending on the folder permissions for /usr/local.

This step rarely presents problems.

Next, we will look at how to control the Nginx demon, oops I meant daemon.

Footnotes

  1. The pid file is a text file that contains the process ID.

  2. The lock file allows other programs to check if Nginx is already running and prevent Nginx from being started more than once.