Nginx is a widely popular and open-source web server that you can use as a web server, reverse proxy, cache server and load balancer among other things. It is very powerful and lightweight and takes very few resources. In this tutorial, you will learn how to install Nginx Web Server on ubuntu 18.04/20.04 machine. So let’s get started.
There are two methods with which you can install the Nginx Web Server on ubuntu OS.
- Using Ubuntu Official Repository
- Using Nginx Official Repository.
Prerequisite
- Ubuntu 20.04 Machine
- User with the sudo user or root user privileges.
- Good Internet Connection
Method:1 – Install Nginx on Ubuntu using the Ubuntu Official Repository
Nginx package is maintained on the default Ubuntu official repositories, so you can install it with the default package manager. It is a good update the official repository first. To install Nginx on Ubuntu, execute the following command,
$ sudo apt upgrade -y
$ sudo apt install nginx
Once the nginx is installed. You can check nginx service status by running the below command,
$ sudo systemctl status nginx
or
$ ps -ef | grep nginx
If it’s not running, you can start the nginx service by the following command,
$ sudo systemctl start nginx
It is a good practice to enable the nginx service at the boot time. You can enable it by running the following command,
$ sudo systemctl enable nginx
Once the webserver is up and running, you can also access the default webpage of Nginx. All you need is the system IP address or if you are trying to access the webpage on the localhost, then we can also access the webpage with either ‘localhost’ or ‘127.0.0.1’ as the URL.
http://IP_address_of_the_system
or
http://localhost
Method:2 – Install Nginx on ubuntu using the Official Nginx Repository
Nginx can be installed by the official nginx repositories. To do this first you need to install the keys that are used to sign the NGINX packages,
$ sudo wget https://nginx.org/keys/nginx_signing.key
$ sudo apt-key add nginx_signing.key
Next, you will have to add the repositories in the file “/etc/apt/sources.list”,
$ nano /etc/apt/sources.list deb https://nginx.org/packages/mainline/ubuntu/ <Version-Name> nginx deb-src https://nginx.org/packages/mainline/ubuntu/ <Version-Name> nginx
Here, <Version-Name> refers to the code the Ubuntu version being used, list is mentioned below,
Version-Name | Version-Code |
Trusty | 14.04 LTS |
Xenial | 16.04 |
Bionic | 18.04 |
Disco | 19.04 |
Focal | 20.04 |
So for Ubuntu 20.04, we need to add the following entries to ‘/etc/sources.list’,
deb https://nginx.org/packages/mainline/ubuntu/ focal nginx
deb-src https://nginx.org/packages/mainline/ubuntu/ focal nginx
Once done, save the file & exit & run the following command to install nginx on Ubuntu,
$ sudo apt update -y && sudo apt upgrade -y
$ sudo apt install nginx
Next check the status of the nginx service and if it’s not running, you must start the nginx service by the following command,
$ sudo systemctl status nginx
$ sudo systemctl start nginx
As we said earlier It is a good practice to enable the nginx service at the boot time. You should enable it by running the following command,
$ sudo systemctl enable nginx
Once the webserver is up and running, you can also access the default webpage of Nginx on your favourite web browser.
Open Default Ports in UFW Firewall
By default, nginx runs on port 80 so we need to open tcp port in default ufw firewall. To check the status of ufw, run the following command:
$ sudo ufw status
Status: inactive
You can enable the ufw firewall using below command.
$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
Now open ssh port 22 and nginx port 80 in ufw using following command:
$ sudo ufw allow 22/tcp
$ sudo ufw allow 80/tcp
Next, check the status of UFW by following command:
$ sudo ufw status
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
Hence, we opened default port of nginx in ufw firewall.
Conclusion
So let’s finish our tutorial on how to install Nginx web server on Ubuntu 18.04/20.04. In our next tutorial, we will discuss how we can create the webserver blocks to host multiple websites on a single Nginx webserver.
Read Also : How to install Nginx Web Server on RHEL/CentOS