How to upgrade NGINX to the latest version

Updated: January 19, 2024 By: Guest Contributor Post a comment

Introduction

Keeping the NGINX web server up-to-date is crucial for securing your site, improving performance, and accessing the latest features. This tutorial guides you through the process of upgrading NGINX to the latest version on different platforms, with a focus on best practices. Whether you’re running NGINX on Ubuntu, CentOS, or Docker, you’ll find step-by-step instructions tailored to your environment.

Before You Begin

Before initiating an upgrade, it’s essential to back up your NGINX configuration files. Run the following command to create a backup:

sudo cp -r /etc/nginx /etc/nginx_backup

Ensure your server’s package lists are updated:

sudo apt update

Or for CentOS systems:

sudo yum update

Upgrading NGINX on Ubuntu

To upgrade NGINX on Ubuntu, you will need to have access to the NGINX repository. If you haven’t added it to your system, you can do so with:

sudo add-apt-repository ppa:nginx/stable

Then run the following commands:

sudo apt-get update
sudo apt-get install nginx

This will automatically install the latest version available in the repository

Advanced Upgrade – Compiling from Source

If you’re looking for the utmost control over your NGINX installation, or if you’re using features that require a custom build, upgrading from source is your best bet. To download and compile the latest version of NGINX from source you will need to follow the next steps:

wget http://nginx.org/download/nginx-1.21.6.tar.gz
tar -zxvf nginx-1.21.6.tar.gz
cd nginx-1.21.6
./configure
make
sudo make install

This sequence of commands will fetch the tarball of the latest NGINX release, decompress it, compile the software, and then install it on the system.

Upgrading NGINX on CentOS

CentOS systems use the yum package manager. To upgrade NGINX to its latest version in a CentOS environment, follow these steps:

sudo yum install epel-release
sudo yum update nginx

If you want to check the installed version of NGINX and make sure your upgrade was successful, execute:

nginx -v

This command will return the installed version of your NGINX server.

Upgrading NGINX in Docker Containers

If you’re running NGINX in Docker containers, you’ll need to pull the latest image from the Docker Hub and roll the container to this new image. For most users, this involves just a couple of commands:

docker pull nginx:latest
docker rm -f my-nginx-container
docker run --name my-nginx-container -d nginx:latest

Be sure to substitute my-nginx-container with the name of your actual container.

Automating Upgrades with Scripts

For those interested in automation, scripting your upgrades can save you a lot of time. Below is a simple bash script for Ubuntu systems that you might find useful:

#!/bin/bash
echo 'Backing up NGINX configuration...'
sudo cp -r /etc/nginx /etc/nginx_backup
echo 'Updating package lists...'
sudo apt-get update
echo 'Upgrading NGINX...'
sudo apt-get install nginx

This script could be run with cronjobs to automate NGINX upgrades regularly.

Conclusion

Following this guide will ensure that your NGINX installation stays current, benefiting from the most recent security patches, performance improvements, and features. Tailor the upgrade process to your platform, always back up your configurations, and consider automation to keep your workload minimal and your services optimal.