By default Ubuntu seems to play things safe and tries to stick with older, but stable versions of software pushed out via the Ubuntu repositories. In most cases this is perfectly fine, but not when it comes to Nginx. If you are running the repository version from Ubuntu 16.04 (Xenial), the latest version is currently at 1.10.3 which is perfectly stable. Unfortunately 1.12.1 is just a lot better. Nginx 1.12 includes now variables support and other improvements in the stream module, HTTP/2 fixes, support for multiple SSL certificates of different types, improved dynamic modules support, and much more. Using this guide, upgrading will take you basically a couple of minutes without breaking running sites and including testing. So let’s start with upgrading Nginx to version 1.12.1.

Upgrade NGINX web server from 1.10.3 to 1.12.1.

In the first step we're going to backup our Nginx configuration directory. We are going to do this in the home directory of the authenticated user. With any kind of upgrade, you should always have a backup in case things go wrong. To back up, do the following steps.

$ cd /etc/nginx
$ sudo mkdir ~/nginx-conf-backup/
$ sudo cp -R * ~/nginx-conf-backup/

If you test to see your current version of Nginx you will see the following.

$ nginx -v
nginx version: nginx/1.10.3 (Ubuntu)

Now we are going to upgrade Nginx to 1.12.1 from the official Nginx Repository. First we add the repository. If you didn't add a personal repository before you need to install the required software-properties-common.

$ sudo apt install software-properties-common

Add the Nginx repository by running the following commands in terminal.

$ sudo add-apt-repository ppa:nginx/$nginx

This should give you the following output.

This PPA contains the latest Stable Release version of the nginx web server software.

The following architectures are supported; if your arch doesn't match this list then the PPA is not supporting that architecture:
* i386
* amd64
* armhf
* arm64
* ppc64el

The following releases of Ubuntu (including in-development releases) are supported in this PPA:
* Trusty (14.04)
* Xenial (16.04)
* Yakkety (16.10)
* Zesty (17.04)
* Artful (17.10 - in development!)

The following releases of Ubuntu are no longer supported, and have had their packages removed from the PPA:
* Lucid (10.04)
* Maverick (10.10)
* Natty (11.04)
* Oneiric (11.10)
* Quantal (11.10)
* Raring (13.04)
* Saucy (13.10)
* Utopic (14.10)
* Vivid (15.04)
* Wily (15.10)
 More info:
Press [ENTER] to continue or ctrl-c to cancel adding it

Now run the following to update your apt repository information.

$ sudo apt update

This should give you the following output

