Guest User

Untitled

a guest
Mar 7th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #!/bin/bash
  2. domain="$1" && test -z "$domain" && exit 2
  3.  
  4. environment() {
  5. read -sp "Please enter the app DB root password: " dbrootp_1 && echo
  6. read -sp "Please enter the app DB root password again:" dbrootp_2 && echo
  7. if [ "$dbrootp_1" != "$dbrootp_2" ]; then echo "Values unmatched. Please try again." && exit 2 fi
  8.  
  9. read -sp "Please enter the app DB user password: " dbuserp_1 && echo
  10. read -sp "Please enter the app DB user password again:" dbuserp_2 && echo
  11. if [ "$dbuserp_1" != "$dbuserp_2" ]; then echo "Values unmatched. Please try again." && exit 2 fi
  12. }
  13. environment
  14.  
  15. wordpress() {
  16. rm -rf "$drt"/"$domain"/ 2>/dev/null
  17. wp core download --path="$drt"/"$domain"/ --allow-root
  18. wp config create --path="$drt"/"$domain"/ --dbname="$domain" --dbuser="$domain" --dbpass="$dbuserp" --dbhost="localhost" --allow-root
  19. }
  20. wordpress
  21.  
  22. nginx() {
  23. rm "$s_a/$domain.conf" 2>/dev/null
  24. rm "$s_e/$domain.conf" 2>/dev/null
  25.  
  26. cat <<-EOF > "$s_a/$domain.conf"
  27. server {
  28. root ${drt}/${domain}/;
  29. server_name ${domain} www.${domain};
  30. location ~* .(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {expires 365d;}
  31. location "/wp-login.php" {access_log "/var/log/httpd/wordpress-logins.log";}
  32. }
  33. EOF
  34.  
  35. ln -sf "$s_a"/"$domain".conf "$s_e"
  36. rse
  37. }
  38. nginx
  39.  
  40. certbot() {
  41. certbot --nginx -d "$domain" -d www."$domain"
  42. rse
  43. }
  44. certbot
  45.  
  46. database() {
  47. cat <<-EOF | mysql -u root -p"$dbrootp_1"
  48. DROP USER IF EXISTS "$domain"@"localhost";
  49. DROP database IF EXISTS "$domain";
  50. CREATE USER "$domain"@"localhost" IDENTIFIED BY "$dbuserp_1";
  51. CREATE DATABASE "$domain";
  52. GRANT ALL PRIVILEGES ON "$domain".* TO "$domain"@"localhost";
  53. EOF
  54. }
  55. database
  56.  
  57. finalize() {
  58. echo "Change http to http2 in your Nginx app conf and run rse"
  59. }
  60. finalize
  61.  
  62. drt="/var/www/html"
  63. s_a="etc/nginx/sites-available"
  64. s_e="etc/nginx/sites-enabled"
  65.  
  66. wordpress() {
  67. local drt="$1"
  68. local domain="$2"
  69. local dbpass="$3"
  70.  
  71. # should do more checking here, this is the bare minimum.
  72. # it avoids running `rm -rf /` if both $drt and $domain
  73. # are empty, and running `rm -rf "$drt/"` if $domain is empty.
  74. [ -z "$drt" ] && error 1 '$drt is empty. aborting!'
  75. [ -z "$domain" ] && error 1 '$domain is empty. aborting!'
  76. [ -z "$dbpass" ] && error 1 '$dbpass is empty. aborting!'
  77.  
  78. # should check exit status of each command run here and take
  79. # appropriate action on any failures. Figure out what you want
  80. # to happen if any of the following fail and implement it.
  81. rm -rf "$drt"/"$domain"/ 2>/dev/null
  82. wp core download --path="$drt"/"$domain"/ --allow-root
  83. wp config create --path="$drt"/"$domain"/ --dbname="$domain"
  84. --dbuser="$domain" --dbpass="$dbpass" --dbhost=localhost
  85. --allow-root
  86. }
  87.  
  88. error() {
  89. local ec msg
  90.  
  91. ec="$1" ; shift
  92. msg="$*"
  93.  
  94. [ -n "$msg" ] && echo "$msg" >&2
  95. # don't exit if $ec==0 - just return after printing a warning to stderr
  96. [ "$ec" != 0 ] && exit "$ec"
  97. }
  98.  
  99. function1 () { ... ; }
  100. function2 () { ... ; }
  101. function3 () { ... ; }
  102. function4 () { ... ; }
  103.  
  104. main () {
  105. function1
  106. function2
  107. function3
  108. function4
  109. }
  110.  
  111. main
  112.  
  113. case "$var" in
  114. 1) function1 ;;
  115. 2) function2 ;;
  116. 3) function3 ;;
  117. 4) function4 ;;
  118. *) error 1 'Unknown case' ;;
  119. esac
  120.  
  121. if [ "$dbrootp_1" != "$dbrootp_2" ]; then echo "Values unmatched. Please try again." && exit 2 fi
  122.  
  123. read -sp "Please enter the app DB root password: " dbrootp_1 && echo
  124.  
  125. readpw() {
  126. local pw1 pw2 label=$1
  127. while :; do
  128. read -sp "Please enter the $label password: " pw1 && echo
  129. read -sp "Please enter the $label password again: " pw2 && echo
  130. [ "$pw1" = "$pw2" ] && break
  131. echo "The values don't match. Please try again."
  132. done
  133. REPLY=$pw1
  134. }
  135.  
  136. readpw "app DB user"
  137. dbuserpw=$REPLY
  138.  
  139. readpw "app DB root"
  140. dbrootpw=$REPLY
Add Comment
Please, Sign In to add comment