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:
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:
(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.
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
(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.