Chris Moore
play

Install the latest version of Nginx on Ubuntu

In most cases, when you instal NGINX from the default repositories found in common Linux distributions like Ubuntu, the version that installs is way out of date. This presents a number of issues, including security vulnerabilities, that we don't want on a brand new server. This post will walk you through installing the most up-to-date stable release of Nginx on Ubuntu 18.04 LTS:

First we need to create a new .list file (using vim in this case) that will point the system to the official NGINX repository where the latest version can be found:

sudo vi /etc/apt/sources.list.d/nginx.list

Paste the following two lines into this new file (if using Ubuntu 16.04 change bionic to xenial):

deb [arch=amd64] http://nginx.org/packages/mainline/ubuntu/ bionic nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ bionic nginx

Save and close the file (in case you have never used vim), just type : and wq for write and quit:

:wq

We next need to install the NGINX public key with the following command (run from your home directory):

wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

Now that we have everything in place, we can update the system:

sudo apt-get update

In the event you had the default or an out-of-date NGINX version installed, we just need to uninstall it:

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

Note: If you have already been running a NGINX configuraion on an older version, you'll want to back up your configuration files.

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old

We are now ready to install the latest version of NGNIX:

sudo apt-get install nginx -y

Once installed, fire it up and set it to start on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Confirm the version:

nginx -v

Change process user and server blocks location

The latest version from the NGINX repository sets nginx as the default process user. As I tend to use the www-datauser and don't like to mix things up, I like to set www-data as the default process user. To do this, open the NGINX config file:

sudo vi /etc/nginx/nginx.conf

On the following line, change nginx to www-data:

user nginx;
user www-data;

(optional) If you prefer to keep all your server block configuration files in the Sites-Available / Sites-Enabled structure, you can keep this setting by changing include /etc/nginx/conf.d/*.conf; in the same configuration file as the user update to:

include /etc/nginx/sites-enabled/*;

Finally, reload the configuration changes:

test code
sudo service nginx reload

If all went well, you should be up and running with the latest NGINX version.