Hit:1 xenial InRelease
Hit:2 xenial-updates InRelease
Hit:3 xenial-backports InRelease
Hit:4 xenial InRelease
Get:5 xenial InRelease [17.5 kB]
Hit:6 xenial InRelease
Get:7 xenial-security InRelease [102 kB]
Hit:8 xenial InRelease
Get:9 xenial/main amd64 Packages [4164 B]
Get:10 xenial/main i386 Packages [4168 B]
Get:11 xenial/main Translation-en [4160 B]
Fetched 132 kB in 0s (263 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
2 packages can be upgraded. Run 'apt list --upgradable' to see them.

To validate the two packages that can be upgraded are indeed the nginx packages run the above mentioned command.

$ sudo apt list --upgradable
Listing... Done
nginx/xenial,xenial 1.12.1-0+xenial0 all [upgradable from: 1.10.3-0ubuntu0.16.04.2]
nginx-common/xenial,xenial 1.12.1-0+xenial0 all [upgradable from: 1.10.3-0ubuntu0.16.04.2]

Now let's upgrade Nginx to 1.12.1 by running the following from the terminal

If we run the normal upgrade command sudo apt upgrade it gives back that 2 packages are kept back. Therfore we need to do a dist upgrade.

$ sudo apt dist-upgrade

it should the following feedback:

reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be REMOVED:
The following NEW packages will be installed:
  libnginx-mod-http-auth-pam libnginx-mod-http-dav-ext libnginx-mod-http-echo libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-subs-filter libnginx-mod-http-upstream-fair libnginx-mod-http-xslt-filter
  libnginx-mod-mail libnginx-mod-stream nginx-full
The following packages will be upgraded:
  nginx nginx-common
2 upgraded, 11 newly installed, 1 to remove and 0 not upgraded.
Need to get 892 kB of archives.
After this operation, 842 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 xenial/main amd64 nginx all 1.12.1-0+xenial0 [24.3 kB]
Get:2 xenial/main amd64 nginx-common all 1.12.1-0+xenial0 [57.2 kB]
Get:3 xenial/main amd64 libnginx-mod-http-auth-pam amd64 1.12.1-0+xenial0 [28.5 kB]
Get:4 xenial/main amd64 libnginx-mod-http-dav-ext amd64 1.12.1-0+xenial0 [30.2 kB]
Get:5 xenial/main amd64 libnginx-mod-http-echo amd64 1.12.1-0+xenial0 [40.0 kB]
Get:6 xenial/main amd64 libnginx-mod-http-geoip amd64 1.12.1-0+xenial0 [29.8 kB]
Get:7 xenial/main amd64 libnginx-mod-http-image-filter amd64 1.12.1-0+xenial0 [33.1 kB]
Get:8 xenial/main amd64 libnginx-mod-http-subs-filter amd64 1.12.1-0+xenial0 [31.5 kB]
Get:9 xenial/main amd64 libnginx-mod-http-upstream-fair amd64 1.12.1-0+xenial0 [31.7 kB]
Get:10 xenial/main amd64 libnginx-mod-http-xslt-filter amd64 1.12.1-0+xenial0 [31.5 kB]
Get:11 xenial/main amd64 libnginx-mod-mail amd64 1.12.1-0+xenial0 [60.2 kB]
Get:12 xenial/main amd64 libnginx-mod-stream amd64 1.12.1-0+xenial0 [81.2 kB]
Get:13 xenial/main amd64 nginx-full amd64 1.12.1-0+xenial0 [412 kB]
Fetched 892 kB in 0s (1229 kB/s)
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = "en_US:en",
	LC_ALL = (unset),
	LC_CTYPE = "UTF-8",
	LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
Preconfiguring packages ...
(Reading database ... 129661 files and directories currently installed.)
Preparing to unpack .../nginx_1.12.1-0+xenial0_all.deb ...
Unpacking nginx (1.12.1-0+xenial0) over (1.10.3-0ubuntu0.16.04.2) ...
(Reading database ... 129661 files and directories currently installed.)
Removing nginx-core (1.10.3-0ubuntu0.16.04.2) ...
(Reading database ... 129656 files and directories currently installed.)
Preparing to unpack .../nginx-common_1.12.1-0+xenial0_all.deb ...
Unpacking nginx-common (1.12.1-0+xenial0) over (1.10.3-0ubuntu0.16.04.2) ...
Selecting previously unselected package libnginx-mod-http-auth-pam.
Preparing to unpack .../libnginx-mod-http-auth-pam_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-auth-pam (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-dav-ext.
Preparing to unpack .../libnginx-mod-http-dav-ext_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-dav-ext (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-echo.
Preparing to unpack .../libnginx-mod-http-echo_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-echo (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-geoip.
Preparing to unpack .../libnginx-mod-http-geoip_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-geoip (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-image-filter.
Preparing to unpack .../libnginx-mod-http-image-filter_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-image-filter (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-subs-filter.
Preparing to unpack .../libnginx-mod-http-subs-filter_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-subs-filter (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-upstream-fair.
Preparing to unpack .../libnginx-mod-http-upstream-fair_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-upstream-fair (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-xslt-filter.
Preparing to unpack .../libnginx-mod-http-xslt-filter_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-xslt-filter (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-mail.
Preparing to unpack .../libnginx-mod-mail_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-mail (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-stream.
Preparing to unpack .../libnginx-mod-stream_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-stream (1.12.1-0+xenial0) ...
Selecting previously unselected package nginx-full.
Preparing to unpack .../nginx-full_1.12.1-0+xenial0_amd64.deb ...
Unpacking nginx-full (1.12.1-0+xenial0) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.35-0ubuntu2) ...
Processing triggers for systemd (229-4ubuntu21) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up nginx-common (1.12.1-0+xenial0) ...
Installing new version of config file /etc/nginx/nginx.conf ...
Installing new version of config file /etc/nginx/sites-available/default ...
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
Setting up libnginx-mod-http-auth-pam (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-dav-ext (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-echo (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-geoip (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-image-filter (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-subs-filter (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-upstream-fair (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-xslt-filter (1.12.1-0+xenial0) ...
Setting up libnginx-mod-mail (1.12.1-0+xenial0) ...
Setting up libnginx-mod-stream (1.12.1-0+xenial0) ...
Setting up nginx-full (1.12.1-0+xenial0) ...
Setting up nginx (1.12.1-0+xenial0) ...

That’s it, your Upgrade Nginx is done and you are now rocking Nginx version 1.12.1 and you can confirm this by typing the following.

$ nginx -v
nginx version: nginx/1.12.1

To ensure all configuration is still ok.

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

After this I've choosen to reboot the VPS (in my case 5 seconds of downtime) to ensure no old processes where kept running.

Top image source
Wallpaper from