How to Install Nginx Latest Version on Ubuntu 16.04 and Ubuntu 16.10

In this tutorial, we’re going to learn how to install Nginx latest version on Ubuntu 16.04 and Ubuntu 16.10. At the time of this writing, the latest version of Nginx is 1.11.8, released on December 27th, 2016. You can view changes over previous versions here.

Installing Nginx Latest Version from Official Nginx Repository

Nginx is included in Ubuntu software repository but the version is out-of-date. To install the latest version of Nginx, we will need to add the official Nginx repository.

Open up your sources.list file with a text editor such as nano.

sudo nano /etc/apt/sources.list

In nano text editor, press CTRL+W then CTRL+V to jump to the end of the file. Next, append the following two lines at the end of the file.

deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx

nginx latest version

Press CTRL+O to save the file. Then CTRL+X to exit. In order to verify the integrity of packages downloaded from this repo, we need to import Nginx public key using the commands below.

wget http://nginx.org/keys/nginx_signing.key

sudo apt-key add nginx_signing.key

Once the repository is added to your Ubuntu 16.04 system, run the following command to update repository info and install Nginx.

sudo apt update

sudo apt install nginx

If you have installed Nginx from Ubuntu software repository, you need to remove it before executing the above two commands.

sudo apt remove nginx nginx-common nginx-full nginx-core

Also you may want to back up the main Nginx config file /etc/nginx/nginx.conf because it will be replaced with a new nginx.conf file.

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

Your existing server block files will be intact.

After Nginx is installed, we need to unmask nginx.service file using the command below.

sudo systemctl unmask nginx

Then start Nginx.

sudo systemctl start nginx

If nginx.service isn’t unmasked and you try to start Nginx, you will see the following error.

failed to start nginx.service: unit nginx.service is masked.

To enable Nginx to auto start at boot time, run

sudo systemctl enable nginx

To check the status of Nginx, run

systemctl status nginx

Output:

 nginx.service - LSB: Stop/start nginx
   Loaded: loaded (/etc/init.d/nginx; bad; vendor preset: enabled)
   Active: active (running) since Sat 2017-01-07 02:58:50 UTC; 3min 16s ago
     Docs: man:systemd-sysv-generator(8)
 Main PID: 1708 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nginx.service
           ├─3429 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.con
           └─3430 nginx: worker process

To check Nginx version, use this command:

nginx -v

Output:

nginx version: nginx/1.11.8

Setting the Nginx Process User

The Nginx package from nginx.org repository set nginx as the Nginx process user which can be inferred from the first line of /etc/nginx/nginx.conf file.

user nginx;

However, the default user and group of PHP-FPM process is www-data as can be seen in /etc/php/7.0/fpm/pool.d/www.conf file.

user = www-data
group = www-data

So we need to set www-data as the Nginx process user in /etc/nginx/nginx.conf file.

sudo nano /etc/nginx/nginx.conf

Change

user nginx;

to

user www-data;

Save and close the file. Then reload Nginx.

sudo systemctl reload nginx

Including Server Block Files

By default, only files under /etc/nginx/conf.d/ directory will be included. If you also want to use server block files in sites-enabled directory, then make sure the following lines are added in the http section of nginx.conf file.

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

That’s it!

I hope this tutorial helped you install Nginx latest version on Ubuntu 16.04 and Ubuntu 16.10. As always, if you found this post useful, then subscribe to our free newsletter. You can also follow us on Google+ Twitter or like our Facebook page.

Rate this tutorial
[Total: 11 Average: 4.7]