In today’s tutorial, we will guide you on how to install Nginx on Ubuntu 22.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
- Ubuntu Cloud VPS running with Ubuntu 22.04
- SSH access
- System user with sudo or root privileges
Login and update the VPS
Login to your Ubuntu 22.04 VPS via SSH
ssh root@IP_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
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 22.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
tar -zxf pcre-8.45.tar.gz
cd pcre-8.45
./configure
make
make install
Download, compile, and install the latest version of zlib
:
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install
Download, compile, and install the latest version of OpenSSL
wget http://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar -zxf openssl-1.1.1g.tar.gz
cd openssl-1.1.1g
./Configure darwin64-x86_64-cc --prefix=/usr
make
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/
./configure
--sbin-path=/usr/local/nginx/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-path=/usr/local/nginx/nginx.pid
--with-pcre=../pcre-8.44
--with-zlib=../zlib-1.2.11
--with-http_ssl_module
--with-stream
--with-mail=dynamic
--add-module=/usr/build/nginx-rtmp-module
--add-dynamic-module=/usr/build/3party_module
After that, compile and install the build by running the following commands
make
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 22.04 VPS:
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' --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 22.04 yourself if you use one of our Ubuntu VPS 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 22.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.