Sling Academy
Home/DevOps/How to upgrade NGINX to the latest version

How to upgrade NGINX to the latest version

Last updated: January 19, 2024

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.

Next Article: How to completely uninstall NGINX from your system

Previous Article: Understanding NGINX Architecture: The Big Picture

Series: NGINX Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide