How to upgrade old version of Nginx on Ubuntu 22.04
How to upgrade old version of Nginx on Ubuntu 22.04

How to upgrade old version of Nginx on Ubuntu 22.04

Mayur Chavhan Nginx

Nginx is a powerful web server, load balancer, and reverse proxy that is used by some of the most popular websites in the world. It can help improve the performance and security of your web applications, and this guide will show you how to install the latest version of Nginx on Ubuntu 22.04.

To install Nginx, you need to follow these steps:

  1. Log in as root To proceed with the installation of Nginx, you need to be logged in as root. If you are not already logged in as root, you can switch to the root user using the following command:
$ sudo -i
  1. Update package list The next step is to update the package list using the following command:
# apt update
  1. Install required packages Install the required packages to your system using the following command:
# apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y
  1. Import the Nginx signing key Import the Nginx signing key using the following command:
# wget -O- <https://nginx.org/keys/nginx_signing.key> | gpg --dearmor \\
    | tee /etc/apt/trusted.gpg.d/nginx.gpg > /dev/null
  1. Verify the key Verify that the downloaded file contains the proper key using the following command:
# gpg --dry-run --quiet --import --import-options import-show /etc/apt/trusted.gpg.d/nginx.gpg
  1. Set up the apt repository Set up the apt repository for stable Nginx packages using the following command:
# echo "deb <http://nginx.org/packages/ubuntu> `lsb_release -cs` nginx" \\
    | tee /etc/apt/sources.list.d/nginx.list
  1. Update repository information Update the repository information using the following command:
# apt update
  1. Remove existing Nginx installations Remove all existing Nginx installations using the following command. (This step can be skipped on new systems.)
# apt purge nginx nginx-common nginx-full nginx-core
  1. Install Nginx Install Nginx using the following command:
# apt install nginx
  1. Verify the installation Verify the installation and Nginx version using the following command:
# nginx -v
  1. Enable the Nginx service Enable the Nginx service using the following command:
# systemctl enable nginx
  1. Start Nginx Start Nginx using the following command:
# systemctl start nginx
  1. Modify the default configuration The default configuration when installing Nginx through the Nginx repository differs from the default configuration when installing Nginx through the Ubuntu repository. We will modify a few things to achieve this. First, create additional directories using the following command:
# mkdir /etc/nginx/{modules-available,modules-enabled,sites-available,sites-enabled,snippets}
  1. Edit the nginx.conf file Edit the nginx.conf file using the following command:
# 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
  1. Check the configuration Check the configuration using the following command:
# nginx -t
  1. Restart Nginx Restart Nginx using the following command:
# systemctl restart nginx
  1. Test Nginx Test if Nginx is responding using the curl command:
# curl localhost

It is important to note that this tutorial assumes you are using Ubuntu 22.04. If you are using a different version of Ubuntu or a different operating system, the commands may be different. Also, make sure you have appropriate permissions before running commands.