Deploy WordPress Automatically With Cpanel

How To Automatically Deploy WordPress With Cpanel

Ever wanted to deploy WordPress automatically when you create a new cpanel account in WordPress?

Maybe it is for a clients and you want to have a boiler plate for that installs and cleans up each account when created.

#!/bin/bash

tmpfile="$(mktemp -p /tmp wp-auto-install-XXXXXXXX)"
cat "${1:-/dev/stdin}" > $tmpfile
cpanelusername=$(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']")
cpanelpass=$(python -c "import sys, json; print json.load(open('$tmpfile'))['data']['pass']")
rm -f $tmpfile

# WP Creds
wppass=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-20};echo;)
wpuser=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-8};echo;)

# URL Encoding
wppass=$(php -r "echo urlencode('${wppass}');")
wpuser=$(php -r "echo urlencode('${wpuser}');")
wpemail=$(php -r "echo urlencode('root@localhost.tld');")
blogname=$(php -r "echo urlencode('Defalt Blog Name');")
blogdesc=$(php -r "echo urlencode('Defalt Description');")
curl -s -O /dev/null --insecure -u "$cpanelusername:$cpanelpass" "https://localhost:2083/frontend/paper_lantern/addoncgi/cpaddons.html.tt?addon=cPanel%3A%3ABlogs%3A%3AWordPressX&action=install&debug=0&verbose=0&oneclick=0&subdomain=${domain}&installdir=/&auser=${wpuser}&apass=${wppass}&apass2=${wppass}&email=${wpemail}&1blog_name=${blogname}&2blog_description=${blogdesc}&table_prefix=wp&existing_mysql="

# SSL certificate
le-cp autossl run-for-user ${cpanelusername}

# Specify path
wppath="/home/${cpanelusername}/public_html"
wpcli="sudo -u ${cpanelusername} -i /opt/cpanel/ea-php73/root/usr/bin/php /usr/local/cpanel/3rdparty/share/cpanel-wp-cli/wp-cli.phar"

# Delete default plugins
${wpcli} --path=${wppath} plugin delete akismet
${wpcli} --path=${wppath} plugin delete hello

# Install Plugins
wppluginlist=(
erp
erp-pdf-invoice
wedevs-project-manager
ajax-load-more
advanced-custom-fields-font-awesome
breadcrumb-navxt
contact-form-7
one-click-demo-import
)

for wpplugin in ${wppluginlist[@]}
do
    ${wpcli} --path=${wppath} plugin install ${wpplugin} --activate
done

TOKEN="api-token"
OWNER="gitaccount"
REPO="repo"
PATH="file.zip"
FILE="https://api.github.com/repos/$OWNER/$REPO/contents/$PATH"

curl --header 'Authorization: token $TOKEN' 
      --header 'Accept: application/vnd.github.v3.raw' 
      --remote-name 
      --location $FILE

# Install Theme
${wpcli} --path=${wppath} theme install theme
${wpcli} --path=${wppath} theme install theme-child --activate

# Delete inactive WordPress themes
#
# wpflags="--path=${wppath} --skip-plugins --skip-themes --allow-root"
#
# themeexceptions=(
# theme
# theme-child
# )
#
# themelist=($(sudo -u ${cpanelusername} -i /opt/cpanel/ea-php73/root/usr/bin/php /usr/local/cpanel/3rdparty/share/cpanel-wp-cli/wp-cli.phar theme list --fields=name --status=inactive ${wpflags}))
#
# for theme in ${themelist[@]}
# do
#   if [[ ! " ${themeexceptions[@]} " =~ " ${theme} " ]]; then
#      ${wpcli} theme delete ${theme} ${wpflags}
#   fi
#   done

# Settings
${wpcli} rewrite structure '/%postname%/' --hard
${wpcli} rewrite flush --hard

Was this helpful?

Leave a Comment