Why Multiple PHP Versions?
Different applications may require different PHP versions. WordPress might run best on 8.2, while a legacy app needs 7.4. Running multiple versions lets you host them all on one server.
Install Multiple Versions
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
# Install PHP 7.4 and 8.2
sudo apt install -y php7.4-fpm php7.4-mysql php7.4-mbstring php7.4-xml
sudo apt install -y php8.2-fpm php8.2-mysql php8.2-mbstring php8.2-xmlNginx Per-Site PHP Version
# Site using PHP 8.2
server {
server_name modern-app.com;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
}
# Site using PHP 7.4
server {
server_name legacy-app.com;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
}CLI Version Switching
# Set default CLI version
sudo update-alternatives --set php /usr/bin/php8.2
# Or use full path
/usr/bin/php7.4 script.php
/usr/bin/php8.2 script.phpPer-Version Configuration
Each version has its own php.ini:
/etc/php/7.4/fpm/php.ini
/etc/php/8.2/fpm/php.ini