Nextcloud Automation - for when you don't have time nor wish to spend too much time deploying stuff. https://nextcloud.com
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

212 行
7.4 KiB

  1. #!/usr/bin/env bash
  2. ###############################################################################
  3. # #
  4. # Nextcloud automatic installation script #
  5. # #
  6. # Designed for stressed VUAs with little to no wish to copypaste a gadzillion #
  7. # UNIX commands. #
  8. # #
  9. # Copyright (c) 2020 - Bryan Pedini #
  10. # #
  11. ###############################################################################
  12. _parse_params() {
  13. VERBOSE=true
  14. VERSION="18.0.4"
  15. INSTALL_DIR="/var/www/nextcloud"
  16. NO_CONFIGURE_MARIADB=false
  17. DATABASE_NAME="nextcloud"
  18. DATABASE_USER="nextcloud_admin"
  19. for par in "$@"; do
  20. case "$par" in
  21. "-h" | "--help" | "--usage")
  22. _print_usage
  23. ;;
  24. "-q" | "--quiet")
  25. VERBOSE=false
  26. shift
  27. ;;
  28. "--version")
  29. [[ -z "$2" ]] && _print_usage "Version not specified" 1
  30. [[ "$2:0:1" = "-" ]] && _print_usage
  31. VERSION="$2"
  32. shift
  33. shift
  34. ;;
  35. "--install-dir")
  36. [[ -z "$2" ]] && _print_usage "Installation directory not \
  37. specified" 1
  38. [[ "$2:0:1" = "-" ]] && _print_usage
  39. INSTALL_DIR="$2"
  40. shift
  41. shift
  42. ;;
  43. "--no-configure-mariadb")
  44. NO_CONFIGURE_MARIADB=true
  45. shift
  46. ;;
  47. "--database-name")
  48. [[ -z "$2" ]] && _print_usage "Database name not speficied" 1
  49. [[ "$2:0:1" = "-" ]] && _print_usage
  50. DATABASE_NAME="$2"
  51. shift
  52. shift
  53. ;;
  54. "--database-user")
  55. [[ -z "$2" ]] && _print_usage "Database admin user not \
  56. specified" 1
  57. [[ "$2:0:1" = "-" ]] && _print_usage
  58. DATABASE_USER="$2"
  59. shift
  60. shift
  61. ;;
  62. *)
  63. _print_usage "Argument not recognized: $1" 1
  64. esac
  65. done
  66. }
  67. # Usage explaination printed to console
  68. _print_usage() {
  69. echo "Usage: $0 [OPTIONS]"
  70. echo
  71. echo "OPTIONS:"
  72. echo " -h, --help, --usage Prints this help message and exits"
  73. echo " -q, --quiet Turns off verbose logging (default: False)"
  74. echo " --version Install a custom version of Nextcloud
  75. (default: $VERSION)"
  76. echo " --install-dir Specifies a custom path for installation
  77. (default: \"/var/www/nextcloud\")"
  78. echo " --no-configure-mariadb Does not launch the default MariaDB server
  79. configuration scriptlet (default: False)"
  80. echo " --database-name Specifies a custom database name
  81. (default: \"nextcloud\")"
  82. echo " --database-user Specifies a custom database admin user
  83. (default \"nextcloud_admin\")"
  84. if [ "$1" ]; then
  85. echo
  86. echo "Error: $1"
  87. if [ "$2" ]; then
  88. exit $2
  89. fi
  90. fi
  91. exit 0
  92. }
  93. # Update current system
  94. _update_system() {
  95. [[ "$VERBOSE" = true ]] && echo "Updating current system"
  96. ERR=$( { apt update 1>/dev/null; } 2>&1 | grep -v "stable CLI interface" )
  97. [[ "$ERR" ]] && echo "Error during package cache update: $ERR"
  98. ERR=$( { apt -y upgrade 1>/dev/null; } 2>&1 | grep -v \
  99. "stable CLI interface" )
  100. [[ "$ERR" ]] && echo "Error during system package updates: $ERR"
  101. }
  102. # Ask the user for mysql `root` password and configure mysql in unattended mode
  103. _configure_mariadb_server() {
  104. read -sp 'Please type a `root` password for mysql database: ' \
  105. MYSQL_ROOT_PASSWORD && echo ""
  106. [[ "$VERBOSE" = true ]] && echo "Configuring mysql in unattended mode"
  107. ERR=$( { apt -y install expect >/dev/null; } 2>&1 | grep -v \
  108. "stable CLI interface" )
  109. [[ "$ERR" ]] && echo "Error during package installations: $ERR"
  110. SECURE_MYSQL=$(expect -c "
  111. set timeout 10
  112. spawn mysql_secure_installation
  113. expect \"Enter current password for root (enter for none):\"
  114. send \"\r\"
  115. expect \"Set root password?\"
  116. send \"y\r\"
  117. expect \"New password:\"
  118. send \"$MYSQL_ROOT_PASSWORD\r\"
  119. expect \"Re-enter new password:\"
  120. send \"$MYSQL_ROOT_PASSWORD\r\"
  121. expect \"Remove anonymous users?\"
  122. send \"y\r\"
  123. expect \"Disallow root login remotely?\"
  124. send \"y\r\"
  125. expect \"Remove test database and access to it?\"
  126. send \"y\r\"
  127. expect \"Reload privilege tables now?\"
  128. send \"y\r\"
  129. expect eof
  130. ")
  131. ERR=$( { echo "$SECURE_MYSQL" 1>/dev/null; } 2>&1 )
  132. [[ "$ERR" ]] && echo "Error during mysql_initialization: $ERR"
  133. ERR=$( { apt -y remove --purge expect >/dev/null; } 2>&1 | \
  134. grep -v "stable CLI interface" )
  135. [[ "$ERR" ]] && echo "Error during package removals: $ERR"
  136. unset SECURE_MYSQL
  137. }
  138. # Create Nextcloud database with associated login
  139. _configure_database() {
  140. [[ "$VERBOSE" = true ]] && echo "Creating Nextcloud database with \
  141. associated login"
  142. MYSQL_ROOT_USER="root"
  143. QUERY="command=password&format=plain&scheme=rrnnnrrnrnnnrrnrnnrr"
  144. MYSQL_USER_PASSWORD=$(curl -s \
  145. "https://www.passwordrandom.com/query?$QUERY")
  146. SQL="CREATE DATABASE nextcloud;"
  147. mysql -u$MYSQL_ROOT_USER -p$MYSQL_ROOT_PASSWORD -e "$SQL"
  148. SQL="GRANT ALL PRIVILEGES ON nextcloud.* TO nextcloud_admin@localhost
  149. IDENTIFIED BY '$MYSQL_USER_PASSWORD';"
  150. mysql -u$MYSQL_ROOT_USER -p$MYSQL_ROOT_PASSWORD -e "$SQL"
  151. SQL="FLUSH PRIVILEGES;"
  152. mysql -u$MYSQL_ROOT_USER -p$MYSQL_ROOT_PASSWORD -e "$SQL"
  153. unset SQL; unset MYSQL_ROOT_USER; unset MYSQL_ROOT_PASSWORD; unset QUERY
  154. }
  155. # Create required folders and set correct permissions
  156. _configure_permissions() {
  157. [[ "$VERBOSE" = true ]] && echo "Creating required folders and setting \
  158. correct permissions"
  159. mkdir -p "$INSTALL_DIR"/{public,data}
  160. chown -R www-data:www-data "$INSTALL_DIR"
  161. }
  162. # Configure Apache2 to run the website
  163. _configure_apache2() {
  164. # Ask the user for Apache2 FQDN hostname
  165. read -p 'Please type the FQDN Nextcloud should run on: ' \
  166. HOSTNAME && echo ""
  167. [[ "$VERBOSE" = true ]] && echo "Configuring Apache2 to run the website"
  168. cat << "EOF" > /etc/apache2/sites-available/cloud.conf
  169. <VirtualHost *:80>
  170. ServerName $HOSTNAME
  171. DocumentRoot "$INSTALL_DIR/public"
  172. <Directory "$INSTALL_DIR/public">
  173. Allow from all
  174. Require all granted
  175. </Directory>
  176. ErrorLog $INSTALL_DIR/error.log
  177. CustomLog $INSTALL_DIR/access.log combined
  178. </VirtualHost>
  179. EOF
  180. unset HOSTNAME; unset INSTALL_DIR
  181. }
  182. # Main program function, calls all other functions in the correct order
  183. _main() {
  184. _update_system
  185. _configure_permissions
  186. [[ "$NO_CONFIGURE_MARIADB" = false ]] && _configure_mariadb_server
  187. _configure_database
  188. _download_website
  189. _configure_apache2
  190. _enable_site
  191. }
  192. # Program execution
  193. _parse_params $@
  194. _main