freedombone-app-nextcloud 24KB

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