Applicable to:
- Plesk for Linux
Question
How to change PHP settings for one or all domains via command line(cli)?
Answer
- Connect to Plesk server using SSH as root user.
- Copy system php.ini to a new location:
# cp -a /etc/php.ini /var/custom-php-settings.ini
- Edit new file
/var/custom-php-settings.ini
and set desired options. - Create backup of Plesk database:
# plesk db dump psa > /root/psa.`date +%F.%s`.sql
- Apply
custom-php-settings.ini
file.
For one domain:
# plesk bin site --update-php-settings example.com -settings /var/custom-php-settings.ini
For all domains:
Note: depending on number of domains this command can require long time to finish (more than 20 minutes).
# mysql -uadmin -p`cat /etc/psa/.psa.shadow` psa -e"select name from domains" | grep -v name |while read i; do plesk bin site --update-php-settings $i -settings /var/custom-php-settings.ini; done
Note: To register additional directives, add them to a separate file (e.g. add.ini) and apply it via the command below:
# plesk bin site --update-php-settings example.com -additional-settings /var/add.ini
-
Connect to a Plesk server via RDP.
-
Start Windows PowerShell and run the following command:
PS plesk bin site --update-php-settings example.com -settings C:\custom-php.ini
The following sample PowerShell script can be used and modified to automate the process for multiple domains:
CONFIG_TEXT: # Run the Plesk database query and store the result
$result = & plesk db "SELECT d.name,h.php_handler_id FROM domains d JOIN hosting h ON h.dom_id=d.id"
# Initialize an array to keep track of updated domains
$updatedDomains = @()
# Split the result into lines and skip the first two lines (header and separator)
$result -split "`n" | Select-Object -Skip 2 | ForEach-Object {
$line = $_.Trim()
if ($line -match '^\|\s*(\S+)\s*\|\s*(\S+)\s*\|$') {
$domain = $matches[1]
$php_handler_id = $matches[2]
Write-Output "Checking PHP version for domain: $domain"
# Check if PHP handler ID is 'fastcgi-8.3'
if ($php_handler_id -eq 'fastcgi-8.3') {
Write-Output "Domain $domain is using PHP 8.3"
Write-Output "Updating PHP settings for domain: $domain"
# Run the Plesk command to update PHP settings for the domain
& plesk bin site --update-php-settings $domain -settings C:\php.ini
Write-Output "PHP settings updated for domain: $domain"
Write-Output "----------------------------------------"
# Add domain to the list of updated domains
$updatedDomains += $domain
} else {
Write-Output "Domain $domain is not using PHP 8.3, skipping..."
Write-Output "----------------------------------------"
}
}
}
# Output the summary of updated domains
Write-Output "All domains using PHP 8.3 that were updated:"
$updatedDomains | ForEach-Object { Write-Output $_ }
Note: The script updates only domains using PHP 8.3.
Comments
2 comments
/etc/php.ini does not exist. That isn't where php.ini lives in a plesk environment. Also, there are separate php.ini's for each version of php on the system so using one custom.php.ini will not suffice.
/etc/php.ini only serves as a template here. You can create new custom php.ini from scratch, or use any php.ini you want. Of course, you are correct that applying the same php settings for different php versions is not recommended. In that case, you should apply the settings only for the chosen domains.
Please sign in to leave a comment.