How to Install Nginx Latest Version on Ubuntu 18.04, Ubuntu 20.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 20.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, 20.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
If you use Ubuntu 20.04, then change bionic
to focal
.
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.
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:
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
How to Automatically Restart Nginx
Sometimes Nginx can crash for various reasons. If you prefer to make Nginx automatically restart after a crash, then we need to edit the Nginx service unit. First, copy the original Nginx service unit to the /etc/systemd/system/
directory.
sudo cp /lib/systemd/system/nginx.service /etc/systemd/system/nginx.service
Then edit the service unit.
sudo nano /etc/systemd/system/nginx.service
Add the following line in the [service]
section.
Restart=always RestartSec=2
Like so:
[Service] Type=forking PIDFile=/var/run/nginx.pid ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID Restart=always RestartSec=2
This will make Nginx try to restart itself every 2 seconds after a crash. Save and close the file. Then restart Nginx.
sudo systemctl restart nginx
Next Step
I hope this tutorial helped you install Nginx latest version on Ubuntu 18.04 and Ubuntu 20.04. You may also want to install the latest version of MariaDB database server on Ubuntu 18.04 and 20.04.
And if you care about security, you can set up the ModSecurity web application firewall to protect your WordPress site from hacking.
As always, if you found this post useful, then subscribe to our free newsletter to get new tips and tricks 🙂
You are awesome man. I am learning AWS and need to install latest stack on ubuntu instance.
Hello there,
While typing the command:
sudo ufw allow ‘Nginx HTTP’
I get the below error:
ERROR: Could not find a profile matching ‘Nginx HTTP’
Your command contains apostrophes, which should have been single quotes.
Thank you for replying.
That is a mistake while typing here. I am using single quotes and getting same error
The content of the ‘Nginx HTTP’ profile is as follows:
You can run the following command to open port 80.
You can also check out my UFW guide.
Thanks,
I already open the port 80 and 443 when the above commands didn’t work. But I was wondering why that didn’t worked.
It look like the profile does not exists in
Answer from the page https://unix.stackexchange.com/questions/460480/allow-access-to-apache-on-both-port-80-and-443-in-ubuntu-16-04 help me to create a profile.
Anyway my problem is solved and thanks for helping.
Is there any tutorial by you installing latest version of PHP on ngnix server in ubuntu ?
I didn’t write such tutorial. You can use this PPA to install latest version of PHP.
I think I can do from here. It should not be that difficult to configure it with nginx and mysql.
Thank you again.
Top tutorisal, all working. Copy past. Ubuntu 18 server.
Saw other tutorisals – shiiit
eyvallah sağol güzel makale faydalı bilgi harika paylaşım +rep
Thank you nice article useful information great sharing + rep
Ya