Nextcloud Automation - for when you don't have time nor wish to spend too much time deploying stuff. https://nextcloud.com
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

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