1. List existing PHP packages
When updating an existing PHP setup, it is easier to list the existing PHP extensions installed as software packages, to match the PHP 8.1 extensions list.
On systems that install PHP 8.1 afresh, this step is not required.
dpkg -l | grep php | tee packages.txt
This command lists all PHP packages installed, displays them on-screen, and saves to a file named packages.txt
in the current working directory.
2. Update Debian System
Update your Debian operating system to make sure all existing packages are up to date:
sudo apt update && sudo apt upgrade -y
3. Install Required Dependencies
You will need to have the following packages installed for this tutorial. Execute the following command to install:
sudo apt-get install ca-certificates apt-transport-https software-properties-common wget curl lsb-release -y
4. Import Ondřej Surý PHP Repository
The first step is to import and install the GPG key and repository which can be done using an automated script initiated by the curl command. In your terminal, use the following command.
curl -sSL https://packages.sury.org/php/README.txt | sudo bash -x
Next, refresh your APT repository list to reflect the changes.
sudo apt update
After running the update command, you may notice some packages require updating, make sure to do this before continuing.
sudo apt upgrade
5.1 Install PHP 8.1 with Nginx Option
Nginx does not contain native PHP processing like some other web servers like Apache. You will need to install PHP-FPM “fastCGI process manager” to handle the PHP files.
First, check for updates on your system and install PHP-FPM, natively installing the PHP packages required.
In your terminal, use the following command to install PHP 8.1 and PHP 8.1-FPM.
sudo apt install php8.1 php8.1-fpm php8.1-cli -y
Once installed, the PHP-FPM service should be automatically started, if not run the following command.
sudo systemctl enable php8.1-fpm --now
Confirm the installation by running:
php -v # Show PHP version.
php -m # Show PHP modules loaded.
5.2 You will need to edit your Nginx server block and add the example below for Nginx to process the PHP files.
Below, example for all server blocks that process PHP files that need the location ~ .php$ added.
server {
# … some other code
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
5.3 For PHP 8.1 FPM with Nginx the php.ini
location will be in following directory.
sudo nano /etc/php/8.1/fpm/php.ini
Hit F6
for search inside the editor and update the following values for better performance.
upload_max_filesize = 32M
post_max_size = 48M
memory_limit = 256M
max_execution_time = 600
max_input_vars = 3000
max_input_time = 1000
For users with Nginx to who use PHP-FPM, you need to restart PHP-FPM.
sudo systemctl restart php8.1-fpm
Test Nginx to make sure you have no errors with the adjustments made with the code above; enter the following.
sudo /usr/sbin/nginx -t
Example output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart Nginx service for installation to be complete.
sudo systemctl restart nginx
6. Purge old PHP versions
If the new installation is working as expected, you can remove the old PHP packages from the system.
sudo apt purge '^php8.0.*'
This assumes you are using PHP 8.0 as the previous version. Change php8.0
part of the command above with the appropriate PHP version.