How to upgrade old version of Nginx on Ubuntu 22.04
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:
- 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
- Update package list The next step is to update the package list using the following command:
# apt update
- 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
- 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
- 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
- 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
- Update repository information Update the repository information using the following command:
# apt update
- 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
- Install Nginx Install Nginx using the following command:
# apt install nginx
- Verify the installation Verify the installation and Nginx version using the following command:
# nginx -v
- Enable the Nginx service Enable the Nginx service using the following command:
# systemctl enable nginx
- Start Nginx Start Nginx using the following command:
# systemctl start nginx
- 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}
- 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
- Check the configuration Check the configuration using the following command:
# nginx -t
- Restart Nginx Restart Nginx using the following command:
# systemctl restart nginx
- 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.