WP Install Script Saturday Night

From Anna, 1 Year ago, written in Bash, viewed 161 times.
URL https://anna.fyi/view/6ee862cc Embed
Download Paste or View Raw
  1. #!/bin/bash
  2.  
  3. # Interactive WordPress Installation Script for cPanel servers
  4. # The ticket number *should* be a unique value for the MySQL db & username, but you can enter anything unique
  5.  
  6. echo "!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!"
  7. echo " This script does not have enough logic to be idiot proof."
  8. echo " Pay attention to what you're doing and don't be careless."
  9. echo "!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!"
  10. echo " "
  11.  
  12. # Prompt for the cPanel username
  13. read -p 'What is the cPanel username for the installation? ' CPANEL
  14. PRT=$(whmapi1 accountsummary user=$CPANEL | grep 'partition: ' | awk -F': ' '{print $2}')
  15.  
  16. # Prompt for the ticket number
  17. read -p 'What is the ticket number (or something unique for the MySQL db & username)? ' TICKET
  18.  
  19.  
  20. # Confirm installation directory
  21. echo -n "Hit y to install the WP site in the default public_html; Otherwise hit n to enter a custom dir: "
  22. # Select and switch case use to structure an options selection loop.
  23. while (( !DIROPTDONE )); do
  24.         read DIROPT
  25.         case "$DIROPT" in
  26.                 y)
  27.                         DESTDIR="/$PRT/$CPANEL/public_html"
  28.                         DIROPTDONE=1
  29.                         ;;
  30.                 n)
  31.                         read -p "Please specify the directory path to install to: " DESTDIR
  32.                         # This little bit strips the ending forward slash if present, and nothing if not
  33.                         DESTDIR=$(echo $DESTDIR | sed '$s%/$%%g')
  34.                         DIROPTDONE=1
  35.                         ;;
  36.                 *)
  37.                         echo "Please choose a valid option (y or n)!"
  38.                         echo -n "Hit y to install the WP site in the default public_html; Otherwise hit n to enter a custom dir: "
  39.                         ;;
  40.         esac
  41. done
  42.  
  43. ############################################
  44.  
  45. # Gathering variables
  46. PREFIX=$(uapi --user=$CPANEL Mysql get_restrictions | grep prefix | awk '{ print $2 }')
  47. MYSQL=$PREFIX'wp'
  48. MYSQL+=$TICKET
  49. DOMAIN=$(grep -B2 $DESTDIR /usr/local/apache/conf/httpd.conf | grep ServerName | head -1 | awk -F' ' '{print $2}')
  50.  
  51. ############################################
  52. echo "****************************"
  53. echo "****Confirm installation****"
  54. echo "Target cPanel Account:" $CPANEL
  55. echo "Target Domain: "$DOMAIN
  56. echo "The MySQL database and username:" $MYSQL
  57. echo "Destination directory:" $DESTDIR
  58. echo "Current contents of the destination directory (empty if nothing listed):"
  59. ls -1 $DESTDIR
  60. read -p "Would you like to proceed with the WP installation? [y/n] " -n 1 -r
  61. if [[ ! $REPLY =~ ^[Yy]$ ]]
  62. then
  63.     exit 1
  64. fi
  65.  
  66. echo " "
  67. # Generate a random pw for the MySQL db user
  68. MYSQLUSERPASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)
  69.  
  70. # Create MySQL db & user
  71. echo "Creating MySQL user......................"
  72. uapi --user=$CPANEL Mysql create_user name=$MYSQL password=$MYSQLUSERPASS
  73. if [ "$?" -ne 0 ]; then
  74.         echo "Error creating MySQL user!"
  75.         exit 1
  76. fi
  77.  
  78. echo "Creating MySQL database......................"
  79. uapi --user=$CPANEL Mysql create_database name=$MYSQL
  80. if [ "$?" -ne 0 ]; then
  81.         echo "Error creating MySQL database!"
  82.         exit 1
  83. fi
  84.  
  85. echo "Setting privileges on database......................"
  86. uapi --user=$CPANEL Mysql set_privileges_on_database user=$MYSQL database=$MYSQL privileges=ALL
  87. if [ "$?" -ne 0 ]; then
  88.         echo "Error setting MySQL privileges!"
  89.         exit 1
  90. fi
  91.  
  92. # Download and extract the WP tarball, clean it up afterwards
  93. wget -qO-  https://wordpress.org/latest.tar.gz | tar --strip-components=1 -xz -C $DESTDIR
  94. rm -f $DESTDIR/latest.tar.gz
  95.  
  96. # Set up the initial wp-config file
  97. mv $DESTDIR/wp-config-sample.php $DESTDIR/wp-config.php
  98.  
  99. # Set up the default WP .htaccess file
  100.  
  101. cat >> $DESTDIR/.htaccess << "EOF"
  102. # BEGIN WordPress
  103. <IfModule mod_rewrite.c>
  104. RewriteEngine On
  105. RewriteBase /
  106. RewriteRule ^index\.php$ - [L]
  107. RewriteCond %{REQUEST_FILENAME} !-f
  108. RewriteCond %{REQUEST_FILENAME} !-d
  109. RewriteRule . /index.php [L]
  110. </IfModule>
  111. # END WordPress
  112. EOF
  113.  
  114. # Make sure everything is chowned to the cPanel user
  115. chown -R $CPANEL:$CPANEL $DESTDIR/* $DESTDIR/.*
  116.  
  117. # Update Salts in the wp-config file
  118. SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
  119. STRING='put your unique phrase here'
  120. printf '%s\n' "g/$STRING/d" a "$SALT" . w | ed -s $DESTDIR/wp-config.php
  121.  
  122. # Update wp-config file with MySQL info
  123. echo "*********************************"
  124. sed -i 's/database_name_here/'"$MYSQL"'/' $DESTDIR/wp-config.php
  125. sed -i 's/username_here/'"$MYSQL"'/' $DESTDIR/wp-config.php
  126. sed -i 's/password_here/'"$MYSQLUSERPASS"'/' $DESTDIR/wp-config.php
  127. echo "Here's the MySQL info in the wp-config.php file"
  128. grep -E 'DB_NAME|DB_USER|DB_PASSWORD' $DESTDIR/wp-config.php
  129.  
  130. echo "*************************************************************"
  131. echo "WP installed, visit $DOMAIN to complete the installation"
  132. echo "*************************************************************"
  133.  

Reply to "WP Install Script Saturday Night"

Here you can reply to the paste above