How To Install NGINX in Debian and Ubuntu

Overview:

This guide shows how to install the latest stable version of NGINX open-source software in Debian and Ubuntu Linux systems.

Foreword

NGINX (pronounced engine-x) is a widely-used open-source and cross-platform reverse proxy, load balancer, web server, and content cache software. It is reliable and efficient and can run on Unix-like systems such as Linux operating systems and many more. NGINX is available in two main flavors:

  • NGINX – the open-source version that doesn’t have premium support.
  • NGINX Plus – the commercial version which is subscription-based and comes with commercial support.

I assume that you have root access to the server where you would want to install NGINX. If not, you should have privileges to invoke the sudo command, to run commands with root privileges.

Install NGINX in Ubuntu from the Official NGINX Repository

First, you need to install some prerequisite tools (that will be used to download files and set up the NGINX repository), by running this command:

$sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring

Next, run these commands to download the NGINX package signing key, set the NGINX official repository, and pin the repository so that the package manager always downloads NGINX packages from it:

$curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
$echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
$echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n"  | sudo tee /etc/apt/preferences.d/99nginx

Then update the APT local package index so that the package manager can have access to the NGINX package in the just added repository. And install NGINX, as follows:

$sudo apt update
$sudo apt install nginx
Install NGINX in Debian from the Official NGINX Repository

Start by installing the required tools that will be used to set up the NGINX repository, by issuing this command:

$sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring

Now set up the NGINX official repository by running these commands:

$curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
$echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
$echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx

Then update the APT cache and install the NGINX package as follows:

$sudo apt update
$sudo apt install nginx
Verify NGINX Installation

To check the version of NGINX installed on your system, run this command:

$ nginx -v
Check the NGINX package version installed

In Debian and Ubuntu, by default, once a service is successfully installed, it is automatically started by systemd (the system and service manager). This allows you to manage the service by performing actions such as checking the service status, stopping it, restarting it, disabling it from auto-starting at system boot, and much more.

After installation, NGINX will run using the default configuration defined in various files stored under the /etc/nginx/ directory. The main configuration is /etc/nginx/nginx.conf and server blocks or virtual hosts can be defined in files under /etc/nginx/conf.d/.

To check if the NGINX service is running and enabled to auto-start at boot time, use the following commands:

$ sudo systemctl status nginx
$ sudo systemctl is-enabled nginx
Check the NGINX service status

The default server block is configured to listen on port 80. You can confirm this by checking all listening TCP ports on your system and grep port 80 as follows:

$sudo ss -tpln | grep 80
OR
$sudo ss -tpln | nginx
Check if NGINX is listening on HTTP port 80

To check if NGINX is ready to receive requests, you can use the curl command below:

$ curl -I http://127.0.0.1
Check if NGINX can receive HTTP requests on the loopback interface
Final Remarks

In this guide, I have shown how to install the NGINX open-source package in Debian and Ubuntu. NGINX has several use cases including deploying it as a web server, reverse proxy, load balancer, content cache, and more. Now that you have it installed, you can explore it. Besides, you can share your thoughts about this guide via the feedback form below.

For more information, see the NGINX open-source website: https://nginx.org/

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Page Contents