freedombone-app-nextcloud 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # nextcloud application
  12. # In tests this is much too slow, and it appeared to self-destruct after a few minutes
  13. # i.e. becoming totally unusable.
  14. #
  15. # License
  16. # =======
  17. #
  18. # Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
  19. #
  20. # This program is free software: you can redistribute it and/or modify
  21. # it under the terms of the GNU Affero General Public License as published by
  22. # the Free Software Foundation, either version 3 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU Affero General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU Affero General Public License
  31. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. VARIANTS='full full-vim cloud'
  33. IN_DEFAULT_INSTALL=0
  34. SHOW_ON_ABOUT=1
  35. NEXTCLOUD_DOMAIN_NAME=
  36. NEXTCLOUD_CODE=
  37. NEXTCLOUD_ONION_PORT=8112
  38. NEXTCLOUD_REPO="https://github.com/nextcloud/server"
  39. # Stable 12 branch
  40. NEXTCLOUD_COMMIT='5e22b330963d01feb636b24e7b1027b50b46e3c2'
  41. NEXTCLOUD_ADMIN_PASSWORD=
  42. nextcloud_variables=(ONION_ONLY
  43. NEXTCLOUD_DOMAIN_NAME
  44. NEXTCLOUD_CODE
  45. DDNS_PROVIDER
  46. MY_USERNAME)
  47. function remove_user_nextcloud {
  48. remove_username="$1"
  49. ${PROJECT_NAME}-pass -u $remove_username --rmapp nextcloud
  50. }
  51. function add_user_nextcloud {
  52. new_username="$1"
  53. new_user_password="$2"
  54. ${PROJECT_NAME}-pass -u $new_username -a nextcloud -p "$new_user_password"
  55. echo '0'
  56. }
  57. function change_password_nextcloud {
  58. curr_username="$1"
  59. export OC_PASS="$2"
  60. su -s /bin/sh www-data -c "php occ user:resetpassword --password-from-env $curr_username"
  61. ${PROJECT_NAME}-pass -u $curr_username -a nextcloud -p "$OC_PASS"
  62. export OC_PASS=""
  63. }
  64. function install_interactive_nextcloud {
  65. if [ ! $ONION_ONLY ]; then
  66. ONION_ONLY='no'
  67. fi
  68. if [[ $ONION_ONLY != "no" ]]; then
  69. NEXTCLOUD_DOMAIN_NAME='nextcloud.local'
  70. else
  71. NEXTCLOUD_DETAILS_COMPLETE=
  72. while [ ! $NEXTCLOUD_DETAILS_COMPLETE ]
  73. do
  74. data=$(tempfile 2>/dev/null)
  75. trap "rm -f $data" 0 1 2 5 15
  76. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  77. dialog --backtitle $"Freedombone Configuration" \
  78. --title $"NextCloud Configuration" \
  79. --form $"\nPlease enter your NextCloud details.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 13 65 3 \
  80. $"Domain:" 1 1 "$(grep 'NEXTCLOUD_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
  81. $"Code:" 2 1 "$(grep 'NEXTCLOUD_CODE' temp.cfg | awk -F '=' '{print $2}')" 2 15 40 255 \
  82. 2> $data
  83. else
  84. dialog --backtitle $"Freedombone Configuration" \
  85. --title $"NextCloud Configuration" \
  86. --form $"\nPlease enter your NextCloud details. The background image URL can be left blank.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 13 65 2 \
  87. $"Domain:" 1 1 "$(grep 'NEXTCLOUD_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
  88. 2> $data
  89. fi
  90. sel=$?
  91. case $sel in
  92. 1) exit 1;;
  93. 255) exit 1;;
  94. esac
  95. NEXTCLOUD_DOMAIN_NAME=$(cat $data | sed -n 1p)
  96. if [ ${#img_url} -gt 1 ]; then
  97. NEXTCLOUD_BACKGROUND_IMAGE_URL=$img_url
  98. fi
  99. if [ $NEXTCLOUD_DOMAIN_NAME ]; then
  100. TEST_DOMAIN_NAME=$NEXTCLOUD_DOMAIN_NAME
  101. validate_domain_name
  102. if [[ $TEST_DOMAIN_NAME != $NEXTCLOUD_DOMAIN_NAME ]]; then
  103. NEXTCLOUD_DOMAIN_NAME=
  104. dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
  105. else
  106. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  107. NEXTCLOUD_CODE=$(cat $data | sed -n 2p)
  108. validate_freedns_code "$NEXTCLOUD_CODE"
  109. if [ ! $VALID_CODE ]; then
  110. NEXTCLOUD_DOMAIN_NAME=
  111. fi
  112. fi
  113. fi
  114. fi
  115. if [ $NEXTCLOUD_DOMAIN_NAME ]; then
  116. NEXTCLOUD_DETAILS_COMPLETE="yes"
  117. fi
  118. done
  119. # remove any invalid characters
  120. if [ ${#NEXTCLOUD_TITLE} -gt 0 ]; then
  121. new_title=$(echo "$NEXTCLOUD_TITLE" | sed "s|'||g")
  122. NEXTCLOUD_TITLE="$new_title"
  123. fi
  124. # save the results in the config file
  125. write_config_param "NEXTCLOUD_CODE" "$NEXTCLOUD_CODE"
  126. fi
  127. write_config_param "NEXTCLOUD_DOMAIN_NAME" "$NEXTCLOUD_DOMAIN_NAME"
  128. APP_INSTALLED=1
  129. }
  130. function change_password_nextcloud {
  131. curr_username="$1"
  132. new_user_password="$2"
  133. read_config_param ${NEXTCLOUD_DOMAIN_NAME}
  134. ${PROJECT_NAME}-pass -u "$curr_username" -a nextcloud -p "$new_user_password"
  135. }
  136. function nextcloud_create_database {
  137. if [ -f $IMAGE_PASSWORD_FILE ]; then
  138. NEXTCLOUD_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  139. else
  140. if [ ! $NEXTCLOUD_ADMIN_PASSWORD ]; then
  141. NEXTCLOUD_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  142. fi
  143. fi
  144. if [ ! $NEXTCLOUD_ADMIN_PASSWORD ]; then
  145. return
  146. fi
  147. function_check create_database
  148. create_database nextcloud "$NEXTCLOUD_ADMIN_PASSWORD" $MY_USERNAME
  149. }
  150. function reconfigure_nextcloud {
  151. echo -n ''
  152. }
  153. function configure_interactive_nextcloud {
  154. echo -n ''
  155. }
  156. function upgrade_nextcloud {
  157. CURR_NEXTCLOUD_COMMIT=$(get_completion_param "nextcloud commit")
  158. if [[ "$CURR_NEXTCLOUD_COMMIT" == "$NEXTCLOUD_COMMIT" ]]; then
  159. chown -R www-data:www-data /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  160. chown -R www-data:www-data /var/www/$NEXTCLOUD_DOMAIN_NAME/data
  161. cd /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  162. sudo -u www-data ./occ maintenance:repair
  163. sudo -u www-data ./occ files:cleanup
  164. sudo -u www-data ./occ files:scan --all
  165. sudo -u www-data ./occ maintenance:mode --off
  166. return
  167. fi
  168. if grep -q "nextcloud domain" $COMPLETION_FILE; then
  169. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  170. fi
  171. # update to the next commit
  172. function_check set_repo_commit
  173. set_repo_commit /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs "nextcloud commit" "$NEXTCLOUD_COMMIT" $NEXTCLOUD_REPO
  174. chown -R www-data:www-data /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  175. chown -R www-data:www-data /var/www/$NEXTCLOUD_DOMAIN_NAME/data
  176. cd /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  177. sudo -u www-data ./occ maintenance:repair
  178. sudo -u www-data ./occ files:cleanup
  179. sudo -u www-data ./occ files:scan --all
  180. sudo -u www-data ./occ maintenance:mode --off
  181. }
  182. function backup_local_nextcloud {
  183. NEXTCLOUD_DOMAIN_NAME='nextcloud'
  184. if grep -q "nextcloud domain" $COMPLETION_FILE; then
  185. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  186. fi
  187. source_directory=/var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  188. if [ -d $source_directory ]; then
  189. function_check suspend_site
  190. suspend_site ${NEXTCLOUD_DOMAIN_NAME}
  191. dest_directory=nextcloudfiles
  192. function_check backup_directory_to_usb
  193. backup_directory_to_usb $source_directory $dest_directory
  194. source_directory=/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config
  195. dest_directory=nextcloudconfig
  196. backup_directory_to_usb $source_directory $dest_directory
  197. function_check backup_database_to_usb
  198. backup_database_to_usb nextcloud
  199. function_check restart_site
  200. restart_site
  201. fi
  202. }
  203. function restore_local_nextcloud {
  204. if ! grep -q "nextcloud domain" $COMPLETION_FILE; then
  205. return
  206. fi
  207. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  208. if [ $NEXTCLOUD_DOMAIN_NAME ]; then
  209. temp_restore_dir=/root/tempnextcloud
  210. function_check nextcloud_create_database
  211. nextcloud_create_database
  212. restore_database nextcloud ${NEXTCLOUD_DOMAIN_NAME}
  213. temp_restore_dir=/root/tempnextcloudfiles
  214. restore_directory_from_usb $temp_restore_dir nextcloudfiles
  215. temp_source_dir=$(find ${temp_restore_dir} -name data)
  216. cp -r ${temp_source_dir} /var/www/${NEXTCLOUD_DOMAIN_NAME}/
  217. if [ ! "$?" = "0" ]; then
  218. set_user_permissions
  219. backup_unmount_drive
  220. exit 346723
  221. fi
  222. rm -rf ${temp_restore_dir}
  223. temp_restore_dir=/root/tempnextcloudconfig
  224. restore_directory_from_usb $temp_restore_dir nextcloudconfig
  225. temp_source_dir=$(find ${temp_restore_dir} -name config)
  226. cp -r ${temp_source_dir} /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/
  227. if [ ! "$?" = "0" ]; then
  228. set_user_permissions
  229. backup_unmount_drive
  230. exit 3467343
  231. fi
  232. rm -rf ${temp_restore_dir}
  233. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs
  234. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  235. cd $nextcloud_dir
  236. sudo -u www-data ./occ maintenance:repair
  237. sudo -u www-data ./occ files:cleanup
  238. sudo -u www-data ./occ files:scan --all
  239. fi
  240. }
  241. function backup_remote_nextcloud {
  242. if grep -q "nextcloud domain" $COMPLETION_FILE; then
  243. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  244. temp_backup_dir=/var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  245. if [ -d $temp_backup_dir ]; then
  246. function_check suspend_site
  247. suspend_site ${NEXTCLOUD_DOMAIN_NAME}
  248. function_check backup_database_to_friend
  249. backup_database_to_friend nextcloud
  250. function_check backup_directory_to_friend
  251. backup_directory_to_friend $temp_backup_dir nextclouddata
  252. temp_backup_dir=/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config
  253. backup_directory_to_friend $temp_backup_dir nextcloudconfig
  254. function_check restart_site
  255. restart_site
  256. else
  257. echo $"nextcloud domain specified but not found in ${temp_backup_dir}"
  258. fi
  259. fi
  260. }
  261. function restore_remote_nextcloud {
  262. if grep -q "nextcloud domain" $COMPLETION_FILE; then
  263. echo $"Restoring nextcloud"
  264. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  265. function_check nextcloud_create_database
  266. nextcloud_create_database
  267. function_check restore_database_from_friend
  268. restore_database_from_friend nextcloud ${NEXTCLOUD_DOMAIN_NAME}
  269. if [ -d /root/tempnextcloud ]; then
  270. rm -rf /root/tempnextcloud
  271. fi
  272. temp_restore_dir=/root/tempnextcloudfiles
  273. restore_directory_from_friend $temp_restore_dir nextcloudfiles
  274. temp_source_dir=$(find ${temp_restore_dir} -name data)
  275. cp -r ${temp_source_dir} /var/www/${NEXTCLOUD_DOMAIN_NAME}/
  276. if [ ! "$?" = "0" ]; then
  277. exit 768254
  278. fi
  279. rm -rf ${temp_restore_dir}
  280. temp_restore_dir=/root/tempnextcloudconfig
  281. restore_directory_from_friend $temp_restore_dir nextcloudconfig
  282. temp_source_dir=$(find ${temp_restore_dir} -name config)
  283. cp -r ${temp_source_dir} /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/
  284. if [ ! "$?" = "0" ]; then
  285. exit 573427
  286. fi
  287. rm -rf ${temp_restore_dir}
  288. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs
  289. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  290. cd /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs
  291. sudo -u www-data ./occ maintenance:repair
  292. sudo -u www-data ./occ files:cleanup
  293. sudo -u www-data ./occ files:scan --all
  294. fi
  295. }
  296. function remove_nextcloud {
  297. if [ ${#NEXTCLOUD_DOMAIN_NAME} -eq 0 ]; then
  298. return
  299. fi
  300. function_check remove_nodejs
  301. remove_nodejs pleroma-nextcloud
  302. sed -i 's|env[PATH]|;env[PATH]|g' /etc/php/7.0/fpm/pool.d/www.conf
  303. read_config_param "NEXTCLOUD_DOMAIN_NAME"
  304. read_config_param "MY_USERNAME"
  305. echo "Removing $NEXTCLOUD_DOMAIN_NAME"
  306. nginx_dissite $NEXTCLOUD_DOMAIN_NAME
  307. remove_certs $NEXTCLOUD_DOMAIN_NAME
  308. if [ -d /var/www/$NEXTCLOUD_DOMAIN_NAME ]; then
  309. rm -rf /var/www/$NEXTCLOUD_DOMAIN_NAME
  310. fi
  311. if [ -f /etc/nginx/sites-available/$NEXTCLOUD_DOMAIN_NAME ]; then
  312. rm /etc/nginx/sites-available/$NEXTCLOUD_DOMAIN_NAME
  313. fi
  314. function_check drop_database
  315. drop_database nextcloud
  316. function_check remove_onion_service
  317. remove_onion_service nextcloud ${NEXTCLOUD_ONION_PORT}
  318. if grep -q "nextcloud" /etc/crontab; then
  319. sed -i "/nextcloud/d" /etc/crontab
  320. fi
  321. remove_app nextcloud
  322. remove_completion_param install_nextcloud
  323. sed -i '/nextcloud/d' $COMPLETION_FILE
  324. remove_backup_database_local nextcloud
  325. function_check remove_ddns_domain
  326. remove_ddns_domain $NEXTCLOUD_DOMAIN_NAME
  327. systemctl restart nginx
  328. systemctl restart php7.0-fpm
  329. }
  330. function install_nextcloud_main {
  331. if [ ! $NEXTCLOUD_DOMAIN_NAME ]; then
  332. echo $'No domain name was given for nextcloud'
  333. exit 7359
  334. fi
  335. if [[ $(app_is_installed nextcloud_main) == "1" ]]; then
  336. return
  337. fi
  338. function_check install_mariadb
  339. install_mariadb
  340. function_check get_mariadb_password
  341. get_mariadb_password
  342. function_check repair_databases_script
  343. repair_databases_script
  344. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
  345. apt-get -yq install php-intl memcached php-memcached libfcgi0ldbl
  346. apt-get -yq install php-zip
  347. # Ensure PATH is available to php
  348. if [ ! -f /etc/php/7.0/fpm/pool.d/www.conf ]; then
  349. echo $'No php www configuration file found'
  350. exit 628757
  351. fi
  352. sed -i 's|;env[PATH]|env[PATH]|g' /etc/php/7.0/fpm/pool.d/www.conf
  353. if [ ! -d /var/www/$NEXTCLOUD_DOMAIN_NAME ]; then
  354. mkdir /var/www/$NEXTCLOUD_DOMAIN_NAME
  355. fi
  356. if [ ! -d /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs ]; then
  357. if [ -d /repos/nextcloud ]; then
  358. mkdir /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  359. cp -r -p /repos/nextcloud/. /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  360. cd /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  361. git pull
  362. else
  363. function_check git_clone
  364. git_clone $NEXTCLOUD_REPO /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  365. fi
  366. if [ ! -d /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs ]; then
  367. echo $'Unable to clone nextcloud repo'
  368. exit 87525
  369. fi
  370. fi
  371. cd /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  372. git submodule update --init
  373. git checkout $NEXTCLOUD_COMMIT -b $NEXTCLOUD_COMMIT
  374. set_completion_param "nextcloud commit" "$NEXTCLOUD_COMMIT"
  375. chmod g+w /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  376. chown -R www-data:www-data /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  377. function_check nextcloud_create_database
  378. nextcloud_create_database
  379. if [ ! -f "/etc/aliases" ]; then
  380. touch /etc/aliases
  381. fi
  382. if ! grep -q "www-data: root" /etc/aliases; then
  383. echo 'www-data: root' >> /etc/aliases
  384. fi
  385. function_check add_ddns_domain
  386. add_ddns_domain $NEXTCLOUD_DOMAIN_NAME
  387. nextcloud_nginx_site=/etc/nginx/sites-available/$NEXTCLOUD_DOMAIN_NAME
  388. if [[ $ONION_ONLY == "no" ]]; then
  389. function_check nginx_http_redirect
  390. nginx_http_redirect $NEXTCLOUD_DOMAIN_NAME
  391. echo 'server {' >> $nextcloud_nginx_site
  392. echo ' listen 443 ssl;' >> $nextcloud_nginx_site
  393. echo ' listen [::]:443 ssl;' >> $nextcloud_nginx_site
  394. echo " server_name $NEXTCLOUD_DOMAIN_NAME;" >> $nextcloud_nginx_site
  395. echo '' >> $nextcloud_nginx_site
  396. echo ' # Security' >> $nextcloud_nginx_site
  397. function_check nginx_ssl
  398. nginx_ssl $NEXTCLOUD_DOMAIN_NAME mobile
  399. function_check nginx_disable_sniffing
  400. nginx_disable_sniffing $NEXTCLOUD_DOMAIN_NAME
  401. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $nextcloud_nginx_site
  402. echo '' >> $nextcloud_nginx_site
  403. echo ' # Logs' >> $nextcloud_nginx_site
  404. echo ' access_log /dev/null;' >> $nextcloud_nginx_site
  405. echo ' error_log /dev/null;' >> $nextcloud_nginx_site
  406. echo '' >> $nextcloud_nginx_site
  407. echo ' # Root' >> $nextcloud_nginx_site
  408. echo " root /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs;" >> $nextcloud_nginx_site
  409. echo '' >> $nextcloud_nginx_site
  410. echo ' # Index' >> $nextcloud_nginx_site
  411. echo ' index index.php;' >> $nextcloud_nginx_site
  412. echo '' >> $nextcloud_nginx_site
  413. echo ' # PHP' >> $nextcloud_nginx_site
  414. echo ' location ~ \.php {' >> $nextcloud_nginx_site
  415. echo ' include snippets/fastcgi-php.conf;' >> $nextcloud_nginx_site
  416. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;' >> $nextcloud_nginx_site
  417. echo ' }' >> $nextcloud_nginx_site
  418. echo '' >> $nextcloud_nginx_site
  419. echo ' # Location' >> $nextcloud_nginx_site
  420. echo ' location / {' >> $nextcloud_nginx_site
  421. function_check nginx_limits
  422. nginx_limits $NEXTCLOUD_DOMAIN_NAME '15m'
  423. echo ' try_files $uri $uri/ @nextcloud;' >> $nextcloud_nginx_site
  424. echo ' }' >> $nextcloud_nginx_site
  425. echo '' >> $nextcloud_nginx_site
  426. echo ' # Fancy URLs' >> $nextcloud_nginx_site
  427. echo ' location @nextcloud {' >> $nextcloud_nginx_site
  428. echo ' rewrite ^(.*)$ /index.php?p=$1 last;' >> $nextcloud_nginx_site
  429. echo ' }' >> $nextcloud_nginx_site
  430. echo '' >> $nextcloud_nginx_site
  431. echo ' # Restrict access that is unnecessary anyway' >> $nextcloud_nginx_site
  432. echo ' location ~ /\.(ht|git) {' >> $nextcloud_nginx_site
  433. echo ' deny all;' >> $nextcloud_nginx_site
  434. echo ' }' >> $nextcloud_nginx_site
  435. echo '' >> $nextcloud_nginx_site
  436. echo ' location = /.well-known/carddav {' >> $nextcloud_nginx_site
  437. echo ' return 301 $scheme://$host/remote.php/dav;' >> $nextcloud_nginx_site
  438. echo ' }' >> $nextcloud_nginx_site
  439. echo ' location = /.well-known/caldav {' >> $nextcloud_nginx_site
  440. echo ' return 301 $scheme://$host/remote.php/dav;' >> $nextcloud_nginx_site
  441. echo ' }' >> $nextcloud_nginx_site
  442. echo '' >> $nextcloud_nginx_site
  443. echo ' location /.well-known/acme-challenge { }' >> $nextcloud_nginx_site
  444. echo '' >> $nextcloud_nginx_site
  445. # DO NOT ENABLE KEYBASE. nextcloud really doesn't like having a .well-known directory
  446. echo '}' >> $nextcloud_nginx_site
  447. else
  448. echo -n '' > $nextcloud_nginx_site
  449. fi
  450. echo 'server {' >> $nextcloud_nginx_site
  451. echo " listen 127.0.0.1:$NEXTCLOUD_ONION_PORT default_server;" >> $nextcloud_nginx_site
  452. echo " server_name $NEXTCLOUD_DOMAIN_NAME;" >> $nextcloud_nginx_site
  453. echo '' >> $nextcloud_nginx_site
  454. function_check nginx_disable_sniffing
  455. nginx_disable_sniffing $NEXTCLOUD_DOMAIN_NAME
  456. echo '' >> $nextcloud_nginx_site
  457. echo ' # Logs' >> $nextcloud_nginx_site
  458. echo ' access_log /dev/null;' >> $nextcloud_nginx_site
  459. echo ' error_log /dev/null;' >> $nextcloud_nginx_site
  460. echo '' >> $nextcloud_nginx_site
  461. echo ' # Root' >> $nextcloud_nginx_site
  462. echo " root /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs;" >> $nextcloud_nginx_site
  463. echo '' >> $nextcloud_nginx_site
  464. echo ' # Index' >> $nextcloud_nginx_site
  465. echo ' index index.php;' >> $nextcloud_nginx_site
  466. echo '' >> $nextcloud_nginx_site
  467. echo ' # PHP' >> $nextcloud_nginx_site
  468. echo ' location ~ \.php {' >> $nextcloud_nginx_site
  469. echo ' include snippets/fastcgi-php.conf;' >> $nextcloud_nginx_site
  470. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;' >> $nextcloud_nginx_site
  471. echo ' }' >> $nextcloud_nginx_site
  472. echo '' >> $nextcloud_nginx_site
  473. echo ' # Location' >> $nextcloud_nginx_site
  474. echo ' location / {' >> $nextcloud_nginx_site
  475. function_check nginx_limits
  476. nginx_limits $NEXTCLOUD_DOMAIN_NAME '15m'
  477. echo ' try_files $uri $uri/ @nextcloud;' >> $nextcloud_nginx_site
  478. echo ' }' >> $nextcloud_nginx_site
  479. echo '' >> $nextcloud_nginx_site
  480. echo ' # Fancy URLs' >> $nextcloud_nginx_site
  481. echo ' location @nextcloud {' >> $nextcloud_nginx_site
  482. echo ' rewrite ^(.*)$ /index.php?p=$1 last;' >> $nextcloud_nginx_site
  483. echo ' }' >> $nextcloud_nginx_site
  484. echo '' >> $nextcloud_nginx_site
  485. echo ' # Restrict access that is unnecessary anyway' >> $nextcloud_nginx_site
  486. echo ' location ~ /\.(ht|git) {' >> $nextcloud_nginx_site
  487. echo ' deny all;' >> $nextcloud_nginx_site
  488. echo ' }' >> $nextcloud_nginx_site
  489. echo '' >> $nextcloud_nginx_site
  490. echo ' location = /.well-known/carddav {' >> $nextcloud_nginx_site
  491. echo ' return 301 $scheme://$host/remote.php/dav;' >> $nextcloud_nginx_site
  492. echo ' }' >> $nextcloud_nginx_site
  493. echo ' location = /.well-known/caldav {' >> $nextcloud_nginx_site
  494. echo ' return 301 $scheme://$host/remote.php/dav;' >> $nextcloud_nginx_site
  495. echo ' }' >> $nextcloud_nginx_site
  496. echo '' >> $nextcloud_nginx_site
  497. echo ' location /.well-known/acme-challenge { }' >> $nextcloud_nginx_site
  498. echo '}' >> $nextcloud_nginx_site
  499. sed -i 's| DENY;| SAMEORIGIN;|g' $nextcloud_nginx_site
  500. # NextCloud doesn't like content-security-policy at all
  501. sed -i '/Content-Security-Policy/d' $nextcloud_nginx_site
  502. function_check configure_php
  503. configure_php
  504. function_check create_site_certificate
  505. create_site_certificate $NEXTCLOUD_DOMAIN_NAME 'yes'
  506. if [[ $ONION_ONLY == "no" ]]; then
  507. if [ ! -f /etc/ssl/certs/${NEXTCLOUD_DOMAIN_NAME}.pem ]; then
  508. echo $'Certificate not generated for nextcloud'
  509. exit 725762
  510. fi
  511. fi
  512. # Ensure that the database gets backed up locally, if remote
  513. # backups are not being used
  514. function_check backup_databases_script_header
  515. backup_databases_script_header
  516. function_check backup_database_local
  517. backup_database_local nextcloud
  518. function_check nginx_ensite
  519. nginx_ensite $NEXTCLOUD_DOMAIN_NAME
  520. # NOTE: For the typical case always enable SSL and only
  521. # disable it if in onion only mode. This is due to complexities
  522. # with the way URLs are generated by nextcloud
  523. nextcloud_ssl='always'
  524. if [[ $ONION_ONLY != 'no' ]]; then
  525. nextcloud_ssl='never'
  526. fi
  527. NEXTCLOUD_ONION_HOSTNAME=$(add_onion_service nextcloud 80 ${NEXTCLOUD_ONION_PORT})
  528. NEXTCLOUD_SERVER=${NEXTCLOUD_DOMAIN_NAME}
  529. if [[ $ONION_ONLY != 'no' ]]; then
  530. NEXTCLOUD_SERVER=${NEXTCLOUD_ONION_HOSTNAME}
  531. fi
  532. systemctl restart php7.0-fpm
  533. systemctl restart nginx
  534. ${PROJECT_NAME}-addemail -u $MY_USERNAME -e "noreply@$NEXTCLOUD_DOMAIN_NAME" -g nextcloud --public no
  535. ${PROJECT_NAME}-pass -u $MY_USERNAME -a nextcloud -p "$NEXTCLOUD_ADMIN_PASSWORD"
  536. cd /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs
  537. if [ -d config ]; then
  538. chown -R www-data:www-data config
  539. fi
  540. if [ -d data ]; then
  541. chown -R www-data:www-data data
  542. fi
  543. chmod +x occ
  544. ./occ maintenance:install --database-name nextcloud --admin-user ${MY_USERNAME} --admin-pass "${NEXTCLOUD_ADMIN_PASSWORD}" --database mysql --database-user root --database-pass "${MARIADB_PASSWORD}"
  545. if [ ! -d data ]; then
  546. echo $'Nextcloud data directory was not found. This probably means that the installation failed.'
  547. echo ''
  548. echo $'Install command was:'
  549. echo "./occ maintenance:install --database-name nextcloud --admin-user ${MY_USERNAME} --admin-pass \"${NEXTCLOUD_ADMIN_PASSWORD}\" --database mysql --database-user root --database-pass \"${MARIADB_PASSWORD}\""
  550. exit 83522
  551. fi
  552. chown -R www-data:www-data config
  553. chown -R www-data:www-data data
  554. ./occ check
  555. ./occ status
  556. ./occ app:list
  557. ./occ app:enable encryption
  558. ./occ config:system:set appstoreenabled --value=false
  559. chmod g+w /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config/config.php
  560. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs
  561. chmod 0644 .htaccess
  562. chmod 0750 data
  563. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  564. sudo -u www-data ./occ config:system:set trusted_domains 1 --value=$NEXTCLOUD_DOMAIN_NAME
  565. sudo -u www-data ./occ config:system:set trusted_domains 2 --value=$NEXTCLOUD_ONION_HOSTNAME
  566. sudo -u www-data ./occ files:cleanup
  567. sudo -u www-data ./occ files:scan --all
  568. sudo -u www-data ./occ maintenance:repair
  569. sudo -u www-data ./occ maintenance:mode --off
  570. systemctl restart mariadb
  571. # move the data directory
  572. mv /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/data /var/www/${NEXTCLOUD_DOMAIN_NAME}/
  573. sed -i "s|'datadirectory'.*|'datadirectory' => '/var/www/$NEXTCLOUD_DOMAIN_NAME/data',|g" /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config/config.php
  574. set_completion_param "nextcloud domain" "$NEXTCLOUD_DOMAIN_NAME"
  575. install_completed nextcloud_main
  576. }
  577. function install_nextcloud {
  578. if [ ! $ONION_ONLY ]; then
  579. ONION_ONLY='no'
  580. fi
  581. install_nextcloud_main
  582. APP_INSTALLED=1
  583. }
  584. # NOTE: deliberately there is no "exit 0"