Articles in this section

How to change PHP settings for one domain or all domains in Plesk using command line?

kb: how-to ABT: Group A

Applicable to:

  • Plesk for Linux

Question

How to change PHP settings for one or all domains via command line(cli)?

Answer

Plesk for Linux
  1. Connect to Plesk server using SSH as root user.
  2. Copy system php.ini to a new location:

    # cp -a /etc/php.ini /var/custom-php-settings.ini

  3. Edit new file /var/custom-php-settings.ini and set desired options.
  4. Create backup of Plesk database:

    # plesk db dump psa > /root/psa.`date +%F.%s`.sql

  5. 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

Plesk for Windows
  1. Connect to a Plesk server via RDP.

  2. 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.

Was this article helpful?

Comments

2 comments
Date Votes
  • /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.

    0
  • /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.

    0

Please sign in to leave a comment.