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
Replace PHP handlers conditionally
-
Connect to the server via SSH
-
Download the PHP script from article attachments:
# wget https://support.plesk.com/hc/en-us/article_attachments/4405355532306/switch_php_handler.tar.gz
-
Extract the PHP script:
# tar -xzvf switch_php_handler.tar.gz
-
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 -
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
Switch the PHP version from a domain list
-
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…
-