Often there are tasks to install the required version of php, but by default a newer or older one is installed. It may also be necessary to install and simultaneously work with two versions of php on the same server.
By default, we install as the root user, if you have a regular user, use sudo.
1. Let's install the necessary repositories:
apt update
apt upgrade
apt install -y apt-transport-https lsb-release ca-certificates wget
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list
apt update
2. If we need version php 8.2, install it:
apt install php8.2
2.1 To install php 8.1, complete the installation:
apt install php8.1
2.2 To install php 7.4, complete the installation:
apt install php7.4
2.3 To install php 7.3, complete the installation:
apt install php7.3
And so on, while all versions will be simultaneously available for work on the server; for each PHP version you need to install its own modules, as well as edit the configuration file if necessary.
Several simultaneously installed php allows you to work with several projects in case of different requirements. For each php, you can install your own php-fpm and specify, by analogy, which port it will work on and specify these parameters in the nginx configuration file.
3. Example of installing php-fpm and php modules for php 8.2:
apt install php8.2 php8.2-gd
3.1 Example of installing php-fpm and php modules for php 8.1:
apt install php8.1 php8.1-gd
4. To check the version, as well as use the scheduler, use the path for each version:
/usr/bin/php8.2 /var/www/domain.tld/cron-file.php
or
/usr/bin/php8.1 /var/www/domain.tld/cron-file.php
5. If you have multiple versions of php, to switch the default version, use the following command:
update-alternatives --set php /usr/bin/php8.1