Get the Latest Version of Nginx on Ubuntu 22.04.

Nginx is a high-performance web server, load balancer, and reverse proxy that powers some of the most visited websites in the world. If you’re looking to improve the performance and security of your web applications, you can’t go wrong with Nginx. In this guide, we’ll show you how to install the latest version of Nginx on Ubuntu 22.04, so you can take advantage of the latest features and improvements. Whether you’re a seasoned sysadmin or a beginner, this step-by-step tutorial will help you master Nginx and take your web applications to the next level.

To install the latest version of Nginx on Ubuntu 22.04, you can follow these steps:

This tutorial requires you to be logged in as root, so switch to root user if you are not already.

$ sudo -i

Update the package list.

# apt update

Install the required packages to your system.

# apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y

Import the Nginx signing key.

# wget -O- | gpg --dearmor \
    | tee /etc/apt/trusted.gpg.d/nginx.gpg > /dev/null

Make a new directory for gpg.

# mkdir -m 600 /root/.gnupg

If you get a warning “”mkdir: cannot create directory ‘/root/.gnupg’: File exists” you can skip and continue.

Verify that the downloaded file contains the proper key.

# gpg --dry-run --quiet --import --import-options import-show /etc/apt/trusted.gpg.d/nginx.gpg

Which should output the full fingerprint 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 as follows:

pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
uid                      nginx signing key <>

To set up the apt repository for stable nginx packages, run the following command.

# echo "deb `lsb_release -cs` nginx" \
    | tee /etc/apt/sources.list.d/nginx.list

Perform another system update to update the repository info.

# apt update

Remove all existing Nginx installations. This step can be skipped on new systems.

# apt purge nginx nginx-common nginx-full nginx-core

Install Nginx.

# apt install nginx

Verify the installation and Nginx version.

# nginx -v

This command should output the version number of the installed Nginx server.

nginx version: nginx/1.22.1

Enable the Nginx service.

# systemctl enable nginx

Start Nginx

# systemctl start nginx

The default configuration when installing nginx through the nginx repository differs from the default configuration when installing nginx through the Ubuntu repository, specifically the directory structure and the default configuration files.

Since we don’t want to deviate from the original Ubuntu setup we will have to modify a few things to achieve this.

To begin with, we are going to create some additional directories.

# mkdir /etc/nginx/{modules-available,modules-enabled,sites-available,sites-enabled,snippets}

Edit the nginx.conf file.

# cat > /etc/nginx/nginx.conf <<EOF
user  www-data;
worker_processes  auto;
pid        /var/run/;
include /etc/nginx/modules-enabled/*.conf;
events {
    worker_connections  1024;
http {
    sendfile on;
    tcp_nopush on;
    types_hash_max_size 2048;

    server_tokens off;

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    access_log  /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

Check the configuration.

# nginx -t

Should give you :

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx.

# systemctl restart nginx

Now check if Nginx is responding using curl command.

# curl

Which should give you following output :

<!DOCTYPE html>
<title>Welcome to nginx!</title>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href=""></a>.<br/>
Commercial support is available at
<a href=""></a>.</p>

<p><em>Thank you for using nginx.</em></p>

By default Nginx will not restart automatically if the proccess is killed in some way. Automatic restart nginx

# mkdir -p /etc/systemd/system/nginx.service.d/
# echo -e "[Service]\nRestart=always\nRestartSec=10s" > /etc/systemd/system/nginx.service.d/restart.conf
# systemctl daemon-reload
# systemctl restart nginx
# systemctl status nginx
# pkill nginx

Check again

# systemctl status nginx

Similar Posts