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- https://nginx.org/keys/nginx_signing.key | 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] 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 uid nginx signing key <signing-key@nginx.com>
To set up the apt repository for stable nginx packages, run the following command.
# echo "deb http://nginx.org/packages/ubuntu `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/nginx.pid; 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/*; } EOF
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 http://127.0.0.1
Which should give you following output :
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <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="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
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