This article explains how to install NGINX Open Source.

Table of Contents

Choosing Between a Stable or a Mainline Version

NGINX Open Source is available in two versions:

Choosing Between a Pre-built Package or Compiling From the Sources

Both NGINX Open Source mainline and stable versions can be installed in two ways:

Compiling and Installing From the Sources

Compiling NGINX from the sources provides you with more flexibility: you can add particular NGINX modules or 3rd party modules and apply latest security patches.

Installing NGINX Dependencies

Prior to compiling NGINX from the sources, it is necessary to install its dependencies:

Downloading the Sources

NGINX provides source files for both stable and mainline versions. The source files can be downloaded from the NGINX Open Source download page at http://www.nginx.org/en/download.html.

To download and unpack source files for the latest mainline version, type-in the commands:

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

To download and unpack source files for the latest stable version, type-in the commands:

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

Configuring the Build Options

Configure options are specified with the ./configure script that sets up various NGINX parameters including paths to source and configuration files, compiler options, connection processing methods and the list of modules. The script ends up with creation of the Makefile required to compile the code and install NGINX.

Example of the configure script:

$ ./configure
--sbin-path=/usr/local/nginx/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-path=/usr/local/nginx/nginx.pid
--with-pcre=../pcre-8.39
--with-zlib=../zlib-1.2.8
--with-http_ssl_module
--with-stream
--with-mail=dynamic
--add-module=/usr/build/nginx-rtmp-module
--add-dynamic-module=/usr/build/3party_module

Configuring NGINX Paths

The configure script allows you to set paths to NIGNX binary and configuration files, and to dependent libraries such as PCRE or SSL in order to link them statically to NGINX binary.

Configuring NGINX GCC Options

In the configure script you can also specify compiler-related options.

Specifying NGINX Connection Processing Methods

In the configure script you can redefine an event-based polling method. See Connection Processing Methods for details.

NGINX Modules

NGINX consists of modules. The set of modules as well as other build options are configured with the ./configure script.

Some modules are built by default – they do not require to be specified in the configure script. However, any default module can be excluded from the NGINX binary with the --without- configure option in the configure script.

Modules not included by default as well as third-party modules must be explicitly specified in the configure script together with other build options. Such modules can be linked to NGINX binary either statically and will be loaded each time NGINX starts; or dynamically and will be loaded only if they are specified in the NGINX configuration file.

Modules Built by Default

If you do not need a module built by default, you can disable it by including it to the configure script with the --without- prefix :

$ ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-stream --with-pcre=../pcre-8.39 --with-zlib=../zlib-1.2.8 --without-http_empty_gif_module

Module Name Description
http_charset_module Adds the specified charset to the “Content-Type” response header field, can convert data from one charset to another.
http_gzip_module Compresses responses using the gzip method, helping to reduce the size of transmitted data by half or more.
http_ssi_module Processes SSI (Server Side Includes) commands in responses passing through it.
http_userid_module Sets cookies suitable for client identification.
http_access_module Limits access to certain client addresses.
http_auth_basic_module Limits access to resources by validating the user name and password using the HTTP Basic Authentication protocol.
http_autoindex_module Processes requests ending with the slash character (‘/’) and produces a directory listing.
http_geo_module Creates variables with values depending on the client IP address.
http_map_module Creates variables whose values depend on values of other variables.
http_split_clients_module Creates variables suitable for A/B testing, also known as split testing.
http_referer_module Blocks access to a site for requests with invalid values in the Referer header field.
http_rewrite_module Changes the request URI using regular expressions and return redirects; conditionally selects configurations. Requires the PCRE library.
http_proxy_module Passes requests to another server.
http_fastcgi_module Passes requests to a FastCGI server
http_uwsgi_module Passes requests to a uwsgi server.
http_scgi_module Passes requests to an SCGI server.
http_memcached_module Obtains responses from a memcached server.
http_limit_conn_module Limits the number of connections per the defined key, in particular, the number of connections from a single IP address.
http_limit_req_module Limits the request processing rate per a defined key, in particular, the processing rate of requests coming from a single IP address.
http_empty_gif_module Emits single-pixel transparent GIF.
http_browser_module Creates variables whose values depend on the value of the “User-Agent” request header field.
http_upstream_hash_module Enables the hash load balancing method.
http_upstream_ip_hash_module Enables the IP hash load balancing method.
http_upstream_least_conn_module Enables the least_conn load balancing method.
http_upstream_keepalive_module Enables keepalive connections.
http_upstream_zone_module Enables the shared memory zone.

