Installing Nginx on Ubuntu 12.04 LTS (Precise Pangolin)

Updated by Elle Krout Written by Linode

Contribute on GitHub

Report an Issue | View File | Edit File

This guide has been deprecated and is no longer being maintained.

Nginx is a lightweight, high performance web server designed to deliver large amounts of static content quickly and with efficient use of system resources. In contrast to the Apache server, Nginx uses an asynchronous event-driven model which provides more predictable performance under load.

This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, you can check our Users and Groups guide.

Before You Begin

  1. Ensure that you have followed the Getting Started and Securing Your Server guides, and the Linode’s hostname is set.

    To check your hostname run:

    hostname -f

    The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

  2. Update your system:

    sudo apt-get update && sudo apt-get upgrade

Installing Nginx

There are two branches of Nginx. Nginx Open Source will be the focus of this guide and there are two sources from which you can install it on your Linode: Either from a distro’s repositories or from NGINX Inc., the company which formed behind the software to provide commercial features and support. Each way has its benefits and drawbacks.

Installing from Ubuntu’s Repositories

This method is the easiest and it ensures that Nginx has been tested to run at its best on Ubuntu. The Ubuntu repositories are often a few versions behind the latest Nginx stable release, so while Nginx will still receive security patches, it can be lacking features and bug fixes in comparison.

  1. Install the Nginx web server:

    sudo apt-get install nginx

    The server will automatically start after the installation completes.

  2. Go to the Testing Nginx section of this guide to ensure your server is accessible.

Installing from NGINX

Nginx’s downloads page has two more ways to install the web server: Using pre-built packages from the official Nginx repository or by building from source code. Either method will give you a more current version than what’s available in Trusty Tahr but with a slightly higher chance of encountering unforeseen issues because of newly-introduced bugs, and that these releases are not tested exclusively for a specific Linux distribution.

Installing from the Official Nginx Repository

The binary packages from Nginx’s repo will update you to new versions of the web server when available. You can choose the stable or mainline versions. If unsure, choose stable, which will be the example used for the remainder of this guide.

  1. Add the Nginx repository to Ubuntu’s sources.list file:

    deb trusty nginx
    deb-src trusty nginx
    The deb-src line is only needed if you want repository access to Nginx’s source code.
  2. Download and add Nginx’s repository key to your GPG keyring:

    sudo wget
    sudo apt-key add nginx_signing.key
  3. Update the repository lists and install Nginx:

    The server will automatically start after the installation completes.

  4. Go to the Testing Nginx section of this guide to ensure your server is accessible.

Installing from Source Distribution

Compiling from source gives you the most flexibility and choice for optimization with compiling options and third-party modules. You can also verify the PGP signature of the distributed tarball before compiling.

  1. Install the needed dependencies to build Nginx:

    sudo apt-get install libpcre3-dev build-essential libssl-dev
  2. You can use any location you prefer to build from. Here, /opt will be used. Navigate to it:

    cd /opt
  3. Download the latest version of Nginx Open Source and its PGP signature. You will have the choice of mainline, stable or legacy versions. Again, stable (1.8.0 at the time of this writing) is used as an example.

    sudo wget
    sudo wget
  4. Attempt to verify the tarball’s signature:

    gpg nginx-1.8.0.tar.gz.asc

    The check will fail because you don’t yet have the public RSA key of the signer, and to get it you first need the RSA key ID from the output:

    gpg: Signature made Tue 21 Apr 2015 02:14:01 PM UTC using RSA key ID A1C052F8
    gpg: Can't check signature: public key not found

    Run the key check again:

    gpg nginx-1.8.0.tar.gz.asc

    The output should include:

    gpg: Good signature from "Maxim Dounin <>"
    gpg: key A1C052F8: public key "Maxim Dounin <>" imported
    gpg: no ultimately trusted keys found
    gpg: Total number processed: 1
    gpg:               imported: 1  (RSA: 1)
  5. Expand the source code and change to the new directory:

    sudo tar -zxvf nginx*.tar.gz
    cd /opt/nginx-*
  6. Configure the build options:

    sudo ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module

    When the configuration process completes successfully, you will see the following output:

    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: "/opt/nginx"
      nginx binary file: "/opt/nginx/sbin/nginx"
      nginx configuration prefix: "/opt/nginx/conf"
      nginx configuration file: "/opt/nginx/conf/nginx.conf"
      nginx pid file: "/opt/nginx/logs/"
      nginx error log file: "/opt/nginx/logs/error.log"
      nginx http access log file: "/opt/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"
  7. Build and install Nginx with the above configuration:

    sudo make
    sudo make install
  8. As the root user, create a separate user and group for Nginx:

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

    NGINX is now installed in /opt/nginx.

  9. Installing from source doesn’t include an init file to control when Nginx starts and stops during boot and shutdown. You can either extract that file from the nginx-common package at, or create an SysV script to manage NGINX as shown below:

     1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
    #! /bin/sh
    # 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
    test -x $DAEMON || exit 0
    # Include nginx defaults if available
    if [ -f /etc/default/nginx ] ; then
            . /etc/default/nginx
    set -e
    case "$1" in
            echo -n "Starting $DESC: "
            start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$ \
                    --exec $DAEMON -- $DAEMON_OPTS
            echo "$NAME."
            echo -n "Stopping $DESC: "
            start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$ \
                    --exec $DAEMON
            echo "$NAME."
            echo -n "Restarting $DESC: "
            start-stop-daemon --stop --quiet --pidfile \
                    /opt/nginx/logs/$ --exec $DAEMON
            sleep 1
            start-stop-daemon --start --quiet --pidfile \
                    /opt/nginx/logs/$ --exec $DAEMON -- $DAEMON_OPTS
            echo "$NAME."
              echo -n "Reloading $DESC configuration: "
              start-stop-daemon --stop --signal HUP --quiet --pidfile     /opt/nginx/logs/$ \
                  --exec $DAEMON
              echo "$NAME."
                echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
                exit 1
        exit 0
  10. Make the file executable and add it to the default run levels:

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

    sudo service nginx start

Testing Nginx

Regardless of installation source or method, Nginx can be tested by navigating to your Linode’s IP address or FQDN in your browser. You should see the NGINX welcome banner shown below.

Nginx welcome

Continue reading our introduction to Basic NGINX Configuration for more information about using and setting up a web server.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

See Also

comments powered by Disqus

This guide is published under a CC BY-ND 4.0 license.