cPanelResources

Tutorial How To Automatically Install WordPress On New Accounts

cPanelResources

Staff
Staff member
Apr 27, 2015
112
83
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,880
2,268
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

Active Member
Jun 15, 2007
27
4
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 _@A-Z-a-z-0-9 | head -c${1:-20};echo;)
dbpass=$(< /dev/urandom tr -dc _@A-Z-a-z-0-9 | 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 _@A-Z-a-z-0-9 | head -c${1:-20};echo;)
dbpass=$(< /dev/urandom tr -dc _@A-Z-a-z-0-9 | 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.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
16,598
2,620
363
cPanel Access Level
Root Administrator

johnhenrique

Member
Mar 8, 2022
5
0
1
Brasil
cPanel Access Level
Website Owner
@johnhenrique - yes, this needs to be setup as the root user. The details for the hook can be found here:


If you were trying this work as a reseller it may not work properly.
I follow other doc from cPanel and add "--escalateprivs=1" (see image) in instruction to add script to manage_hooks but, not working yet :-(


I don`t install the WordPress toolkit because my cPanel/WHM already WordPress toolkit installed. It`s the same WordPress Toolkit?
 

Attachments

cPanelAdamF

cPanel Product Owner
Staff member
Mar 21, 2013
297
136
168
Houston TX
cPanel Access Level
DataCenter Provider
Twitter
Starting with v102, Jupiter will now offer to install WordPress on the primary domain of an account if the account's document root is empty and the user hasn't otherwise opted-out:
download.png

This occurs only if WordPress Toolkit is installed and enabled for the account, the primary document root is empty, and the user hasn't otherwise skipped or dismissed this.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
16,598
2,620
363
cPanel Access Level
Root Administrator
If you have root access, I would expect that directory to exist. Here is what I see on my personal server:

Code:
[root@host ~]# cd /usr/local/cpanel/3rdparty/wp-toolkit/var/logs/
[root@host logs]# ll
total 704
drwx------. 3 wp-toolkit wp-toolkit   4096 Mar  8 18:00 action-logs
-rw-------  1 wp-toolkit wp-toolkit 257283 Mar 12 23:58 main-2022-03-12.log
-rw-------  1 wp-toolkit wp-toolkit 257334 Mar 13 23:58 main-2022-03-13.log
-rw-------  1 wp-toolkit wp-toolkit 173037 Mar 14 13:58 main-2022-03-14.log
-rw-------. 1 root       root         5018 Mar  8 20:43 sw-engine.access.log
-rw-------. 1 root       root          752 Mar  9 14:32 sw-engine.error.log
 

nucleobrasil

Registered
Sep 11, 2022
1
0
1
Porto Alegre Brasil
cPanel Access Level
DataCenter Provider
Friend, I need to make a script like this, but customized.

If I pay, will you build one for me?



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 _@A-Z-a-z-0-9 | head -c${1:-20};echo;)
dbpass=$(< /dev/urandom tr -dc _@A-Z-a-z-0-9 | 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".