Modules Not Built by Default

Some NGINX modules are not built by default. You will need to enable them manually by adding to the ./configure command. The mail, stream, geoip, image_filter, perl and xslt modules can be compiled as dynamic. See Dynamic Modules for details.

Example of the ./configure command that includes modules:

$ ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-pcre=../pcre-8.39 --with-zlib=../zlib-1.2.8 --with-http_ssl_module --with-stream --with-mail

Third Party Modules

You can extend NGINX functionality by compiling NGINX Open Source with your own module or a third-party module. Some third-party modules are listed in the https://nginx.com/resources/wiki/modules/ page. Use third-party modules at your own risk as their stability is not guaranteed.

Statically Linked Modules

Most modules which are built into NGINX Open Source are statically linked: they are built into NGINX Open Source at compile time and are linked to the NGINX binary statically. These modules can be disabled only after NGINX recompilation.

To compile NGINX Open Source with a statically linked third-party module, in the configure script specify the --add-module= option and type-in the path to the module:

$  ./configure ... --add-module=/usr/build/nginx-rtmp-module

Dynamically Linked Modules

NGINX modules can also be compiled as a shared object (*.so file) and then dynamically loaded into NGINX Open Source at runtime. This provides more flexibility, as the module can be loaded or unloaded at any time with the load_module directive specified in the NGINX configuration file. Note that such a module must support dynamic linking.

To compile NGINX Open Source with a dynamically loaded third-party module, in the configure script specify the --add-dynamic-module= configure option and provide the path to the module:

$  ./configure ... --add-dynamic-module=/path/to/module

The resulting *.so files with dynamic modules can be found after compilation in the prefix/modules/ directory, where the prefix is a directory that keep server files, for example, /usr/local/nginx/modules. To load a dynamic module, use the load_module directive after NGINX installation.

See Introducing Dynamic Modules in NGINX 1.9.11 and Extending NGINX for more information.

Completing the Installation

Installing a Pre-Built Package

Installing NGINX Open Source from a package is much easier and faster comparing to building NGINX from the sources, though it lacks flexibility. Pre-built packages are available for Red Hat, CentOS, Debian, Ubuntu and SLES operating systems.

Modules Included in a Pre-Built Package

See Configure Arguments on nginx.org for the list of modules included in each pre-built package.

Installing Red Hat/CentOS Packages

NGINX provides packages for Red Hat/CentOS 5, 5.x, 6, 6.x, 7 and 7.x. The package can be installed from:

Installing NGINX package From a Default Red Hat/CentOS Repository

  1. Install the EPEL repository:
    $ sudo yum install epel-release
  2. Update the repository and install NGINX Open Source:
    $ sudo yum update
  3. Verify the installation:
    $ sudo nginx -v
    nginx version: nginx/1.6.3

Installing From NGINX Repository

  1. Set up the yum repository for RHEL or CentOS by creating a file nginx.repo in /etc/yum.repos.d, for example, using vi:
    $ sudo vi /etc/yum.repos.d/nginx.repo
  2. Add the lines to the file:
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/mainline/OS/OSRELEASE/$basearch/
    gpgcheck=0
    enabled=1
    where:

    For example, to get the latest mainline package for CentOS 7.0, insert:

    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
    gpgcheck=0
    enabled=1
  3. Save the changes and quit vi: press ESC and type-in :wq
  4. Update the repository and install NGINX Open Source:
    $ sudo yum update
  5. Run NGINX Open Source:
    $ sudo nginx
  6. Verify that NGINX Open Source is up and running:
    $ curl -I 127.0.0.1
    HTTP/1.1 200 OK
    Server: nginx/1.11.7

