roboWP.sh

From Anna & Sean, 5 Years ago, written in Plain Text, viewed 829 times.
URL https://anna.fyi/view/fc147070 Embed
Download Paste or View Raw
  1. #!/bin/bash
  2.  
  3. function wp-install() {
  4. #title          :cpwpinstall.sh
  5. #description    :Script to install WordPress on cPanel servers
  6. #author         :Anna Schoolfield and Sean Hicks
  7. #date           :20180709
  8. #version        :0.1rcs2
  9. #notes          :Probably shouldn't use if the server is old and/or weird
  10. #============================================================================
  11. # Copyright (c) 2018 Anna Schoolfield, Sean Hicks
  12. #
  13. # Permission is hereby granted, free of charge, to any person obtaining a copy
  14. # of this software and associated documentation files (the "Software"), to deal
  15. # in the Software without restriction, including without limitation the rights
  16. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. # copies of the Software, and to permit persons to whom the Software is
  18. # furnished to do so, subject to the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included in all
  21. # copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. # SOFTWARE.
  30. #============================================================================
  31. # Return codes:
  32. #       0: Fine
  33. #       1: MySQL creation issues
  34. #       2: User stop
  35. #============================================================================
  36.         echo "!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!"
  37.         echo " This script does not have enough logic to be idiot proof."
  38.         echo " Pay attention to what you're doing and don't be careless."
  39.         echo "         USE ABSOLUTE PATH IF SPECIFYING LOCATION!        "
  40.         echo "!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!"
  41.         echo
  42.  
  43.         # Ask for user and find home directory location.
  44.         read -p "Designate the username to install under: " installUser
  45.         installPart="$(whmapi1 accountsummary user=$installUser | grep 'partition: ' | awk -F': ' '{print $2}')"
  46.  
  47.         # Ask for salt.
  48.         read -p "Please provide the ticket number, or a unique value to distinguish generated information: " installSalt
  49.  
  50.         # Query setup to determine how to proceed.
  51.         echo -n "Enter \"y\" to install the WP site in the default document root; Enter \"n\" to provide a specific path: "
  52.  
  53.         while (( !dirOptDone )); do
  54.                 read dirOpt
  55.                 case "$dirOpt" in
  56.                         y)
  57.                                 destDir="/$installPart/$installUser/public_html"
  58.                                 dirOptDone=1
  59.                                 ;;
  60.                         n)
  61.                                 read -p "Please specify the directory path to install to: " destDir
  62.                                 # This little bit strips the ending forward slash if present, and nothing if not.
  63.                                 destDir="$(echo $destDir | sed '$s%/$%%g')"
  64.                                 dirOptDone=1
  65.                                 ;;
  66.                         *)
  67.                                 echo "Please choose a valid option (y or n)!"
  68.                                 echo -n "Enter \"y\" to install the WP site in the default document root; Enter \"n\" to provide a specific path: "
  69.                                 ;;
  70.                 esac
  71.         done
  72.         unset dirOptDone
  73.         unset dirOpt
  74.  
  75.         # MySQL variable setup.
  76.         dbPrefix="$(uapi --user=$installUser Mysql get_restrictions | grep prefix | awk '{ print $2 }')"
  77.         mysqlName="$dbPrefix"'wp'"$installSalt"
  78.         unset installSalt
  79.         unset dbPrefix
  80.  
  81.         domName=""
  82.         domList="$(ls -1 /var/cpanel/userdata/$installUser/* | grep -v '_SSL$\|.yaml$\|.json$\|cache$\|main$')"
  83.         cnt=4
  84.         subdir="$(echo "$destDir" | sed 's%^/'"$installPart"'/'"$installUser"'/%%')"
  85.         tmpBuffer="/$installPart/$installUser/"
  86.  
  87.         # Domain ID subfunction.
  88.         while (( !domfind_done )); do
  89.                 tmpBuffer="${tmpBuffer}$(echo "$destDir" | cut -d'/' -f$cnt)"
  90.                 subdir=$(echo "$subdir" | sed -E 's%^[^/]+/%%')
  91.                 domList=$(echo "$domList" | xargs grep -l "$tmpBuffer")
  92.                 tmpBuffer="${tmpBuffer}/"
  93.                 domCnt=$(echo "$domList" | wc -l)
  94.                 if [[ "$domCnt" -eq 1 ]]; then
  95.                         domName=$(echo "$domList" | rev | cut -d'/' -f1 | rev)
  96.                         domfind_done=1
  97.                 fi
  98.                 if [[ "$domCnt" -eq 0 ]]; then
  99.                         echo "Domain not found! Continuing without..."
  100.                         domName="!!!ERROR!!!"
  101.                         domfind_done=1
  102.                 fi
  103.                 ((cnt++))
  104.         done
  105.         unset cnt
  106.         unset tmpBuffer
  107.         unset domList
  108.         unset domCnt
  109.         unset domfind_done
  110.  
  111.         echo "****************************"
  112.         echo "****Confirm installation****"
  113.         echo "****************************"
  114.         echo 'Target cPanel Account: '"$installUser"
  115.         echo 'Target Domain: '"$domName"'/'"$subdir"
  116.         echo 'MySQL database and username: '"$mysqlName"
  117.         echo 'Destination directory: '"$destDir"
  118.         echo 'Current contents of the destination directory (empty if nothing listed):'
  119.         ls -1 "$destDir" 2>/dev/null
  120.         if [[ "$?" == 2 ]]; then
  121.                 read -p "Directory not found! Create? [y/n]" -n1 -r dirChk
  122.                 if [[ "$dirChk" =~ ^[yY]$ ]]; then
  123.                         mkdir -p $destDir
  124.                 else
  125.                         echo 'Exiting...'
  126.                         return 2
  127.                 fi
  128.         fi
  129.         echo
  130.         unset dirChk
  131.  
  132.         read -p "Would you like to proceed with the installation of Wordpress? [y/n]" -n 1 -r contChk
  133.         if [[ ! "$contChk" =~ ^[yY]$ ]]; then
  134.                 echo 'Exiting...'
  135.                 return 2
  136.         fi
  137.         echo
  138.         unset contChk
  139.  
  140.         mysqlPass="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)"
  141.  
  142.         echo "Creating MySQL user..."
  143.         mysqlGenUsr="$(uapi --user=$installUser Mysql create_user name=$mysqlName password=$mysqlPass | grep -Eo "status:\s\S+" | awk '{print $2}')"
  144.         if [ "$mysqlGenUsr" -ne 1 ]; then
  145.                 echo "Error creating MySQL user!"
  146.                 return 1
  147.         fi
  148.  
  149.         echo "Creating MySQL database..."
  150.         mysqlGenDB="$(uapi --user=$installUser Mysql create_database name=$mysqlName | grep -Eo "status:\s\S+" | awk '{print $2}')"
  151.         if [ "$mysqlGenDB" -ne 1 ]; then
  152.                 echo "Error creating MySQL database!"
  153.                 return 1
  154.         fi
  155.  
  156.         echo "Setting privileges on database..."
  157.         mysqlSetPrivs="$(uapi --user=$installUser Mysql set_privileges_on_database user=$mysqlName database=$mysqlName privileges=ALL | grep -Eo "status:\s\S+" | awk '{print $2}')"
  158.         if [ "$mysqlSetPrivs" -ne 1 ]; then
  159.                 echo "Error setting MySQL privileges!"
  160.                 return 1
  161.         fi
  162.  
  163.         wget -qO- https://wordpress.org/latest.tar.gz 2>/dev/null | tar --strip-components=1 -xz -C $destDir
  164.         rm -f $destDir/latest.tar.gz
  165.         cp -p $destDir/wp-config{-sample,}.php
  166.  
  167.         cat >> $destDir/.htaccess << "EOF"
  168. # BEGIN WordPress
  169. <IfModule mod_rewrite.c>
  170. RewriteEngine On
  171. RewriteBase /
  172. RewriteRule ^index\.php$ - [L]
  173. RewriteCond %{REQUEST_FILENAME} !-f
  174. RewriteCond %{REQUEST_FILENAME} !-d
  175. RewriteRule . /index.php [L]
  176. </IfModule>
  177. # END WordPress
  178. EOF
  179.  
  180.         chown -R $installUser:$installUser $destDir/{.*,}
  181.         configSalt="$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)"
  182.         repString="put your unique phrase here"
  183.         printf '%s\n' "g/$repString/d" a "$configSalt" . w | ed -s $destDir/wp-config.php
  184.         unset configSalt
  185.         unset repString
  186.         unset installUser
  187.  
  188.         echo "*************************************************************"
  189.         sed -i 's/database_name_here/'"$mysqlName"'/' $destDir/wp-config.php
  190.         sed -i 's/username_here/'"$mysqlName"'/' $destDir/wp-config.php
  191.         sed -i 's/password_here/'"$mysqlPass"'/' $destDir/wp-config.php
  192.  
  193.         echo "Here's the MySQL configuration details in the configuration file:"
  194.         grep -E 'DB_(NAME|USER|PASSWORD)' $destDir/wp-config.php
  195.  
  196.         echo "******************************************************************************************"
  197.         echo "WP installed, visit $domName/$subdir to complete the installation"
  198.         echo "******************************************************************************************"
  199.         unset mysqlName
  200.         unset mysqlPass
  201.         unset domName
  202.         unset subdir
  203.         unset destDir
  204. }
  205.  
  206. wp-install

Reply to "roboWP.sh"

Here you can reply to the paste above