Upgrading core server components like PHP and the Ubuntu operating system is essential for maintaining security, compatibility, and performance. But when it comes to production environments, one mistake can lead to critical downtime or broken apps.
In this guide, we’ll walk you through a safe, tested method to upgrade PHP and Ubuntu on a live production server — using a staging clone to eliminate risk.
Why Upgrading Matters
If your server is running older versions like Ubuntu 18.04 or PHP 7.4, you’re at risk for:
- Incompatibility with modern apps
- Security vulnerabilities
- Lack of performance improvements
To avoid these issues, upgrading to Ubuntu 20.04+ and PHP 8.1 or 8.2 is now considered best practice.Step 1: Clone the Production Server for Safe Testing
Before touching anything live:
- Log into your hosting provider (e.g., DigitalOcean, AWS, or GCP)
- Take a snapshot or image backup of your production server
- Launch a new staging server from that snapshot
- SSH into the staging server and verify everything is intact
Step 2: Upgrade Ubuntu (18.04 to 20.04)
Ubuntu upgrades must happen sequentially. From 18.04, upgrade to 20.04 first:
sudo apt update && sudo apt upgrade -y
sudo apt dist-upgrade -y
sudo apt install update-manager-core -y
sudo do-release-upgrade
Reboot after upgrade:
sudo reboot
Check your version:
lsb_release -a
✅ Output should show: Ubuntu 20.04.6 LTS
Step 3: Remove Legacy PHP Versions (If Any)
sudo apt purge php7.* php8.0* php8.4* -y
sudo apt autoremove -y
Step 4: Install PHP 8.2 and Extensions
Add PHP Repository:
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
Install PHP 8.2 and common extensions:
sudo apt install php8.2 php8.2-cli php8.2-fpm php8.2-mysql php8.2-curl php8.2-xml php8.2-mbstring php8.2-zip php8.2-gd php8.2-soap php8.2-bcmath -y
Step 5: Switch to PHP 8.2 for Apache or Nginx
For Apache:
sudo a2dismod php7.4
sudo a2enmod php8.2
sudo update-alternatives --set php /usr/bin/php8.2
sudo systemctl restart apache2
For Nginx:
Update the site’s config to use:
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
Then restart:
sudo systemctl restart php8.2-fpm
sudo systemctl restart nginx
Step 6: Verify Everything Works
- Check PHP version:
php -v
- Browse any hosted application
- Review logs (
/var/log/nginx/error.log
,/var/log/apache2/error.log
) - Check for broken services or deprecated code warnings
- Step 7: Repeat Process on Production Server
Once you’ve verified that everything works perfectly on staging:
- Take a fresh snapshot of your live server
- Repeat the same commands on your production environment
- Carefully monitor the server for issues post-deployment
- Step 8: Clean Up (Optional)
Remove any lingering older packages:
sudo apt purge php7.* php8.0* php8.4* -y
sudo apt autoremove -y
Conclusion
Upgrading PHP and Ubuntu in production doesn’t have to be risky. With proper cloning, testing, and command-line preparation, you can modernize your server infrastructure with confidence.
Whether you’re hosting APIs, apps, or websites — upgrading responsibly is part of maintaining a healthy, future-ready system.