cPanelResources

Tutorial How To Automatically Install WordPress On New Accounts

cPanelResources

Staff
Staff member
Apr 27, 2015
112
73
153
Houston, TX
cPanel Access Level
DataCenter Provider
cPanelResources submitted a new resource:

How To Automatically Install WordPress On New Accounts - Learn how to automatically install WordPress on new accounts using BASH.

Introduction
WordPress has become a defacto standard for many users over the years, and we're seeing hosting providers moving into the arena of specialty WordPress only hosting.

If you are looking to provide WordPress specific hosting, one of the basic requirements that you'll need to fulfill is ensuring that WordPress is instantly installed and available upon account creation. With a short bash script integrated with cPanel's WordPress Manager and Standardized hooks, this is a...
Read more about this resource...
 

Kevin Perrow

Registered
Feb 19, 2019
1
0
1
Gold Coast
cPanel Access Level
Root Administrator
Would be awesome if cPanel stepped up and offered an advance WordPress feature like Plesk offers its a really good tool for web agency. The number one control panel and the number one cms platform...
Would be a winner for everyone.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,222
463
Now is there a way after running curl to run wp cli commands within the script to install and activate plugins?
Hello @Cody Leeper,

Yes, you could append new lines at the end of the example script if you want to add additional commands (e.g. wp-cli). You'll just need to design the commands to detect and run on the individual WordPress installation under the cPanel account.

Would be awesome if cPanel stepped up and offered an advance WordPress feature like Plesk offers its a really good tool for web agency. The number one control panel and the number one cms platform...
Would be a winner for everyone.
We are committed to improving the WordPress Manager feature going forward. You can take a look at the recent improvements at:

WordPress Manager Release Notes - cPanel Knowledge Base - cPanel Documentation

Staging/Production syncing is one of the next additions to WordPress Manager feature that's planned for future versions.

Thank you.
 

chengkinhung

Member
Jun 15, 2007
23
3
53
This feature is great ! Thanks for providing this feature. Base on this feature, I created my own script: /usr/local/cpanel/3rdparty/bin/wpAutoInstall.sh with the following content:

Code:
#!/bin/bash

tmpfile="$(mktemp -p /tmp wp-auto-install-XXXXXXXX)"
cat "${1:-/dev/stdin}" > $tmpfile
cpuser=$(python -c "import sys, json; print json.load(open('$tmpfile'))['data']['user']")
domain=$(python -c "import sys, json; print json.load(open('$tmpfile'))['data']['domain']")
cppass=$(python -c "import sys, json; print json.load(open('$tmpfile'))['data']['pass']")
/bin/rm -f $tmpfile

srcroot="/root/tmp/wp_src/*"
sqlfile="/root/tmp/wp_sql/wpdb.sql"
webroot="/home/${cpuser}/public_html"
webconf="${webroot}/wp-config.php"

wpuser="admin"
wppass=$(< /dev/urandom tr -dc [email protected] | head -c${1:-20};echo;)
dbpass=$(< /dev/urandom tr -dc [email protected] | head -c${1:-16};echo;)
dbname="${cpuser}_wpdb"
dbuser="${cpuser}_wpuser"
dbhost="localhost"

/usr/bin/uapi --user=${cpuser} Mysql create_database name=${dbname}
/usr/bin/uapi --user=${cpuser} Mysql create_user name=${dbuser} password="${dbpass}"
/usr/bin/uapi --user=${cpuser} Mysql set_privileges_on_database user=${dbuser} database=${dbname} privileges=ALL
/usr/bin/mysql -u${dbuser} -p"${dbpass}" ${dbname} < ${sqlfile}

/usr/bin/rsync -av ${srcroot} ${webroot}
/usr/bin/sed -i "s/MYWPDB_DATABASE/${dbname}/g" ${webconf}
/usr/bin/sed -i "s/MYWPDB_USERNAME/${dbuser}/g" ${webconf}
/usr/bin/sed -i "s/MYWPDB_PASSWORD/${dbpass}/g" ${webconf}
/usr/bin/sed -i "s/MYWPDB_HOSTNAME/${dbhost}/g" ${webconf}

/usr/bin/chown -R ${cpuser}:${cpuser} ${webroot}

sudo -u ${cpuser} /opt/cpanel/ea-php72/root/usr/bin/php /usr/local/cpanel/3rdparty/share/cpanel-wp-cli/wp-cli.phar --path=${webroot} user update ${wpuser} --user_pass="${wppass}"
sudo -u ${cpuser} /opt/cpanel/ea-php72/root/usr/bin/php /usr/local/cpanel/3rdparty/share/cpanel-wp-cli/wp-cli.phar --path=${webroot} option update home "http://${domain}"
sudo -u ${cpuser} /opt/cpanel/ea-php72/root/usr/bin/php /usr/local/cpanel/3rdparty/share/cpanel-wp-cli/wp-cli.phar --path=${webroot} option update siteurl "http://${domain}"
My script can clone a custom designed wordpress website to new created cPanel user automatically. Just copy your wordpress website files into source path(/root/tmp/wp_src/) and export your wordpress database into a SQL text file and upload into source path(/root/tmp/wp_sql/wpdb.sql). You may change the source path if you like, and adjust any options as your wish.

Current my script capable to:

(1) rsync wordpress files from source path:/root/tmp/wp_src/ to cPanel user's public_html/
(2) use uapi command to create cPanel user's wordpress databalse with the following values:
Code:
  DATABASE: {CPUSER}_wpdb
  USERNAME: {CPUSER}_wpuser
  PASSWORD: ****(random)
  HOSTNAME: localhost
