In this guide I’ll walk through the process of upgrading PHP on an Ubuntu server running Nginx. For my example, I’ll do a PHP 8.0 → 8.2 upgrade, but you should be able to apply the same general principles to other version jumps.
In the notes that follow, when you see reference to php8.x
swap out x
with the relevant version number for the upgrade you’re doing.
Running an Apache web server instead of Nginx? Go here: Upgrading PHP (Ubuntu with Apache)
Output phpinfo
Before you begin the upgrade, create a PHP file on your server that you can access in the browser that invokes PHP’s phpinfo function:
<?php
phpinfo();
This will output useful information about your server and PHP setup that can be used to confirm the update process was successful.
Record list of current PHP related packages
In addition to updating PHP, you will need to update any PHP related packages your server is using. To help with this, run the following command which will create a text file called php-packages.txt
that will contain a list of all your currently installed PHP packages.
> sudo dpkg -l | awk '/^ii/{print $2}' | grep php | tee php-packages.txt
Example resulting php-packages.txt
file:
> cat php-packages.txt
php-common
php8.0-apcu
php8.0-cli
php8.0-common
php8.0-curl
php8.0-fpm
php8.0-mysql
php8.0-opcache
php8.0-readline
Add repository
Next, we’ll use the Linux package manager apt to target a repository called ondrej/php PPA which contains all the PHP software we’ll need:
> sudo add-apt-repository ppa:ondrej/php
> sudo apt update
Install PHP and packages
Now we’re ready to install PHP and its related packages referencing the text file you created in the above step. Here’s a template for a command you can use to do this:
> sudo apt install php8.x {list packages here...}
Note that most packages follow the pattern of php8.x-packagename
, e.g. php8.2-mysql
.
As an example, based on my above packages list and an update to PHP 8.2 my install command would look like this:
> sudo apt install php8.2 php-common php8.2-apcu php8.2-cli php8.2-common php8.2-curl php8.2-fpm php8.2-mysql php8.2-opcache php8.2-readline
Update site’s PHP handler
Next you need to update your Nginx site configs (located in /etc/nginx/sites-enabled
) to use your updated PHP handler. To do this, edit all of your site configs so that fastcgi_pass
points to the appropriate php-fpm version. For example:
server {
listen 80;
server_name demo.codewithsusan.com;
root /var/www/html/;
index index.php index.html;
location ~ \.php$ {
# ↓↓↓
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
Then restart Nginx:
> sudo service nginx restart
Confirm it worked
Refresh your phpinfo page and confirm you’re seeing the updated PHP version number:
You can also confirm that PHP from command line is reporting the correct version with the command php --version
:
> php
PHP 8.2.2 (cli) (built: Feb 7 2023 11:28:53) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.2, Copyright (c) Zend Technologies
with Zend OPcache v8.2.2, Copyright (c), by Zend Technologies
Sync changes to php.ini
Your final step is to make sure any configurations you might have made to PHP in your previous install are reflected in your upgraded install. To do this you need to first locate the path to your new/current php.ini
file from the Loaded Configuration File value in your phpinfo
output:
Example path:
/etc/php/8.2/fpm/php.ini
From this path we can infer the path to your previous php.ini
file by changing the version number:
/etc/php/8.0/fpm/php.ini
Next, you can plug these paths into a helper script I’ve created that will tell you the setting differences between these two files. To do this. create a PHP file on your server that you can access in the browser called ini-diff.php
that contains this code.
Within the code, update the first two variables to reflect your current and previous php.ini
paths. E.g.:
# UPDATE THESE TWO PATHS:
$current = '/etc/php/8.2/fpm/php.ini';
$previous = '/etc/php/8.0/fpm/php.ini';
When you run this file, it will outline any setting differences in your previous and current ini file. E.g.:
Use this information to update your current php.ini file to reflect any settings you wish to persist in your upgraded PHP.
When you’re done, make the changes take effect by reloading your PHP handler:
> systemctl reload php8.2-fpm
You can reload your phpinfo page and search for one of the settings you changed to confirm the changes took affect.
Clean up
When you’re done you can (but don’t have to) purge your old unused PHP packages using the following command (replace 8.x as appropriate):
> sudo apt purge php8.x*