Setting the PHP version at boot time



When installing my Kubuntu 22.04 system I got PHP 7.4 with it. I then also installed 8.0 and later 8.1. I use more than just one PHP version because I also run Drupal on it with more than one version. I now mainly use 8.1. So far I have always had to run a "home-produced" bash script to switch PHP versions. I would like to have PHP 8.1 fixed at boot time now so as to save having to execute the switching script. Where is the version fixed at start-up time?
Thanks for your help
H. Stoellinger
Basically you should run in terminal:
sudo update-alternatives --config php

You should also have the configuration for the http server. Do you use apache, nginx or another to serve php code?
For apache you should have module for each version of php that you have.
For nginx you should set the right php-fpm service
Thanks for bothering to help! I use apache and I have the required modules. In my vhost defs in apache2 I presently have the statement s
<FilesMatch \.php$>
# For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
#SetHandler "proxy:unix:/run/php/php8.0-fpm.sock|fcgi://localhost"
When - after booting - I enter php -v I see that the version is 8.1. Nevertheless when trying to access a page on my local server (I have control over it!) I get the errors...
[Thu Jul 14 10:36:53.038856 2022] [proxy:error] [pid 1179] (2)No such file or directory: AH02454: FCGI: attempt to connect to Unix domain socket /run/php/php8.1-fpm.sock (*) failed
[Thu Jul 14 10:36:53.039588 2022] [proxy_fcgi:error] [pid 1179] [client] AH01079: failed to make connection to backend: httpd-UDS
As I mentioned earlier, I do a manual switch of the PHP version by executing the following bash script:
## switch php-version between 8.0 and 8.1 (alias sphp)
## parameter: php-version (8.0, 8.1
## momentan sind 8.0, 8.1 und 7.4 installiert
# set -ve
if [ $# = 0 ]; then
# echo "specify PHP version!"
# exit
else version=$1
set -v
sudo service apache2 stop
if [ $version = 8.0 ]; then
sudo systemctl stop php8.1-fpm
sudo systemctl disable php8.1-fpm
sudo a2dismod php8.1
elif [ $version = 8.1 ]; then
sudo systemctl stop php8.0-fpm
sudo systemctl disable php8.0-fpm
sudo a2dismod php8.0
sudo update-alternatives --set php /usr/bin/php$version
sudo update-alternatives --set phar /usr/bin/phar$version
sudo update-alternatives --set phar.phar /usr/bin/phar.phar$version
sudo update-alternatives --set phpize /usr/bin/phpize$version
sudo update-alternatives --set php-config /usr/bin/php-config$version
sudo systemctl start php$version-fpm
echo "php$version-fpm"
sudo service apache2 start
php -v
you can run many php-fpm services so you can do that:
sudo service apache2 stop
sudo systemctl enable php8.0-fpm
sudo systemctl enable php8.1-fpm
sudo service php8.0-fpm start
sudo service php8.1-fpm start
sudo service apache2 start
sudo a2dismod php8.0
sudo a2dismod php8.1
Because you are using fpm service, then you do not need the php module for the apache server. You need only to configure the right SetHandler
Thanks again! Switching to PHP 8.1 with my bash script works o.k., I would just like to "automate" this switching, e.g. by putting its invocation somewhere so it gets called during boot time...
Use cron to do that. Run in terminal:
crontab -e
Then add the line at the bottom:
@reboot change-php-version 8.1
Then test, if it will be executed to early then you can change it to:
@reboot sleep 20 && change-php-version 8.1
To wait 20 seconds before change the php version
Hello again,
When I do php -v I get the answer
PHP 8.1.8 (cli) (built: Jul 11 2022 08:53:35) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.8, Copyright (c) Zend Technologies
with Zend OPcache v8.1.8, Copyright (c), by Zend Technologies
with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans
Then, when trying to access a page on my (LAN-connected) server,
and doing tail -50 www_error.log, I see the following lines in
the error.log:
[Mon Jul 25 17:00:10.932426 2022] [proxy:error] [pid 1090] (2)No such file or directory: AH02454: FCGI: attempt to connect to Unix domain socket /run/php/php8.1-fpm.sock (*) failed
[Mon Jul 25 17:00:10.932500 2022] [proxy_fcgi:error] [pid 1090] [client] AH01079: failed to make connection to backend: httpd-UDS, referer: ...
It seems to me that apache2 doesn't understand that it should
use the 8.1 fcgi-handler. Could it be that a corresponding entry
is missing in .htaccess? I do have the following definition
in vhost.conf:
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
Obviously I am missing something very basic.
Thanks for helping and greetings from Austria
H. Stoellinger
The php-fpm is a service so you need to start it:
sudo service php8.1-fpm start
Then the apache will be able to connect with the php server
Thanks a lot! That did it! One never stops learning! Greetings from Salzburg
H. Stoellinger