Help with automatic install WordPress on new accounts using BASH

Operating System & Version
Centos 7
cPanel & WHM Version
96.0.8

Alnaggar

Member
May 25, 2021
7
1
3
Riyadh
cPanel Access Level
Root Administrator
Hello, I have been searching for a way to automatically install WordPress on new accounts creation and came across this topics:

How To Automatically Install WordPress On New Accounts.
and chengkinhung reply with his amazing script:

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}"

And as he explained:

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.

My current 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 the change 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 new created domain information.

It seems that WP CLi commands from the script doesn't work ,I have installed WP CLi for all users following this WP CLI: Install and Manage WordPress® on the Command Line and everything seems to work fine. What may I have missed? Could someone please help.
 

Alnaggar

Member
May 25, 2021
7
1
3
Riyadh
cPanel Access Level
Root Administrator
@cPRex Thank you for your reply, I have checked his profile (Last seen: Aug 26, 2020) I don't think he will reply any soon, I posted it here maybe someone with experience can help out. If you don't mind could you please try the script and tell me if it's does the same with you? Maybe I didn't configure things in cPanel right or maybe I didn't install WP CLi the right way? I'm new to cPanel and will appreciate any help.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
15,298
2,438
363
cPanel Access Level
Root Administrator
We aren't able to perform troubleshooting on custom scripts on our end. If you get a specific error I would be able to tell you if that's related to cPanel tools. If you're having issues with the WP CLI tool you could always open a ticket with our team so we can check that directly on the system and confirm that is set up correctly.
 

Alnaggar

Member
May 25, 2021
7
1
3
Riyadh
cPanel Access Level
Root Administrator
@cPRex I opened a support ticket and will wait for their reply, In the mean I have tried to check what might be the issue and I think it's this directory not in my sever (/opt/cpanel/ea-php72/root/usr/bin/php) what is this directory for ? and how can I locate the right one in my server? when I try "cd /opt/cpanel/ea-php72/root/usr/bin/php" it show me error "Not a directory"
 

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.
 
  • Like
Reactions: cPRex