Using cPanel’s standardized hooks system

Working with Standardized Hooks

While cPanel’s standardized hook documentation is great it lacks practical examples of how to add and remove non Perl based hooks. We’ll be looking at how to implement a simple bash script to maintain custom entries to Apache’s PHP configuration file across handler and PHP version changes. The example being used is a script which adds the requisite entries to enable local php.ini files for use with FCGId.

 

Creating the script

First you’ll need to create the script to facilitate the custom php.conf entries. Add the following to the /scripts/post_php_set_handler.sh file and make it executable:

#!/bin/bash
APHPCONF="/etc/apache2/conf.d/php.conf"
sed -i "9i # Enable local php.ini files for FCGId" $APHPCONF
sed -i "10i AddType php55-fastcgi /cgi-bin/php.fcgi" $APHPCONF
sed -i "11i Action php55-fastcgi /cgi-bin/php.fcgi" $APHPCONF
# chmod +x /scripts/post_php_set_handler.sh

 

Adding the script as a hook

Now that you’ve created the script we’ll need to add it to the standardized hook system, we’ll be hooking into the Whostmgr::Lang::PHP::set_handler event using the post stage so the hook is executed after cPanel regenerates the php.conf file:

# /usr/local/cpanel/bin/manage_hooks add script /scripts/post_php_set_handler.sh --category=Whostmgr --event=Lang::PHP::set_handler --stage=post --manual

Added hook for Whostmgr::Lang::PHP::set_handler to hooks registry

 

Running the hook

We’ve created the hook, now lets test it. First, enable debug hook output through ‘WHM -> Server Configuration -> Tweak Settings -> Development’ and select the 3rd option to have hook output logged to cPanel’s error log and click save. Now tail the output of cPanel’s error log and try changing the PHP handler for a PHP version through ‘WHM -> Software -> MultiPHP Manager -> PHP Handlers’, if all went well you should see the following log output and the custom entry will be added to Apache’s PHP configuration file:

# tail -fn 0 /usr/local/cpanel/logs/error_log
[2018-04-18 18:34:41 -0500] info [xml-api] ---debug_hooks---
[2018-04-18 18:34:41 -0500] info [xml-api] msg: Beginning execution of script hook.
[2018-04-18 18:34:41 -0500] info [xml-api] context: Whostmgr::Lang::PHP::set_handler
[2018-04-18 18:34:41 -0500] info [xml-api] stage: post
[2018-04-18 18:34:41 -0500] info [xml-api] point: main
[2018-04-18 18:34:41 -0500] info [xml-api] id: 1fcXJKR2gNyYGyYvOqFmaZk3
[2018-04-18 18:34:41 -0500] info [xml-api] hook: /scripts/post_php_set_handler.sh
[2018-04-18 18:34:41 -0500] info [xml-api] escalateprivs: 0
[2018-04-18 18:34:41 -0500] info [xml-api] weight: 100
[2018-04-18 18:34:41 -0500] info [xml-api] data: {"version":"ea-php55","handler":"fcgi"}
[2018-04-18 18:34:41 -0500] info [xml-api] HOOK INFO: hook /scripts/post_php_set_handler.sh did not output any data
[2018-04-18 18:34:41 -0500] info [xml-api] ---debug_hooks---
[2018-04-18 18:34:41 -0500] info [xml-api] msg: Finished execution of script hook.
[2018-04-18 18:34:41 -0500] info [xml-api] context: Whostmgr::Lang::PHP::set_handler
[2018-04-18 18:34:41 -0500] info [xml-api] stage: post
[2018-04-18 18:34:41 -0500] info [xml-api] result: 1
[2018-04-18 18:34:41 -0500] info [xml-api] point: main
[2018-04-18 18:34:41 -0500] info [xml-api] id: 1fcXJKR2gNyYGyYvOqFmaZk3
[2018-04-18 18:34:41 -0500] info [xml-api] hook: /scripts/post_php_set_handler.sh
[2018-04-18 18:34:41 -0500] info [xml-api] escalateprivs: 0
[2018-04-18 18:34:41 -0500] info [xml-api] weight: 100
[2018-04-18 18:34:41 -0500] info [xml-api] data: {"version":"ea-php55","handler":"fcgi"}
# grep php55-fastcgi /etc/apache2/conf.d/php.conf
AddType php55-fastcgi /cgi-bin/php.fcgi
Action php55-fastcgi /cgi-bin/php.fcgi

If later you want to remove that hook you can do so using the manage_hooks utility:

# /usr/local/cpanel/bin/manage_hooks del script /scripts/post_php_set_handler.sh --category=Whostmgr --event=Lang::PHP::set_handler --stage=post --manual

Deleted hook /scripts/post_php_set_handler.sh for Whostmgr::Lang::PHP::set_handler in hooks registry

Using cPanel’s standardized hook system you can automate all sorts of changes based on events that occur normally on a cPanel server.

One thought on “Using cPanel’s standardized hooks system

  1. Pingback:cPanel Tutorials - Blag

Leave a Reply

Your email address will not be published. Required fields are marked *