Nk_enable_fpm

Home

Description

The function nk_enable_fpm() is used to enable PHP-FPM for a domain and optionally set the max_children and max_requests values.

This function takes a domain name as its first parameter, and if not provided, it will fail and return an error message. It then uses the nk_php_version() function to determine the PHP version of the domain and stores it in the variable $php_version. It also gets the user associated with the domain using the nk_user function.

Next, it creates a PHP-FPM YAML file path at /var/cpanel/userdata/$user/$domain.php-fpm.yaml and uses the whmapi1 command to enable PHP-FPM for the domain by setting the php_fpm value to 1 and setting the version value to $php_version.

If the optional max_children and max_requests parameters are provided, the function sets the $max_children and $max_requests variables to these values and replaces the existing values in the YAML file with the new ones using sed commands.

Finally, it prints out the domain name and the contents of the PHP-FPM YAML file using the cat command. It also restarts the Apache PHP-FPM service to apply any changes made.

Example:

[root@cloudvpsserver ~]# nk_enable_fpm nkern.net 5 500
nkern.net
---
_is_present: 1
pm_max_children: 5
pm_max_requests: 500
pm_process_idle_timeout: 10

Code:

nk_enable_fpm() {
# nk_enable_fpm expects to be passed a domain name. Fail if not.
domain="$1"
if [ "$1" = "" ]; then
    echo "You must provide a domain name"
    return 0
fi

# We also need to know the php version. So store the results of nk_php_version to the variable $php_version
php_version="$(nk_php_version "$domain")"
user="$(nk_user "$domain")"
yaml_file="/var/cpanel/userdata/$user/$domain.php-fpm.yaml"
# Now that we know the domain name and php version we can update the domain to use php fpm using whmapi.
whmapi1 php_set_vhost_versions version="$php_version" vhost="$domain" php_fpm=1 >/dev/null

# However if you provided max_children and max_requests then we can do more stuff.
if [ "$2" != "" ] && [ "$3" != "" ]; then
    max_children="$2"
    max_requests="$3"
    # Yaml file should have already been created when we enabled fpm.
    # Replace the existing values with the ones we got from the user.
    sed -i "s/pm_max_children: [0-9]\+/pm_max_children: $max_children/g" "$yaml_file"
    sed -i "s/pm_max_requests: [0-9]\+/pm_max_requests: $max_requests/g" "$yaml_file"
fi

# print out the yaml file instead of the whmapi output. It's more useful.
# also echo the domain so the output is more useful in loops.
# and I'm fairly certain we have to restart fpm to have the values take effect.
/scripts/restartsrv_apache_php_fpm >/dev/null
echo "$domain"
cat "$yaml_file"
}

Author: Nichole Kernreicht

Created: 2023-04-09 Sun 21:10