Why You Need Several PHP Versions
On a server with several sites you often need different PHP versions: one WordPress site requires PHP 8.1+, while another — a legacy Magento 1 — only runs on PHP 7.4. The ondrej/php repository lets you keep several versions at the same time.
Adding the ondrej/php Repository (Ubuntu/Debian)
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt updateInstalling Several PHP Versions
# PHP 7.4
sudo apt install php7.4-fpm php7.4-cli php7.4-mysql php7.4-curl \
php7.4-mbstring php7.4-xml php7.4-zip php7.4-gd
# PHP 8.1
sudo apt install php8.1-fpm php8.1-cli php8.1-mysql php8.1-curl \
php8.1-mbstring php8.1-xml php8.1-zip php8.1-gd
# PHP 8.2
sudo apt install php8.2-fpm php8.2-cli php8.2-mysql php8.2-curl \
php8.2-mbstring php8.2-xml php8.2-zip php8.2-gdChecking the Running PHP-FPM Services
systemctl status php7.4-fpm php8.1-fpm php8.2-fpm
# List of sockets
ls /run/php/
# php7.4-fpm.sock php8.1-fpm.sock php8.2-fpm.sockNginx Setup: Different Versions for Different Sites
# Site 1 — PHP 8.2
server {
server_name newsite.com;
root /var/www/newsite;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
# Site 2 — PHP 7.4 (legacy site)
server {
server_name oldsite.com;
root /var/www/oldsite;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}Choosing the Default PHP CLI Version
# Show the current version
php --version
# List the available versions
update-alternatives --list php
# Change the default version
sudo update-alternatives --set php /usr/bin/php8.2
# Interactive selection
sudo update-alternatives --config phpSwitching the PHP Version in cPanel (via .htaccess)
# In the site's .htaccess
AddHandler application/x-httpd-php82 .php
# or via the PHP-FPM pool — set in the hosting control panelphp.ini for PHP 8.2 does not affect PHP 7.4.Real Errors From Mixing Versions
Socket not found
connect() to unix:/run/php/php7.4-fpm.sock failed (2: No such file or directory), client: 1.2.3.4, server: oldsite.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.4-fpm.sock:"This is an Nginx error, not a PHP one: the socket named in the vhost does not exist. Either the php7.4-fpm service is not running, or its own pool config names the socket differently — check both places, not just Nginx.
Apache with mod_proxy_fcgi produces the same underlying error with different wording — AH01079: failed to make connection to backend. The cause is the same: the web server is reaching for the wrong socket, or the service is down.
Syntax not supported by the old version
PHP Parse error: syntax error, unexpected identifier "string", expecting "{" in /var/www/html/app/Status.php on line 5A typical cause is code using an enum (introduced in PHP 8.1) while the site is mistakenly served by a PHP 7.4 pool. A syntax error with no hint about the version is confusing: the code looks perfectly valid when opened on a developer's machine running PHP 8.2.
When Several Versions Become a Source of Problems
- An extension is installed for one version but not another:
php8.2-imagickis present whilephp7.4-imagickis not, and the legacy site fails with "Class Imagick not found" for no obvious reason at first glance. - Each PHP-FPM version keeps its own worker pool and its own OPcache — with limited RAM, three PHP versions consume memory like three independent servers.
- Cron jobs launched through the plain
phpcommand use the default version fromupdate-alternatives, not the one configured in the site's FPM pool — it is easy to end up with the web and cron on different versions.
Verifying the Result
# Are all the daemons actually running?
systemctl status php7.4-fpm php8.1-fpm php8.2-fpm --no-pager
# Which version is serving a specific site
curl -sI https://newsite.com | grep -i x-powered-by
# Nginx configuration syntax is valid
nginx -tIf systemctl status shows active (running) but the site still returns a 502, check the owner and permissions on the socket file itself: PHP-FPM and Nginx must have access to the same path with matching group permissions.
Another way to confirm which version is actually serving a request is to temporarily add echo phpversion(); to the site's code and compare it with the browser output. It is faster than a full phpinfo(), but good enough for a spot check.
Remove that debug line right after checking — a forgotten echo phpversion(); on a live page leaks the PHP version to anyone, including bots scanning for known vulnerabilities tied to specific releases.