Sep 10, 2017

Hot to install the latest NGINX on Ubuntu 16

Alexey Doronin
Founder, developer, designer

Consider installing the latest NGINX version on a server running Ubuntu 16.

Let's check which version is already installed and which one we want to install:

nginx -v

1. If necessary, delete the current version:

sudo apt remove nginx nginx-common nginx-full nginx-core
sudo systemctl unmask nginx
# sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

2. Add the official NGINX repository:

sudo nano /etc/apt/sources.list
# Add to the end of the file:
deb xenial nginx
deb-src xenial nginx

3. Add the key:

sudo apt-key add nginx_signing.key

4. Update the list of repositories and install NGINX:

sudo apt update
sudo apt install nginx

If you need to install a specific version:

apt-cache policy nginx
sudo apt-get install nginx=1.13.6-1~xenial

5. Add NGINX to autostart:

sudo systemctl enable nginx

6. Check status:

systemctl status nginx

7. If you use PHP-FPM, you need to change the group and user to www-data (when installing NGINX from the official repository, there will be nginx). Change and reboot:

sudo nano /etc/nginx/nginx.conf
## Replace nginx with www-data
user www-data;

## reload nginx
sudo systemctl reload nginx

8. Check that the following lines are in nginx.conf:

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

9. If we compile dynamic modules for NGINX, we need to block NGINX from updating (since dynamic modules are built for a specific version):

sudo apt-mark hold nginx

## Unhold
sudo apt-mark unhold <package-name>

10. Check the version and the fact that it is on the "hold "(blocked from updates):

nginx -v
apt-mark showhold