Install latest version of Nginx on Ubuntu

NginxI always run the latest LTS version of Ubuntu on all my servers. Unfortunately, the Nginx versions tend to be quite the bit behind the current release. So how do you get an updated, current version of without resorting to having to maintain the packages yourself? Luckily, the Nginx team have their own Ubuntu apt repository so it’s easy to keep current with the latest version of Nginx.

In general, you should deploy the NGINX mainline branch at all times.  You may wish to use stable if you are concerned about possible impacts of new features, such as incompatibility with third-party modules or the introduction of bugs in new features.

Source: NGINX 1.6 and 1.7 released – Which version should I use?

First, to avoid missing PGP key during installation and upgrades, install the Nginx team’s package signing key:

$ curl -s https://nginx.org/keys/nginx_signing.key | sudo apt-key add -

Add the repo to your apt sources:

$ sudo echo -e "deb https://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx\ndeb-src https://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx" > /etc/apt/sources.list.d/nginx.list

Resynchronize the package index files from their sources:

$ sudo apt-get update

If you are installing nginx:

$ sudo apt-get install nginx

… or if you are upgrading:

$ sudo apt-get dist-upgrade

And you’re done :-)

Maybe you are also looking for how to install the latest version of PHP?
… or even replace PHP with HHVM.

15 thoughts on “Install latest version of Nginx on Ubuntu”

  1. Hi, this is a really nice article, but I have a couple of questions if I may.
    If I run the apt-get install nginx, will it install it with http2 module in it?
    Is there a guide on how to build nginx to support http2?

    Off-topic: I’ve tried to add the php fpm to nginx and didn’t worked like in previous versions, any thoughts on that?

    Kind regards, Alex.

    1. Bjørn Johansen

      Nginx version 1.9.5 from the official Nginx APT repository includes the HTTP/2 module.

      If you’re building Nginx yourself, add --with-http_v2_module to the configure arguments.

      There’s no change in how it handles upstreams or fastcgi, so it works just as in previous versions.

    1. Bjørn Johansen

      None of the screenshots are working, but I see that you are running Nginx 1.9.5 which currently is the newest Nginx version, so I guess you fixed it.

  2. Hi, short update, I googled around and I think it should work ok.
    This is what I did on a new ubuntu 14.04 installation.

    Installed nginx 1.9.5 from repository
    Install php fast cgi

    Here were the problems in my case:
    The default value of the nginx user in nginx.conf was “nginx” and not “www-data”. Changed to “www-data”.

    In the “/etc/php5/fpm/pool.d/www.conf” I’ve un- commented the line with the “listen.mode = 0660”

    After that I’ve added two additional lines in “fastcgi_params” from nginx dir.
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    fastcgi_param PATH_INFO $fastcgi_script_name;

    This is the rest of the editing made in default configuration file for nginx.
    location ~ \.php$ {
    root /usr/share/nginx/html;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }

    Hope this is useful for someone. :)

    Best regards,
    Alexandru.

    1. Bjørn Johansen

      You are absolutely right. I’ve fixed this, and a few similar things in the post now.
      Thank you!

  3. Hi, it is nice article.

    I am facing one issue in website.
    Issue is, sometime HTML code is displaying without rendering.

    Currently using nginx 1.12 and http 1.1. so planning upgrade to nginx latest version and http2.

    Please suggest which version of nginx needs to be install and how to enable http2.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.