Applicable to:
- Plesk for Linux
Question
How to change the PHP version for multiple domains or for certain domains via CLI in Plesk for Linux?
Answer
Warning: The solution described in this article will restart the web services each time the PHP handler is assigned to the domains, consider applying the solution in maintenance time frame to avoid any downtime
-
Connect to the server via SSH
-
Download the PHP script from article attachments:
# curl -OL https://raw.githubusercontent.com/plesk/kb-scripts/master/apply-phphandler-multidomain/switch_php_handler.php
-
Get the PHP Handler ID of the enabled PHP versions available in the Plesk server:
# plesk bin php_handler --list 2>/dev/null | grep enabled | awk '{print $1}'
plesk-php74-fastcgi
plesk-php74-fpm
plesk-php80-fastcgi
plesk-php80-fpm -
Check the available command arguments:
# plesk php switch_php_handler.php --help
[i] Available command arguments:
--old (Required) Current PHP Handler ID assigned to the domains
--new (Required) New PHP Handler ID to be assigned to the domains
--ngx (Default: false) Set this variable to true when the PHP FPM Handler has to be managed by Nginx
--include-disabled (Default: false) Set this variable to true when the PHP Handler must be updated to the domains with PHP disabled
[i] Check installed and enabled PHP handlers with the following command:
plesk bin php_handler --list -
Switch the PHP handlers according to the requirements. For example, this command will switch domains with PHP enabled and disabled from Plesk PHP-FPM 7.4 to Plesk PHP-FPM 8.0:
# plesk php switch_php_handler.php --old plesk-php74-fpm --new plesk-php80-fpm --include-disabled true
-
Connect to the server via SSH
-
Get the PHP Handler ID of the enabled PHP versions available in the Plesk server:
# plesk bin php_handler --list 2>/dev/null | grep enabled | awk '{print $1}'
plesk-php74-fastcgi
plesk-php74-fpm
plesk-php80-fastcgi
plesk-php80-fpm -
Create a file including the domains with hosting enabled:
Warning: Edit the file
/root/domains_hosting_php_enabled.txt
and/or/root/domains_hosting_php_disabled.txt
and remove the domains that should not be changed-
Domains with the hosting and PHP enabled:
# plesk db -Ne "SELECT d.name FROM domains d, hosting h WHERE d.id=h.dom_id AND d.htype = 'vrt_hst' AND h.php='true'" > /root/domains_hosting_php_enabled.txt
-
Domains with the hosting enabled but PHP disabled:
# plesk db -Ne "SELECT d.name FROM domains d, hosting h WHERE d.id=h.dom_id AND d.htype = 'vrt_hst' AND h.php='false'" > /root/domains_hosting_php_disabled.txt
-
-
Run the command below to change the PHP handler for all domains included in the respective files:
Note: Change the plesk-php80-fpm directive to the required PHP Handler ID retrieved from the 2nd step and specify the nginx-serve-php value keeping in mind that it must be set to false when a PHP FastCGI handler is assigned
-
Domains with the hosting and PHP enabled:
# cat /root/domains_hosting_php_enabled.txt | while read i; do echo "[+] Updating domain: $i"; plesk bin domain -u $i -php_handler_id plesk-php80-fpm -nginx-serve-php false; done
-
Domains with the hosting enabled but PHP disabled:
# cat /root/domains_hosting_php_disabled.txt | while read i; do echo "[+] Updating domain: $i"; plesk bin domain -u $i -php true -php_handler_id plesk-php80-fpm -nginx-serve-php false; plesk bin domain -u $i -php false; done
-
Comments
7 comments
or just switch the vendor fastcgi to newer handler on centos for all domains
/usr/local/psa/bin/php_handler --replace -old-id fastcgi -new-id plesk-php71-fastcgi
@Jan Thats correct, it will change php handler for all domains that used this old php handler. But not for all domains on the server.
This method triggers an httpd restart for each domain. Is there a way to do it without restarting httpd for each domain?
@Oxilion, currently this is the only method for changing handler to multiple/all domains.
To minimize effect from downtime, consider applying this instruction during maintenance time frame.
Keep getting exit status 1 on each domain when running this command.
Any ideas?
Hello @Jaden, is there any additional message before "exit status 1"?
For example:
# plesk bin domain -u example.com -php_handler_id plesk-php70-fpm -nginx-serve-php false
The license key is invalid. In order to use Plesk, please obtain and install a new valid license key.
Your license key has expired. To continue using Plesk, buy a non-expiring commercial license key.
exit status 1
In case there is no any, please, enable Plesk debug, reproduce the issue and check Plesk log at /var/log/plesk/panel.log, there will be more specific error message.
Daniel Broen
I changed the Apache restart interval to 300 (was previously 0), then ran the command - it performed significantly faster than when the restart interval was set to 0. If you do this method don't forget to put it back to 0, and manually restart apache.
Cheers,
C.
Please sign in to leave a comment.