Keeping your Nginx server up-to-date ensures better security, performance, and new features. This guide will walk you through a safe and efficient way to upgrade Nginx on Ubuntu while preserving your configuration.
Prerequisites: What You Need Before Upgrading
Before starting, make sure you have:
✅ Root or Sudo Access – Required to install and configure software.
✅ Backup of Configuration – Prevents data loss in case of an issue.
Step 1: Check Your Current Nginx Version
Before upgrading, check the current version installed:
nginx -v
Expected output (or similar):
nginx version: nginx/1.18.0 (Ubuntu)
This helps verify if the upgrade was successful later.
Step 2: Backup Your Nginx Configuration
To prevent loss of important configurations, create a backup:
sudo cp -r /etc/nginx /etc/nginx.backup
Verify the backup:
ls -l /etc/nginx.backup
You should see a copy of your configuration files.
Step 3: Add the Latest Nginx Repository
By default, Ubuntu’s package manager doesn’t always have the latest Nginx version. To get the newest release, we need to add the official Nginx repository.
1️⃣ Update and install required dependencies:
sudo apt update && sudo apt install software-properties-common -y
2️⃣ Add the official Nginx repository:
sudo add-apt-repository ppa:nginx/stable -y
3️⃣ Update the package list:
sudo apt update
4️⃣ Confirm the latest version is available:
apt policy nginx | grep "Candidate"
This should display the latest version that will be installed.
Step 4: Install/Upgrade Nginx
Now, install the latest version of Nginx:
sudo apt install nginx -y
During installation, you may see a prompt:
Configuration file ‘/etc/nginx/nginx.conf’ ==> Modified (by you or a script) since installation. ==> Package distributor has shipped an updated version. What would you like to do about it? (Y/I/N/O/D/Z) [default=N] ?
Select "N" to keep your existing configurations.
Once the installation is complete, verify the version:
nginx -v
Expected output:
nginx version: nginx/1.27.0
Step 5: Restart and Verify Nginx
After upgrading, restart Nginx to apply changes:
sudo systemctl restart nginx
Check the status to ensure it’s running:
sudo systemctl status nginx
✅ Expected output: "Active (running)" in green.
Step 6: Test Your Configuration
To avoid errors, always test your Nginx configuration before restarting:
sudo nginx -t
Expected output:
nginx: configuration file /etc/nginx/nginx.conf test is successful
If there are errors, fix them before proceeding.
Step 7: Monitor Nginx After Upgrade
Check the logs to ensure Nginx is functioning correctly:
sudo tail -f /var/log/nginx/error.log
Additionally, perform a quick HTTP request test:
curl -I http://localhost
Expected response:
HTTP/1.1 200 OK
This confirms that the server is handling requests correctly.
Conclusion: Upgrade Complete!
By following these steps, you've successfully upgraded Nginx on Ubuntu while keeping your configurations intact. Your server is now more secure, optimized, and up to date! ?
Pro Tip: Keep monitoring Nginx logs and stay updated with future releases to maintain optimal performance.