This guide makes use of WordPress Toolkit and cPanel's Standard Hooks system to implement automatic WordPress installations for new cPanel accounts.
Install WordPress Toolkit
WordPress Toolkit will be used to install and manage the WordPress sites on the server.
First, install WordPress Toolkit if it is not already installed with either SSH or Terminal as the root user:
sh <(curl https://wp-toolkit.plesk.com/cPanel/installer.sh)
Installing The Hook Action Script in Two Simple Steps
1. Create the following script located at:
/usr/local/cpanel/3rdparty/bin/wpAutoInstall.sh
#!/bin/bash
TMPFILE="$(mktemp -p /tmp wp-auto-install-XXXXXXXX)"
cat "${1:-/dev/stdin}" > $TMPFILE
DOMAIN=$(python -c "import sys, json; print json.load(open('$TMPFILE'))['data']['domain']")
CUSTOMEREMAIL=$(python -c "import sys, json; print json.load(open('$TMPFILE'))['data']['contactemail']")
## The following variable contains the name of the Package that was assigned to the account.
## It would be possible to add additinal logic to this script so that WordPress is only installed for certain packages
# PACKAGE=$(python -c "import sys, json; print json.load(open('$TMPFILE'))['data']['plan']")
rm -f $TMPFILE
if [ ! -z $CUSTOMEREMAIL ];then
wp-toolkit --install -domain-name $DOMAIN -admin-email $CUSTOMEREMAIL
else
# If a contact email address was not provided while creating the account, WordPress Toolkit will use the default of admin@DOMAIN.TLD as the WordPress Admin email
wp-toolkit --install -domain-name $DOMAIN
fi
2. Execute the following commands via SSH or Terminal as the root user:
chmod 0755 /usr/local/cpanel/3rdparty/bin/wpAutoInstall.sh
chown root:root /usr/local/cpanel/3rdparty/bin/wpAutoInstall.sh
/usr/local/cpanel/bin/manage_hooks add script /usr/local/cpanel/3rdparty/bin/wpAutoInstall.sh --manual --category Whostmgr --event Accounts::Create --stage=post
Once the above steps are complete, create a new account to test the script.
Log in to the newly created cPanel account and look for the WordPress Toolkit icon under the Applications Section to manage the WordPress site.
Examining The wpAutoInstall.sh Script
The script is executed when an account is created because it was registered in cPanel's Standard Hooks system in step two of the above section. You can read more about the Standard Hooks system in this guide: How to Setup Standardized Hooks with BASH in cPanel
The following code pulls data from STDIN and then saves the data to a temporary file so that we can pull multiple values from the data. cPanel's Standardized Hooks system publishes the output from the account creation event to STDIN.
TMPFILE=$(mktemp -p /tmp wp-auto-install-XXXXXXXX)"
cat "${1:-/dev/stdin}" > $TMPFILE
The data published to STDIN is in JSON format. In order to make use of this data, you'll need a way to parse the JSON data. In the next part of our script, we parse some data out of the temporary file and assign that data to a few variables with Python:
DOMAIN=$(python -c "import sys, json; print json.load(open('$TMPFILE'))['data']['domain']")
CUSTOMEREMAIL=$(python -c "import sys, json; print json.load(open('$TMPFILE'))['data']['contactemail']")
Now that we've obtained the needed values from the event data, we remove our temporary file:
rm -f $TMPFILE
The next part uses the wp-toolkit command-line tool to do the actual installation of WordPress:
wp-toolkit --install -domain-name $DOMAIN -admin-email $CUSTOMEREMAIL
You can read more about the wp-toolkit command line here: