Introduction
This guide makes use of WP Toolkit and cPanel's Standard Hooks system to implement automatic WordPress installations for new cPanel accounts. WP Toolkit will have to be installed if it has not already been installed. WP Toolkit can be installed from either the command line or WHM.
How do I install WP Toolkit via the command line?
How to install WP Toolkit from the WHM Marketplace
Procedure
- SSH into the server as the 'root' user.
- Create the /usr/local/cpanel/3rdparty/bin/wpAutoInstall.sh with your preferred text editor.
- Add the following content to the file.
#!/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, WP Toolkit will use the default of admin@DOMAIN.TLD as the WordPress Admin email
wp-toolkit --install -domain-name $DOMAIN
fi - Save the changes to the file.
- Exit the text editor.
- 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 - Create a new account to test the script.
- Log in to the newly created cPanel account.
- Click the "WP Toolkit" icon under the Domains Section.
- Verify that the new WordPress site is listed.
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
After installing WordPress, you can get the instance ID by using the list command:
wp-toolkit list
After you have the instance ID you can perform any WP-CLI action using the --wp-cli flag:
wp-toolkit --wp-cli -instance-id $INSTANCE_ID -- plugin install $PLUGIN_NAME
You can read more about the wp-toolkit command line here: