Upgrading PHP on Ubuntu can feel like teaching a cat to swim. It sounds risky. It may look messy. But with a calm plan, it is very doable. Think of it as changing the engine oil in your website. You do not want to do it while racing down the highway.
TLDR: Back up your site, database, and PHP config files first. Check your current PHP version and what your apps need. Install the new PHP version, test it, then switch your web server to use it. Keep the old version for a while, so you can roll back if your site throws a tiny digital tantrum.
Why upgrade PHP?
PHP powers many websites. WordPress, Laravel, Drupal, and many custom apps use it. Newer PHP versions are usually faster. They also get security fixes. That is a big deal.
Old PHP is like old milk. It may still sit there. But you should not trust it.
An upgrade can bring:
- Better security against bugs and attacks.
- Faster performance for your site.
- New features for developers.
- Better support from modern apps and plugins.
But do not rush. Safe upgrades are slow and boring. Boring is good. Boring means your site stays alive.
Step 1: Check your current PHP version
Open your terminal. Ask Ubuntu what PHP version is running.
php -v
You may see something like PHP 7.4, PHP 8.1, or PHP 8.2. Write it down. This is your starting point.
Also check which PHP packages are installed:
dpkg -l | grep php
This command may show many packages. Do not panic. PHP likes to travel with friends. You may see extensions like php-mysql, php-curl, php-xml, and php-mbstring.
Step 2: Check what your website supports
This part matters. A lot.
Your app may not support every PHP version. For example, an old WordPress plugin may dislike PHP 8.2. A dusty custom script may cry when it sees PHP 8.3.
Check these places:
- Your app documentation.
- Your CMS admin panel.
- Your plugin or theme requirements.
- Your framework version notes.
- Your hosting or server notes.
If you run WordPress, update WordPress first. Update plugins too. Update themes. Then upgrade PHP. This lowers the chance of weird errors.
Step 3: Make backups like a responsible wizard
Before you touch PHP, make backups. Not “I think I have one” backups. Real backups.
Back up these things:
- Website files, such as
/var/www/html. - Databases, such as MySQL or MariaDB.
- PHP config files, often in
/etc/php/. - Web server config, such as Apache or Nginx files.
Example file backup:
sudo tar -czf site-backup.tar.gz /var/www/html
Example database backup:
mysqldump -u root -p database_name > database-backup.sql
Also copy PHP settings:
sudo cp -r /etc/php /etc/php-backup
Now you have a parachute. Good job.
Step 4: Update Ubuntu packages
Before adding or upgrading PHP, refresh your package list.
sudo apt update
sudo apt upgrade
This makes sure Ubuntu knows what packages are available. It also installs current security updates. That is always nice.
Step 5: Add a trusted PHP repository if needed
Ubuntu includes PHP packages. But the version depends on your Ubuntu release. If you need a newer PHP version, many admins use the popular Ondřej Surý PPA.
Install tools first:
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https
Add the PPA:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Important: Only use repositories you trust. A repository can install server software. Treat it like giving someone keys to your kitchen.
Step 6: Install the new PHP version
Now install the version you want. For this example, we use PHP 8.3.
sudo apt install php8.3 php8.3-cli php8.3-common
Most websites need extensions. Install the ones your old PHP had. Common ones include:
sudo apt install php8.3-mysql php8.3-curl php8.3-xml php8.3-mbstring php8.3-zip php8.3-gd php8.3-intl
If you use Apache with PHP as a module, install:
sudo apt install libapache2-mod-php8.3
If you use Nginx, you probably need PHP-FPM:
sudo apt install php8.3-fpm
Do not remove the old PHP version yet. Let it stay on the couch for now. It may save you later.
Step 7: Switch the command line PHP version
Your server may have several PHP versions installed. Check them:
sudo update-alternatives --config php
Pick the new version from the list. Then check:
php -v
You should now see the new PHP version.
Step 8: Switch Apache or Nginx
This part depends on your web server.
If you use Apache
Disable the old PHP module:
sudo a2dismod php8.1
Enable the new one:
sudo a2enmod php8.3
Restart Apache:
sudo systemctl restart apache2
Change the version numbers to match your server.
If you use Nginx with PHP-FPM
Nginx usually talks to a PHP-FPM socket. Your site config may point to the old socket.
Open your Nginx site file. It may be in /etc/nginx/sites-available/. Look for a line like this:
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
Change it to:
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
Then test and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
Restart PHP-FPM too:
sudo systemctl restart php8.3-fpm
Step 9: Test the site
Now comes the suspense music.
Open your website. Click around. Log in. Submit a form. Check checkout pages. Test contact forms. Visit admin pages. Do the things real users do.
Also check logs:
sudo tail -f /var/log/apache2/error.log
For Nginx:
sudo tail -f /var/log/nginx/error.log
For PHP-FPM:
sudo tail -f /var/log/php8.3-fpm.log
If you see warnings, read them. If you see fatal errors, do not scream. Well, maybe scream a little. Then fix the app or roll back.
Step 10: Roll back if needed
This is why you kept the old PHP version. Smart move.
For Apache, disable the new version and enable the old one:
sudo a2dismod php8.3
sudo a2enmod php8.1
sudo systemctl restart apache2
For Nginx, change the socket back to the old PHP-FPM version. Then reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
You can also switch command line PHP back:
sudo update-alternatives --config php
A rollback is not failure. It is a seatbelt.
Step 11: Clean up later
Wait a few days before removing the old PHP version. Watch logs. Ask users if anything is broken. Check cron jobs too. They may use command line PHP.
When you are sure all is well, you can remove old packages:
sudo apt remove php8.1*
Be careful with wildcards. Read the list before pressing yes. Ubuntu will tell you what it plans to remove.
Final safety tips
- Upgrade during quiet traffic hours.
- Use a staging server if you can.
- Never skip backups.
- Keep notes of every command you run.
- Check extensions after upgrading.
- Test before and after the switch.
PHP upgrades do not have to be scary. Go step by step. Keep backups close. Test like a detective. Then enjoy a faster, safer server with fewer gremlins hiding in the pipes.

