Select Page

How to Safely Upgrade Nginx on Ubuntu: A Step-by-Step Guide

How to Safely Upgrade Nginx on Ubuntu: A Step-by-Step Guide

Upgrading Nginx to a newer version can improve performance, add features, and close potential security gaps. This guide will walk you through a step-by-step process to safely upgrade Nginx on your Ubuntu system.

1. Check Your Current Nginx Version

First, you need to know the version currently running on your system. Use the following command:

nginx -v

Example output: nginx version: nginx/1.14.0 (Ubuntu)

2. Backup Important Nginx Directories

Taking backups ensures that you can restore your configuration if anything goes wrong. Run these commands to back up essential directories:

sudo cp -r /etc/nginx /etc/nginx-backup
sudo cp -r /etc/ssl /etc/ssl-backup
sudo cp -r /var/www /var/www-backup
sudo cp -r /var/log/nginx /var/log/nginx-backup

3. Update Your Package List

After making backups, update your system’s package list to get the latest versions:

sudo apt update

4. Install Required Packages

Next, install essential packages that allow you to add the official Nginx repository:

sudo apt install curl gnupg2 ca-certificates lsb-release

5. Add the Official Nginx Repository

Adding the official Nginx repository ensures that you receive the latest stable release. Use these commands:

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

6. Update Package List Again

After adding the Nginx repository, update the package list once more:

sudo apt update

7. Upgrade Nginx

Now, you are ready to upgrade Nginx. Execute the following command:

sudo apt upgrade nginx

Handling Potential Errors

If you encounter dependency issues, such as:

The following packages have unmet dependencies:
nginx : Conflicts: nginx-common but 1.14.0-0ubuntu1.11 is to be installed

You can resolve these conflicts by removing previous Nginx packages:

sudo apt remove nginx nginx-common nginx-core
sudo apt install nginx

When prompted about the configuration file, choose:

Configuration file ‘/etc/nginx/nginx.conf’ has been modified since installation. What would you like to do?
(Y/I: install package maintainer's version, N/O: keep your version)

Choose ‘N’ to keep your current configuration.

8. Verify and Restart Nginx

Once the upgrade is complete, verify the update and restart the Nginx service:

nginx -v    # Check the Nginx version
sudo systemctl restart nginx
sudo nginx -t  # Test Nginx configuration

After following these steps, you should see the updated version displayed, such as nginx/1.24.0.

By following these steps, you can safely upgrade your Nginx web server. Taking backups and managing configuration files carefully allows for a smooth transition to the latest version. This upgrade enhances both performance and security on your Ubuntu server.

About The Author

sauravjaiswal

Experienced FULL STACK Developer with around 5+ years of professional exposure in multiple industries to design, develop and deliver scalable applications. Skilled in Python, Vuejs, Bootstrap, Quasar Framework, Flask Framework, Tailwind CSS and Web development.

Leave a reply

Your email address will not be published. Required fields are marked *

Share This