#!/bin/bash function wp-install() { #title :cpwpinstall.sh #description :Script to install WordPress on cPanel servers #author :Anna Schoolfield and Sean Hicks #date :20180709 #version :0.1rcs2 #notes :Probably shouldn't use if the server is old and/or weird #============================================================================ # Copyright (c) 2018 Anna Schoolfield, Sean Hicks # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. #============================================================================ # Return codes: # 0: Fine # 1: MySQL creation issues # 2: User stop #============================================================================ echo "!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!" echo " This script does not have enough logic to be idiot proof." echo " Pay attention to what you're doing and don't be careless." echo " USE ABSOLUTE PATH IF SPECIFYING LOCATION! " echo "!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!" echo # Ask for user and find home directory location. read -p "Designate the username to install under: " installUser installPart="$(whmapi1 accountsummary user=$installUser | grep 'partition: ' | awk -F': ' '{print $2}')" # Ask for salt. read -p "Please provide the ticket number, or a unique value to distinguish generated information: " installSalt # Query setup to determine how to proceed. echo -n "Enter \"y\" to install the WP site in the default document root; Enter \"n\" to provide a specific path: " while (( !dirOptDone )); do read dirOpt case "$dirOpt" in y) destDir="/$installPart/$installUser/public_html" dirOptDone=1 ;; n) read -p "Please specify the directory path to install to: " destDir # This little bit strips the ending forward slash if present, and nothing if not. destDir="$(echo $destDir | sed '$s%/$%%g')" dirOptDone=1 ;; *) echo "Please choose a valid option (y or n)!" echo -n "Enter \"y\" to install the WP site in the default document root; Enter \"n\" to provide a specific path: " ;; esac done unset dirOptDone unset dirOpt # MySQL variable setup. dbPrefix="$(uapi --user=$installUser Mysql get_restrictions | grep prefix | awk '{ print $2 }')" mysqlName="$dbPrefix"'wp'"$installSalt" unset installSalt unset dbPrefix domName="" domList="$(ls -1 /var/cpanel/userdata/$installUser/* | grep -v '_SSL$\|.yaml$\|.json$\|cache$\|main$')" cnt=4 subdir="$(echo "$destDir" | sed 's%^/'"$installPart"'/'"$installUser"'/%%')" tmpBuffer="/$installPart/$installUser/" # Domain ID subfunction. while (( !domfind_done )); do tmpBuffer="${tmpBuffer}$(echo "$destDir" | cut -d'/' -f$cnt)" subdir=$(echo "$subdir" | sed -E 's%^[^/]+/%%') domList=$(echo "$domList" | xargs grep -l "$tmpBuffer") tmpBuffer="${tmpBuffer}/" domCnt=$(echo "$domList" | wc -l) if [[ "$domCnt" -eq 1 ]]; then domName=$(echo "$domList" | rev | cut -d'/' -f1 | rev) domfind_done=1 fi if [[ "$domCnt" -eq 0 ]]; then echo "Domain not found! Continuing without..." domName="!!!ERROR!!!" domfind_done=1 fi ((cnt++)) done unset cnt unset tmpBuffer unset domList unset domCnt unset domfind_done echo "****************************" echo "****Confirm installation****" echo "****************************" echo 'Target cPanel Account: '"$installUser" echo 'Target Domain: '"$domName"'/'"$subdir" echo 'MySQL database and username: '"$mysqlName" echo 'Destination directory: '"$destDir" echo 'Current contents of the destination directory (empty if nothing listed):' ls -1 "$destDir" 2>/dev/null if [[ "$?" == 2 ]]; then read -p "Directory not found! Create? [y/n]" -n1 -r dirChk if [[ "$dirChk" =~ ^[yY]$ ]]; then mkdir -p $destDir else echo 'Exiting...' return 2 fi fi echo unset dirChk read -p "Would you like to proceed with the installation of Wordpress? [y/n]" -n 1 -r contChk if [[ ! "$contChk" =~ ^[yY]$ ]]; then echo 'Exiting...' return 2 fi echo unset contChk mysqlPass="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)" echo "Creating MySQL user..." mysqlGenUsr="$(uapi --user=$installUser Mysql create_user name=$mysqlName password=$mysqlPass | grep -Eo "status:\s\S+" | awk '{print $2}')" if [ "$mysqlGenUsr" -ne 1 ]; then echo "Error creating MySQL user!" return 1 fi echo "Creating MySQL database..." mysqlGenDB="$(uapi --user=$installUser Mysql create_database name=$mysqlName | grep -Eo "status:\s\S+" | awk '{print $2}')" if [ "$mysqlGenDB" -ne 1 ]; then echo "Error creating MySQL database!" return 1 fi echo "Setting privileges on database..." mysqlSetPrivs="$(uapi --user=$installUser Mysql set_privileges_on_database user=$mysqlName database=$mysqlName privileges=ALL | grep -Eo "status:\s\S+" | awk '{print $2}')" if [ "$mysqlSetPrivs" -ne 1 ]; then echo "Error setting MySQL privileges!" return 1 fi wget -qO- https://wordpress.org/latest.tar.gz 2>/dev/null | tar --strip-components=1 -xz -C $destDir rm -f $destDir/latest.tar.gz cp -p $destDir/wp-config{-sample,}.php cat >> $destDir/.htaccess << "EOF" # BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress EOF chown -R $installUser:$installUser $destDir/{.*,} configSalt="$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)" repString="put your unique phrase here" printf '%s\n' "g/$repString/d" a "$configSalt" . w | ed -s $destDir/wp-config.php unset configSalt unset repString unset installUser echo "*************************************************************" sed -i 's/database_name_here/'"$mysqlName"'/' $destDir/wp-config.php sed -i 's/username_here/'"$mysqlName"'/' $destDir/wp-config.php sed -i 's/password_here/'"$mysqlPass"'/' $destDir/wp-config.php echo "Here's the MySQL configuration details in the configuration file:" grep -E 'DB_(NAME|USER|PASSWORD)' $destDir/wp-config.php echo "******************************************************************************************" echo "WP installed, visit $domName/$subdir to complete the installation" echo "******************************************************************************************" unset mysqlName unset mysqlPass unset domName unset subdir unset destDir } wp-install