How to Install Nginx Latest Version on Ubuntu 18.04, Ubuntu 19.04

Previously we discussed how to install LEMP stack, which is a common software stack to host dynamic websites, on Ubuntu 18.04 LTS. However, software in Ubuntu LTS (long term support) release are often out-of-date. In this tutorial, we’re going to learn how to install Nginx latest version on Ubuntu 18.04 and Ubuntu 19.04. At the time of this writing, the latest version of Nginx is 1.17.0, released on May 21, 2019. You can see the change log here.

Installing Nginx Latest Version on Ubuntu 18.04, 19.04 from Official Nginx Repository

Nginx.org maintains a repository for Ubuntu. We can use this repository to install the latest version of Nginx. First, create a repository source file for Nginx with the following command. Nano is a command line text editor.

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

Add the following two lines in the file.

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

ubuntu 18.04 nginx latest version amd64

If you use Ubuntu 19.04, then change bionic to disco.

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

To save the file in Nano text editor, press CTRL+O, then press Enter to confirm. Press CTRL+X to exit. In order to verify the integrity of packages downloaded from this repository, 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 18.04 system, run the following command to update repository info.

sudo apt update

If you have installed Nginx from the default Ubuntu software repository, you need to remove it.

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

Also you may want to back up the main Nginx configuration file /etc/nginx/nginx.conf because it will be replaced with a new nginx.conf file when we later install the latest version of Nginx.

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

Your existing server block files (aka virtual host file) will be intact. Now run the following command to install Nginx from nginx.org repository.

sudo apt install nginx

If the apt package manager asks you if you want to install a new version of /etc/nginx/nginx.conf file, you can answer No.

nginx 1.17.0 ubuntu

After Nginx is installed, test Nginx configuration.

sudo nginx -t

If the test is successful, start Nginx.

sudo systemctl start nginx

Enable autostart at boot time.

sudo systemctl enable nginx

To check the status of Nginx, run

systemctl status nginx

Output:

 nginx.service - nginx - high performance web server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-05-26 21:01:10 CST; 3s ago
     Docs: http://nginx.org/en/docs/
  Process: 16159 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 16160 (nginx)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/nginx.service
           ├─16160 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─16161 nginx: worker process

May 26 21:01:10 bionic.local.domain systemd[1]: Starting nginx - high performance web server...
May 26 21:01:10 bionic.local.domain systemd[1]: Started nginx - high performance web server.

To check Nginx version, use this command:

nginx -v

Output:

nginx version: nginx/1.17.0

You can also check more detailed information with:

nginx -V

Output:

nginx ubuntu 18.04 with compat

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. (If you chose to install the new version 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.2/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/*;

Install Certbot Nginx Plugin

If you previously installed Nginx from Ubuntu repository, then the python3-certbot-nginx package was probably removed when you install Nginx from nginx.org repository. We need to install it back so that your TLS certificate can be automatically renewed as usual.

sudo apt install python3-certbot-nginx

Next Step

I hope this tutorial helped you install Nginx latest version on Ubuntu 18.04 and Ubuntu 19.04. You may also want to install the latest version of MariaDB database server on Ubuntu 18.04 and 19.04. As always, if you found this post useful, then subscribe to our free newsletter to get new tips and tricks 🙂

Rate this tutorial
[Total: 0 Average: 0]

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • If my answer helped you, please consider supporting this site. Thanks :)