freedombone-app-nextcloud 26KB

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