How to Install NGINX on Ubuntu 20.04

/ Category: Tutorials, Ubuntu / Comments: None

How to Install NGINX on Ubuntu 20.04
Spread the love

In today’s tutorial, we will guide you through the steps of installing Nginx on Ubuntu 20.04. Nginx (pronounced as engine x) is a free and open-source web server designed for maximum performance, stability and scalability. It can be also used for caching, reverse proxying different protocols, load balancing, media streaming, etc.

Nginx is used by more than 30% of all active websites, which makes it one of the most popular and widely used web servers in the world. It is used by some of the well-known companies such as WordPress, GitHub, Cloudflare, Dropbox and Netflix.

Prerequisites

  • VPS running with Ubuntu 20.04
  • SSH access
  • System user with sudo or root privileges

Login and Update the VPS

Login to your Ubuntu 20.04 VPS via SSH

ssh [email protected]_Address -p Port_number

Replace IP_Address and Port_number with the actual IP address of the VPS and SSH port number.

Once you are in, run the following command to make sure that all installed packages are updated to the latest available version

apt update && apt upgrade

Method 1: Install Nginx from Repository

Most of the major Linux distributions, including Ubuntu have Nginx in their official repositories, so this is the easiest and most convenient way to install the web server. Nginx can be easily installed with Ubuntu’s package manager ‘apt’. The nginx package will install the web server with some Nginx modules and dependencies

apt -y install nginx

After the installation of the web server completes, start it and enable it to automatically start after a reboot

systemctl start nginx<br>systemctl enable nginx

To confirm that Nginx is up and running you can check the status of the service with the following command

systemctl status nginx

Output:

● nginx.service - A high performance web server and a reverse proxy server
        Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:         enabled)
        Active: active (running)
        Docs: man:nginx(8)
        Process: 18046 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process      on; (code=exited, status=0/SUCCESS)
        Process: 18047 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
        Main PID: 18062 (nginx)
        Tasks: 3 (limit: 4618)
        Memory: 3.4M
        CGroup: /system.slice/nginx.service
       ├─18062 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

You can also visit http://IP_Address and you should get the default Nginx landing page

In order to check the installed version and the configured arguments, run the following

nginx -V

Output:

nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-KTLRnK/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' ...

From the output above we can notice that Nginx version 1.18.0 is shipped with Ubuntu 20.04, which is not the latest version. If for some reason we need the latest release of the web server, or some custom third-party module enabled, we can use method 2 explained in this tutorial and install the web server from the source.

Method 2: Install Nginx from Source

Nginx is available in two versions, the stable and mainline versions. In this step, we will explain how to compile and install the mainline version of Nginx which is up to date and comes with the latest features and security patches and it is more flexible compared to prebuild packages. Be advised that this version is reliable, but it may contain some experimental modules and new bugs.

Nginx requires some libraries such as PCRE, zlib, and OpenSSL to function properly. We will download the source packages of these libraries and compile them from the source.

apt install -y build-essential

Download, compile, and install the latest version of PCRE

wget https://ftp.pcre.org/pub/pcre/pcre-8.45.tar.gz<br>tar -zxf pcre-8.45.tar.gz<br>cd pcre-8.45<br>./configure<br>make<br>make install

Download, compile, and install the latest version of zlib

wget http://zlib.net/zlib-1.2.11.tar.gz<br>tar -zxf zlib-1.2.11.tar.gz<br>cd zlib-1.2.11<br>./configure<br>make<br>make install

Download, compile, and install the latest version of OpenSSL

wget http://www.openssl.org/source/openssl-1.1.1g.tar.gz<br>tar -zxf openssl-1.1.1g.tar.gz<br>cd openssl-1.1.1g<br>./Configure darwin64-x86_64-cc --prefix=/usr<br>make<br>make install

Download the source code of the latest mainline version of Nginx.

wget http://nginx.org/download/nginx-1.21.1.tar.gz

Unpack the downloaded tarball archive

tar zxf nginx-1.21.1.tar.gz

Configure the build options with the parameters of your choice. You can include configuration files, compiler options, and modules.

cd nginx-1.21.1/<br>./configure<br>--sbin-path=/usr/local/nginx/nginx<br>--conf-path=/usr/local/nginx/nginx.conf<br>--pid-path=/usr/local/nginx/nginx.pid<br>--with-pcre=../pcre-8.44<br>--with-zlib=../zlib-1.2.11<br>--with-http_ssl_module<br>--with-stream<br>--with-mail=dynamic<br>--add-module=/usr/build/nginx-rtmp-module<br>--add-dynamic-module=/usr/build/3party_module

After that, compile and install the build by running the following commands

make<br>make install

Once the installation is completed, run a check if the latest version of Nginx and the included options and modules are properly installed on your Ubuntu 20.04 VPS

nginx -V

Output:

nginx version: nginx/1.18.0 (Ubuntu)<br>built with OpenSSL 1.1.1f 31 Mar 2020<br>TLS SNI support enabled<br>configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-KTLRnK/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid

To start the web server run the following command

nginx

And try to access http://IP_Address to confirm that the newly installed web server function properly.


Of course, you don’t need to install Nginx on Ubuntu 20.04 yourself if you use one of our Nginx Hosting services, in which case you can simply ask our expert Linux admins to install and set this up for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to install Nginx on Ubuntu 20.04, please share it with your friends on the social networks by using the share shortcuts below, or simply leave a comment in the comments section. Thanks.

Be the first to write a comment.

Your feedback