roboWP.sh

From Anna, 1 Year ago, written in Bash, viewed 151 times.
URL https://anna.fyi/view/56d1f0ea Embed
Download Paste or View Raw
  1. #!/bin/bash
  2. #title          :cpwpinstall.sh
  3. #description    :Script to install WordPress on cPanel servers
  4. #author         :Anna Schoolfield and Sean Hicks
  5. #date           :20180617
  6. #version        :0.1    
  7. #usage          :./cpwpinstall.sh
  8. #notes          :Probably shouldn't use if the server is old and/or weird
  9. #============================================================================
  10. # Copyright (c) 2018 Anna Schoolfield, Sean Hicks
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documentation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furnished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in all
  20. # copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. # SOFTWARE.
  29. #============================================================================
  30. echo "!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!"
  31. echo " This script does not have enough logic to be idiot proof."
  32. echo " Pay attention to what you're doing and don't be careless."
  33. echo "        USE ABSOLUTE PATH IF SPECIFYING LOCATION!"
  34. echo "!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!"
  35. echo " "
  36.  
  37. # Prompt for the cPanel username
  38. read -p 'What is the cPanel username to install under? ' CPANEL
  39. PRT=$(whmapi1 accountsummary user=$CPANEL | grep 'partition: ' | awk -F': ' '{print $2}')
  40.  
  41. # Prompt for the ticket number
  42. read -p 'What is the ticket number? (Or some unique value to generate db name & username) ' TICKET
  43.  
  44.  
  45. # Confirm installation directory
  46. echo -n "Hit y to install the WP site in the default public_html; Otherwise hit n to enter a custom dir: "
  47. # While loop and switch case use to structure an options selection loop.
  48. while (( !DIROPTDONE )); do
  49.         read DIROPT
  50.         case "$DIROPT" in
  51.                 y)
  52.                         DESTDIR="/$PRT/$CPANEL/public_html"
  53.                         DIROPTDONE=1
  54.                         ;;
  55.                 n)
  56.                         read -p "Please specify the directory path to install to: " DESTDIR
  57.                         # This little bit strips the ending forward slash if present, and nothing if not
  58.                         DESTDIR=$(echo $DESTDIR | sed '$s%/$%%g')
  59.                         DIROPTDONE=1
  60.                         ;;
  61.                 *)
  62.                         echo "Please choose a valid option (y or n)!"
  63.                         echo -n "Hit y to install the WP site in the default public_html; Otherwise hit n to enter a custom dir: "
  64.                         ;;
  65.         esac
  66. done
  67.  
  68. ############################################
  69. # Gathering variables
  70. PREFIX=$(uapi --user=$CPANEL Mysql get_restrictions | grep prefix | awk '{ print $2 }')
  71. MYSQL=$PREFIX'wp'
  72. MYSQL+=$TICKET
  73. ############################################
  74. # Messy solution to find domain
  75. ############################################
  76. DOMAIN=""
  77. DOMLIST=$(ls -1 /var/cpanel/userdata/$CPANEL/* | grep -v '_SSL$\|.yaml$\|.json$\|cache$\|main$')
  78. CNT=4
  79. SUBDIR=$(echo "$DESTDIR" | sed 's%^/'"$PRT"'/'"$CPANEL"'/%%')
  80. BUFFER=/$PRT/$CPANEL/
  81. while (( !domfind_done )); do
  82.         BUFFER="${BUFFER}$(echo "$DESTDIR" | cut -d'/' -f$CNT)"
  83.         SUBDIR=$(echo "$SUBDIR" | sed -E 's%^[^/]+/%%')
  84.         DOMLIST=$(echo "$DOMLIST" | xargs grep -l "$BUFFER")
  85.         BUFFER="${BUFFER}/"
  86.         DOMCNT=$(echo "$DOMLIST" | wc -l)
  87.         if [[ "$DOMCNT" -eq 1 ]]; then
  88.                 DOMAIN=$(echo "$DOMLIST" | rev | cut -d'/' -f1 | rev)
  89.                 domfind_done=1
  90.         fi
  91.         if [[ "$DOMCNT" -eq 0 ]]; then
  92.                 echo "Domain not found! Continuing without..."
  93.                 DOMAIN="!!!ERROR!!!"
  94.                 domfind_done=1
  95.         fi
  96.         ((CNT++))
  97. done
  98. ############################################
  99. echo "****************************"
  100. echo "****Confirm installation****"
  101. echo "****************************"
  102. echo "Target cPanel Account:" $CPANEL
  103. echo "Target Domain: "$DOMAIN / $SUBDIR
  104. echo "MySQL database and username:" $MYSQL
  105. echo "Destination directory:" $DESTDIR
  106. echo "Current contents of the destination directory (empty if nothing listed):"
  107. ls -1 $DESTDIR
  108. echo " "
  109. read -p "Would you like to proceed with the WP installation? [y/n] " -n 1 -r
  110. if [[ ! $REPLY =~ ^[Yy]$ ]]
  111. then
  112.     exit 1
  113. fi
  114.  
  115. echo " "
  116. # Generate a random pw for the MySQL db user
  117. MYSQLUSERPASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)
  118.  
  119. # Create MySQL db & user
  120. echo "Creating MySQL user......................"
  121. uapi --user=$CPANEL Mysql create_user name=$MYSQL password=$MYSQLUSERPASS
  122. if [ "$?" -ne 0 ]; then
  123.         echo "Error creating MySQL user!"
  124.         exit 1
  125. fi
  126.  
  127. echo "Creating MySQL database......................"
  128. uapi --user=$CPANEL Mysql create_database name=$MYSQL
  129. if [ "$?" -ne 0 ]; then
  130.         echo "Error creating MySQL database!"
  131.         exit 1
  132. fi
  133.  
  134. echo "Setting privileges on database......................"
  135. uapi --user=$CPANEL Mysql set_privileges_on_database user=$MYSQL database=$MYSQL privileges=ALL
  136. if [ "$?" -ne 0 ]; then
  137.         echo "Error setting MySQL privileges!"
  138.         exit 1
  139. fi
  140.  
  141. # Download and extract the WP tarball, clean it up afterwards
  142. wget -qO-  https://wordpress.org/latest.tar.gz | tar --strip-components=1 -xz -C $DESTDIR
  143. rm -f $DESTDIR/latest.tar.gz
  144.  
  145. # Set up the initial wp-config file
  146. mv $DESTDIR/wp-config-sample.php $DESTDIR/wp-config.php
  147.  
  148. # Set up the default WP .htaccess file
  149.  
  150. cat >> $DESTDIR/.htaccess << "EOF"
  151. # BEGIN WordPress
  152. <IfModule mod_rewrite.c>
  153. RewriteEngine On
  154. RewriteBase /
  155. RewriteRule ^index\.php$ - [L]
  156. RewriteCond %{REQUEST_FILENAME} !-f
  157. RewriteCond %{REQUEST_FILENAME} !-d
  158. RewriteRule . /index.php [L]
  159. </IfModule>
  160. # END WordPress
  161. EOF
  162.  
  163. # Make sure everything is chowned to the cPanel user
  164. chown -R $CPANEL:$CPANEL $DESTDIR/* $DESTDIR/.*
  165.  
  166. # Update Salts in the wp-config file
  167. SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
  168. STRING='put your unique phrase here'
  169. printf '%s\n' "g/$STRING/d" a "$SALT" . w | ed -s $DESTDIR/wp-config.php
  170.  
  171. # Update wp-config file with MySQL info
  172. echo "*************************************************************"
  173. sed -i 's/database_name_here/'"$MYSQL"'/' $DESTDIR/wp-config.php
  174. sed -i 's/username_here/'"$MYSQL"'/' $DESTDIR/wp-config.php
  175. sed -i 's/password_here/'"$MYSQLUSERPASS"'/' $DESTDIR/wp-config.php
  176. echo "Here's the MySQL info in the wp-config.php file"
  177. grep -E 'DB_NAME|DB_USER|DB_PASSWORD' $DESTDIR/wp-config.php
  178.  
  179. echo "******************************************************************************************"
  180. echo "WP installed, visit $DOMAIN / $SUBDIR to complete the installation"
  181. echo "******************************************************************************************"
  182.  

Reply to "roboWP.sh"

Here you can reply to the paste above