Installing Debian Packages

NGINX provides packages for Debian 7.x (“wheezy”) and 8.x (“jessie”). The package can be installed from:

Installing NGINX package From a Default Debian Repository

  1. Update Debian repository information:
    $ sudo apt-get update
  2. Install the package:
    $ sudo apt-get install nginx
  3. Verify the installation:
    $ sudo nginx -v
    nginx version: nginx/1.6.2

Installing From NGINX Repository

  1. Download the key used to sign NGINX packages and repository to the apt program keyring and add it:
    $ sudo wget http://nginx.org/keys/nginx_signing.key
    $ sudo apt-key add nginx_signing.key
  2. Add the “sources” from which NGINX Open Source packages can be obtained: open the /etc/apt/sources.list file in any text editor, for example, vi:
    $ sudo vi /etc/apt/sources.list
  3. Add the lines to the file:
    deb http://nginx.org/packages/mainline/debian/ codename nginx
    deb-src http://nginx.org/packages/mainline/debian/ codename nginx
    where:

    For example, to get the latest mainline package for Debian 8.2 (“jessie”), insert:

    deb http://nginx.org/packages/mainline/debian/ jessie nginx
    deb-src http://nginx.org/packages/mainline/debian/ jessie nginx
  4. Save the changes and quit vi: press ESC and type-in :wq
  5. Run the commands:
    $ sudo apt-get remove nginx-common
    $ sudo apt-get update
    $ sudo apt-get install nginx
  6. Run NGINX Open Source:
    $ sudo nginx
  7. Verify that NGINX Open Source is up and running:
    $ curl -I 127.0.0.1
    HTTP/1.1 200 OK
    Server: nginx/1.11.7

Installing Ubuntu Packages

NGINX provides packages for Ubuntu 12.04 (“precise”), 14.04 (“trusty”), 15.10 (“wily”) and 16.04 (“xenial”). The package can be installed from:

Installing NGINX package From a Default Ubuntu Repository

  1. Update Ubuntu repository information:
    $ sudo apt-get update
  2. Install the package:
    $ sudo apt-get install nginx
  3. Verify the installation:
    nginx version: nginx/1.4.6 (Ubuntu)

Installing From NGINX Repository

  1. Download the key used to sign NGINX packages and repository to the apt program keyring and add it:
    $ sudo wget http://nginx.org/keys/nginx_signing.key
    $ sudo apt-key add nginx_signing.key
  2. Add the “sources” from which NGINX Open Source packages can be obtained: open the /etc/apt/sources.list file in any text editor, for example, vi:
    $ sudo vi /etc/apt/sources.list
  3. Add the lines to the file:
    deb http://nginx.org/packages/mainline/ubuntu/ codename nginx
    deb-src http://nginx.org/packages/mainline/ubuntu/ codename nginx
    where:

    For example, to get the latest mainline package for Ubuntu 14.04 (“trusty”), insert:

    deb http://nginx.org/packages/mainline/ubuntu/ trusty nginx
    deb-src http://nginx.org/packages/mainline/ubuntu/ trusty nginx
  4. Save the changes and quit vi: press ESC and type-in :wq
  5. Run the commands:
    $ sudo apt-get remove nginx-common
    $ sudo apt-get update
    $ sudo apt-get install nginx
  6. Run NGINX Open Source:
    $ sudo nginx
  7. Verify that NGINX Open Source is up and running:
    $ curl -I 127.0.0.1
    HTTP/1.1 200 OK
    Server: nginx/1.11.7

Installing SUSE Packages

NGINX provides packages for SUSE Linux Enterprise Server 12.

To download and install the latest stable version, run the commands:

$ zypper addrepo -G -t yum -c 'http://nginx.org/packages/sles/12' nginx
$ zypper install nginx

To download and install the latest mainline version, run the commands:

$ zypper addrepo -G -t yum -c 'http://nginx.org/packages/mainline/sles/12' nginx
$ zypper install nginx