(3) Import wordpress SQL content from file:/root/tmp/wp_sql/wpdb.sql into above database.
(4) Change configuration of public_html/wp-config.php with above database information.
(5) Change wordpress administrator user(admin)'s password with random password(or with cPanel user's password).
(6) Update wordpress settings: HOME_URL and SITE_URL with the new created domain information.
(7) cPanel user can discovery/manage wordpress with cPanel addon "WordPress Manager".
 
Last edited:

Alnaggar

Member
May 25, 2021
7
1
3
Riyadh
cPanel Access Level
Root Administrator
This feature is great ! Thanks for providing this feature. Base on this feature, I created my own script: /usr/local/cpanel/3rdparty/bin/wpAutoInstall.sh with the following content:

Code:
#!/bin/bash

tmpfile="$(mktemp -p /tmp wp-auto-install-XXXXXXXX)"
cat "${1:-/dev/stdin}" > $tmpfile
cpuser=$(python -c "import sys, json; print json.load(open('$tmpfile'))['data']['user']")
domain=$(python -c "import sys, json; print json.load(open('$tmpfile'))['data']['domain']")
cppass=$(python -c "import sys, json; print json.load(open('$tmpfile'))['data']['pass']")
/bin/rm -f $tmpfile

srcroot="/root/tmp/wp_src/*"
sqlfile="/root/tmp/wp_sql/wpdb.sql"
webroot="/home/${cpuser}/public_html"
webconf="${webroot}/wp-config.php"

wpuser="admin"
wppass=$(< /dev/urandom tr -dc [email protected] | head -c${1:-20};echo;)
dbpass=$(< /dev/urandom tr -dc [email protected] | head -c${1:-16};echo;)
dbname="${cpuser}_wpdb"
dbuser="${cpuser}_wpuser"
dbhost="localhost"

/usr/bin/uapi --user=${cpuser} Mysql create_database name=${dbname}
/usr/bin/uapi --user=${cpuser} Mysql create_user name=${dbuser} password="${dbpass}"
/usr/bin/uapi --user=${cpuser} Mysql set_privileges_on_database user=${dbuser} database=${dbname} privileges=ALL
/usr/bin/mysql -u${dbuser} -p"${dbpass}" ${dbname} < ${sqlfile}

/usr/bin/rsync -av ${srcroot} ${webroot}
/usr/bin/sed -i "s/MYWPDB_DATABASE/${dbname}/g" ${webconf}
/usr/bin/sed -i "s/MYWPDB_USERNAME/${dbuser}/g" ${webconf}
/usr/bin/sed -i "s/MYWPDB_PASSWORD/${dbpass}/g" ${webconf}
/usr/bin/sed -i "s/MYWPDB_HOSTNAME/${dbhost}/g" ${webconf}

/usr/bin/chown -R ${cpuser}:${cpuser} ${webroot}

sudo -u ${cpuser} /opt/cpanel/ea-php72/root/usr/bin/php /usr/local/cpanel/3rdparty/share/cpanel-wp-cli/wp-cli.phar --path=${webroot} user update ${wpuser} --user_pass="${wppass}"
sudo -u ${cpuser} /opt/cpanel/ea-php72/root/usr/bin/php /usr/local/cpanel/3rdparty/share/cpanel-wp-cli/wp-cli.phar --path=${webroot} option update home "http://${domain}"
sudo -u ${cpuser} /opt/cpanel/ea-php72/root/usr/bin/php /usr/local/cpanel/3rdparty/share/cpanel-wp-cli/wp-cli.phar --path=${webroot} option update siteurl "http://${domain}"
My script can clone a custom designed wordpress website to new created cPanel user automatically. Just copy your wordpress website files into source path(/root/tmp/wp_src/) and export your wordpress database into a SQL text file and upload into source path(/root/tmp/wp_sql/wpdb.sql). You may change the source path if you like, and adjust any options as your wish.

Current my script capable to:

(1) rsync wordpress files from source path:/root/tmp/wp_src/ to cPanel user's public_html/
(2) use uapi command to create cPanel user's wordpress databalse with the following values:
Code:
  DATABASE: {CPUSER}_wpdb
  USERNAME: {CPUSER}_wpuser
  PASSWORD: ****(random)
  HOSTNAME: localhost
(3) Import wordpress SQL content from file:/root/tmp/wp_sql/wpdb.sql into above database.
(4) Change configuration of public_html/wp-config.php with above database information.
(5) Change wordpress administrator user(admin)'s password with random password(or with cPanel user's password).
(6) Update wordpress settings: HOME_URL and SITE_URL with the new created domain information.
(7) cPanel user can discovery/manage wordpress with cPanel addon "WordPress Manager".

I have tried it and it partially works, When a new account created it copy the files from the /root/tmp/wp_src/) and successfully create a new database and import (/root/tmp/wp_sql/wpdb.sql) to it.

The problem is it doesn't change the configuration of public_html/wp-config.php (newly created site) with the database information created, And it doesn't update WordPress settings: HOME_URL and SITE_URL in the database with the newly created domain information.

It seems that WP CLi commands from the script don't work, I tried with ea-php74, I have installed WP CLi for all users and everything seems to work fine. What may I have missed?
 

Alnaggar

Member
May 25, 2021
7
1
3
Riyadh
cPanel Access Level
Root Administrator
I have found the problem and it was my bad, The script is perfect and many thanks to @chengkinhung for sharing it with public you really a great man, First I didn't match the data correctly in the /root/tmp/wp_src/wp-config.php So they can be replaced with new created value :
Code:
MYWPDB_DATABASE
MYWPDB_USERNAME
MYWPDB_PASSWORD
MYWPDB_HOSTNAME
Second, I didn't give the right permission to the script after I edited it:
Code:
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
Again many thanks to @chengkinhung for this amazing script.