freedombone-app-nextcloud 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. }
  172. function backup_local_nextcloud {
  173. NEXTCLOUD_DOMAIN_NAME='nextcloud'
  174. if grep -q "nextcloud domain" $COMPLETION_FILE; then
  175. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  176. fi
  177. source_directory=/var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  178. if [ -d $source_directory ]; then
  179. function_check suspend_site
  180. suspend_site ${NEXTCLOUD_DOMAIN_NAME}
  181. dest_directory=nextcloudfiles
  182. function_check backup_directory_to_usb
  183. backup_directory_to_usb $source_directory $dest_directory
  184. source_directory=/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config
  185. dest_directory=nextcloudconfig
  186. backup_directory_to_usb $source_directory $dest_directory
  187. function_check backup_database_to_usb
  188. backup_database_to_usb nextcloud
  189. function_check restart_site
  190. restart_site
  191. fi
  192. }
  193. function restore_local_nextcloud {
  194. if ! grep -q "nextcloud domain" $COMPLETION_FILE; then
  195. return
  196. fi
  197. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  198. if [ $NEXTCLOUD_DOMAIN_NAME ]; then
  199. temp_restore_dir=/root/tempnextcloud
  200. function_check nextcloud_create_database
  201. nextcloud_create_database
  202. restore_database nextcloud ${NEXTCLOUD_DOMAIN_NAME}
  203. temp_restore_dir=/root/tempnextcloudfiles
  204. restore_directory_from_usb $temp_restore_dir nextcloudfiles
  205. temp_source_dir=$(find ${temp_restore_dir} -name data)
  206. cp -r ${temp_source_dir} /var/www/${NEXTCLOUD_DOMAIN_NAME}/
  207. if [ ! "$?" = "0" ]; then
  208. set_user_permissions
  209. backup_unmount_drive
  210. exit 346723
  211. fi
  212. rm -rf ${temp_restore_dir}
  213. temp_restore_dir=/root/tempnextcloudconfig
  214. restore_directory_from_usb $temp_restore_dir nextcloudconfig
  215. temp_source_dir=$(find ${temp_restore_dir} -name config)
  216. cp -r ${temp_source_dir} /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/
  217. if [ ! "$?" = "0" ]; then
  218. set_user_permissions
  219. backup_unmount_drive
  220. exit 3467343
  221. fi
  222. rm -rf ${temp_restore_dir}
  223. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs
  224. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  225. cd $nextcloud_dir
  226. sudo -u www-data ./occ maintenance:repair
  227. sudo -u www-data ./occ files:cleanup
  228. sudo -u www-data ./occ files:scan --all
  229. fi
  230. }
  231. function backup_remote_nextcloud {
  232. if grep -q "nextcloud domain" $COMPLETION_FILE; then
  233. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  234. temp_backup_dir=/var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  235. if [ -d $temp_backup_dir ]; then
  236. function_check suspend_site
  237. suspend_site ${NEXTCLOUD_DOMAIN_NAME}
  238. function_check backup_database_to_friend
  239. backup_database_to_friend nextcloud
  240. function_check backup_directory_to_friend
  241. backup_directory_to_friend $temp_backup_dir nextclouddata
  242. temp_backup_dir=/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config
  243. backup_directory_to_friend $temp_backup_dir nextcloudconfig
  244. function_check restart_site
  245. restart_site
  246. else
  247. echo $"nextcloud domain specified but not found in ${temp_backup_dir}"
  248. fi
  249. fi
  250. }
  251. function restore_remote_nextcloud {
  252. if grep -q "nextcloud domain" $COMPLETION_FILE; then
  253. echo $"Restoring nextcloud"
  254. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  255. function_check nextcloud_create_database
  256. nextcloud_create_database
  257. function_check restore_database_from_friend
  258. restore_database_from_friend nextcloud ${NEXTCLOUD_DOMAIN_NAME}
  259. if [ -d /root/tempnextcloud ]; then
  260. rm -rf /root/tempnextcloud
  261. fi
  262. temp_restore_dir=/root/tempnextcloudfiles
  263. restore_directory_from_friend $temp_restore_dir nextcloudfiles
  264. temp_source_dir=$(find ${temp_restore_dir} -name data)
  265. cp -r ${temp_source_dir} /var/www/${NEXTCLOUD_DOMAIN_NAME}/
  266. if [ ! "$?" = "0" ]; then
  267. exit 768254
  268. fi
  269. rm -rf ${temp_restore_dir}
  270. temp_restore_dir=/root/tempnextcloudconfig
  271. restore_directory_from_friend $temp_restore_dir nextcloudconfig
  272. temp_source_dir=$(find ${temp_restore_dir} -name config)
  273. cp -r ${temp_source_dir} /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/
  274. if [ ! "$?" = "0" ]; then
  275. exit 573427
  276. fi
  277. rm -rf ${temp_restore_dir}
  278. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs
  279. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  280. cd /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs
  281. sudo -u www-data ./occ maintenance:repair
  282. sudo -u www-data ./occ files:cleanup
  283. sudo -u www-data ./occ files:scan --all
  284. fi
  285. }
  286. function remove_nextcloud {
  287. if [ ${#NEXTCLOUD_DOMAIN_NAME} -eq 0 ]; then
  288. return
  289. fi
  290. function_check remove_nodejs
  291. remove_nodejs pleroma-nextcloud
  292. sed -i 's|env[PATH]|;env[PATH]|g' /etc/php5/fpm/pool.d/www.conf
  293. read_config_param "NEXTCLOUD_DOMAIN_NAME"
  294. read_config_param "MY_USERNAME"
  295. echo "Removing $NEXTCLOUD_DOMAIN_NAME"
  296. nginx_dissite $NEXTCLOUD_DOMAIN_NAME
  297. remove_certs $NEXTCLOUD_DOMAIN_NAME
  298. if [ -d /var/www/$NEXTCLOUD_DOMAIN_NAME ]; then
  299. rm -rf /var/www/$NEXTCLOUD_DOMAIN_NAME
  300. fi
  301. if [ -f /etc/nginx/sites-available/$NEXTCLOUD_DOMAIN_NAME ]; then
  302. rm /etc/nginx/sites-available/$NEXTCLOUD_DOMAIN_NAME
  303. fi
  304. function_check drop_database
  305. drop_database nextcloud
  306. function_check remove_onion_service
  307. remove_onion_service nextcloud ${NEXTCLOUD_ONION_PORT}
  308. if grep -q "nextcloud" /etc/crontab; then
  309. sed -i "/nextcloud/d" /etc/crontab
  310. fi
  311. remove_app nextcloud
  312. remove_completion_param install_nextcloud
  313. sed -i '/nextcloud/d' $COMPLETION_FILE
  314. remove_backup_database_local nextcloud
  315. function_check remove_ddns_domain
  316. remove_ddns_domain $NEXTCLOUD_DOMAIN_NAME
  317. systemctl restart nginx
  318. systemctl restart php5-fpm
  319. }
  320. function install_nextcloud_main {
  321. if [ ! $NEXTCLOUD_DOMAIN_NAME ]; then
  322. echo $'No domain name was given for nextcloud'
  323. exit 7359
  324. fi
  325. if [[ $(app_is_installed nextcloud_main) == "1" ]]; then
  326. return
  327. fi
  328. function_check install_mariadb
  329. install_mariadb
  330. function_check get_mariadb_password
  331. get_mariadb_password
  332. function_check repair_databases_script
  333. repair_databases_script
  334. apt-get -yq install php-gettext php5-curl php5-gd php5-mysql git curl php-xml-parser
  335. apt-get -yq install php5-memcached php5-intl memcached php5-memcached
  336. # Ensure PATH is available to php
  337. if [ ! -f /etc/php5/fpm/pool.d/www.conf ]; then
  338. echo $'No php www configuration file found'
  339. exit 628757
  340. fi
  341. sed -i 's|;env[PATH]|env[PATH]|g' /etc/php5/fpm/pool.d/www.conf
  342. if [ ! -d /var/www/$NEXTCLOUD_DOMAIN_NAME ]; then
  343. mkdir /var/www/$NEXTCLOUD_DOMAIN_NAME
  344. fi
  345. if [ ! -d /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs ]; then
  346. function_check git_clone
  347. git_clone $NEXTCLOUD_REPO /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  348. if [ ! -d /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs ]; then
  349. echo $'Unable to clone nextcloud repo'
  350. exit 87525
  351. fi
  352. fi
  353. cd /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  354. git submodule update --init
  355. git checkout $NEXTCLOUD_COMMIT -b $NEXTCLOUD_COMMIT
  356. set_completion_param "nextcloud commit" "$NEXTCLOUD_COMMIT"
  357. chmod g+w /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  358. chown -R www-data:www-data /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  359. function_check nextcloud_create_database
  360. nextcloud_create_database
  361. if [ ! -f "/etc/aliases" ]; then
  362. touch /etc/aliases
  363. fi
  364. if ! grep -q "www-data: root" /etc/aliases; then
  365. echo 'www-data: root' >> /etc/aliases
  366. fi
  367. function_check add_ddns_domain
  368. add_ddns_domain $NEXTCLOUD_DOMAIN_NAME
  369. nextcloud_nginx_site=/etc/nginx/sites-available/$NEXTCLOUD_DOMAIN_NAME
  370. if [[ $ONION_ONLY == "no" ]]; then
  371. function_check nginx_http_redirect
  372. nginx_http_redirect $NEXTCLOUD_DOMAIN_NAME
  373. echo 'server {' >> $nextcloud_nginx_site
  374. echo ' listen 443 ssl;' >> $nextcloud_nginx_site
  375. echo ' listen [::]:443 ssl;' >> $nextcloud_nginx_site
  376. echo " server_name $NEXTCLOUD_DOMAIN_NAME;" >> $nextcloud_nginx_site
  377. echo '' >> $nextcloud_nginx_site
  378. echo ' # Security' >> $nextcloud_nginx_site
  379. function_check nginx_ssl
  380. nginx_ssl $NEXTCLOUD_DOMAIN_NAME
  381. function_check nginx_disable_sniffing
  382. nginx_disable_sniffing $NEXTCLOUD_DOMAIN_NAME
  383. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $nextcloud_nginx_site
  384. echo '' >> $nextcloud_nginx_site
  385. echo ' # Logs' >> $nextcloud_nginx_site
  386. echo ' access_log /dev/null;' >> $nextcloud_nginx_site
  387. echo ' error_log /dev/null;' >> $nextcloud_nginx_site
  388. echo '' >> $nextcloud_nginx_site
  389. echo ' # Root' >> $nextcloud_nginx_site
  390. echo " root /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs;" >> $nextcloud_nginx_site
  391. echo '' >> $nextcloud_nginx_site
  392. echo ' # Index' >> $nextcloud_nginx_site
  393. echo ' index index.php;' >> $nextcloud_nginx_site
  394. echo '' >> $nextcloud_nginx_site
  395. echo ' # PHP' >> $nextcloud_nginx_site
  396. echo ' location ~ \.php {' >> $nextcloud_nginx_site
  397. echo ' include snippets/fastcgi-php.conf;' >> $nextcloud_nginx_site
  398. echo ' fastcgi_pass unix:/var/run/php5-fpm.sock;' >> $nextcloud_nginx_site
  399. echo ' }' >> $nextcloud_nginx_site
  400. echo '' >> $nextcloud_nginx_site
  401. echo ' # Location' >> $nextcloud_nginx_site
  402. echo ' location / {' >> $nextcloud_nginx_site
  403. function_check nginx_limits
  404. nginx_limits $NEXTCLOUD_DOMAIN_NAME '15m'
  405. echo ' try_files $uri $uri/ @nextcloud;' >> $nextcloud_nginx_site
  406. echo ' }' >> $nextcloud_nginx_site
  407. echo '' >> $nextcloud_nginx_site
  408. echo ' # Fancy URLs' >> $nextcloud_nginx_site
  409. echo ' location @nextcloud {' >> $nextcloud_nginx_site
  410. echo ' rewrite ^(.*)$ /index.php?p=$1 last;' >> $nextcloud_nginx_site
  411. echo ' }' >> $nextcloud_nginx_site
  412. echo '' >> $nextcloud_nginx_site
  413. echo ' # Restrict access that is unnecessary anyway' >> $nextcloud_nginx_site
  414. echo ' location ~ /\.(ht|git) {' >> $nextcloud_nginx_site
  415. echo ' deny all;' >> $nextcloud_nginx_site
  416. echo ' }' >> $nextcloud_nginx_site
  417. echo '' >> $nextcloud_nginx_site
  418. echo ' location = /.well-known/carddav {' >> $nextcloud_nginx_site
  419. echo ' return 301 $scheme://$host/remote.php/dav;' >> $nextcloud_nginx_site
  420. echo ' }' >> $nextcloud_nginx_site
  421. echo ' location = /.well-known/caldav {' >> $nextcloud_nginx_site
  422. echo ' return 301 $scheme://$host/remote.php/dav;' >> $nextcloud_nginx_site
  423. echo ' }' >> $nextcloud_nginx_site
  424. echo '' >> $nextcloud_nginx_site
  425. echo ' location /.well-known/acme-challenge { }' >> $nextcloud_nginx_site
  426. echo '' >> $nextcloud_nginx_site
  427. # DO NOT ENABLE KEYBASE. nextcloud really doesn't like having a .well-known directory
  428. echo '}' >> $nextcloud_nginx_site
  429. else
  430. echo -n '' > $nextcloud_nginx_site
  431. fi
  432. echo 'server {' >> $nextcloud_nginx_site
  433. echo " listen 127.0.0.1:$NEXTCLOUD_ONION_PORT default_server;" >> $nextcloud_nginx_site
  434. echo " server_name $NEXTCLOUD_DOMAIN_NAME;" >> $nextcloud_nginx_site
  435. echo '' >> $nextcloud_nginx_site
  436. function_check nginx_disable_sniffing
  437. nginx_disable_sniffing $NEXTCLOUD_DOMAIN_NAME
  438. echo '' >> $nextcloud_nginx_site
  439. echo ' # Logs' >> $nextcloud_nginx_site
  440. echo ' access_log /dev/null;' >> $nextcloud_nginx_site
  441. echo ' error_log /dev/null;' >> $nextcloud_nginx_site
  442. echo '' >> $nextcloud_nginx_site
  443. echo ' # Root' >> $nextcloud_nginx_site
  444. echo " root /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs;" >> $nextcloud_nginx_site
  445. echo '' >> $nextcloud_nginx_site
  446. echo ' # Index' >> $nextcloud_nginx_site
  447. echo ' index index.php;' >> $nextcloud_nginx_site
  448. echo '' >> $nextcloud_nginx_site
  449. echo ' # PHP' >> $nextcloud_nginx_site
  450. echo ' location ~ \.php {' >> $nextcloud_nginx_site
  451. echo ' include snippets/fastcgi-php.conf;' >> $nextcloud_nginx_site
  452. echo ' fastcgi_pass unix:/var/run/php5-fpm.sock;' >> $nextcloud_nginx_site
  453. echo ' }' >> $nextcloud_nginx_site
  454. echo '' >> $nextcloud_nginx_site
  455. echo ' # Location' >> $nextcloud_nginx_site
  456. echo ' location / {' >> $nextcloud_nginx_site
  457. function_check nginx_limits
  458. nginx_limits $NEXTCLOUD_DOMAIN_NAME '15m'
  459. echo ' try_files $uri $uri/ @nextcloud;' >> $nextcloud_nginx_site
  460. echo ' }' >> $nextcloud_nginx_site
  461. echo '' >> $nextcloud_nginx_site
  462. echo ' # Fancy URLs' >> $nextcloud_nginx_site
  463. echo ' location @nextcloud {' >> $nextcloud_nginx_site
  464. echo ' rewrite ^(.*)$ /index.php?p=$1 last;' >> $nextcloud_nginx_site
  465. echo ' }' >> $nextcloud_nginx_site
  466. echo '' >> $nextcloud_nginx_site
  467. echo ' # Restrict access that is unnecessary anyway' >> $nextcloud_nginx_site
  468. echo ' location ~ /\.(ht|git) {' >> $nextcloud_nginx_site
  469. echo ' deny all;' >> $nextcloud_nginx_site
  470. echo ' }' >> $nextcloud_nginx_site
  471. echo '' >> $nextcloud_nginx_site
  472. echo ' location = /.well-known/carddav {' >> $nextcloud_nginx_site
  473. echo ' return 301 $scheme://$host/remote.php/dav;' >> $nextcloud_nginx_site
  474. echo ' }' >> $nextcloud_nginx_site
  475. echo ' location = /.well-known/caldav {' >> $nextcloud_nginx_site
  476. echo ' return 301 $scheme://$host/remote.php/dav;' >> $nextcloud_nginx_site
  477. echo ' }' >> $nextcloud_nginx_site
  478. echo '' >> $nextcloud_nginx_site
  479. echo ' location /.well-known/acme-challenge { }' >> $nextcloud_nginx_site
  480. echo '}' >> $nextcloud_nginx_site
  481. sed -i 's| DENY;| SAMEORIGIN;|g' $nextcloud_nginx_site
  482. # NextCloud doesn't like content-security-policy at all
  483. sed -i '/Content-Security-Policy/d' $nextcloud_nginx_site
  484. function_check configure_php
  485. configure_php
  486. function_check create_site_certificate
  487. create_site_certificate $NEXTCLOUD_DOMAIN_NAME 'yes'
  488. if [[ $ONION_ONLY == "no" ]]; then
  489. if [ ! -f /etc/ssl/certs/${NEXTCLOUD_DOMAIN_NAME}.pem ]; then
  490. echo $'Certificate not generated for nextcloud'
  491. exit 725762
  492. fi
  493. fi
  494. # Ensure that the database gets backed up locally, if remote
  495. # backups are not being used
  496. function_check backup_databases_script_header
  497. backup_databases_script_header
  498. function_check backup_database_local
  499. backup_database_local nextcloud
  500. function_check nginx_ensite
  501. nginx_ensite $NEXTCLOUD_DOMAIN_NAME
  502. # NOTE: For the typical case always enable SSL and only
  503. # disable it if in onion only mode. This is due to complexities
  504. # with the way URLs are generated by nextcloud
  505. nextcloud_ssl='always'
  506. if [[ $ONION_ONLY != 'no' ]]; then
  507. nextcloud_ssl='never'
  508. fi
  509. NEXTCLOUD_ONION_HOSTNAME=$(add_onion_service nextcloud 80 ${NEXTCLOUD_ONION_PORT})
  510. NEXTCLOUD_SERVER=${NEXTCLOUD_DOMAIN_NAME}
  511. if [[ $ONION_ONLY != 'no' ]]; then
  512. NEXTCLOUD_SERVER=${NEXTCLOUD_ONION_HOSTNAME}
  513. fi
  514. systemctl restart php5-fpm
  515. systemctl restart nginx
  516. ${PROJECT_NAME}-addemail -u $MY_USERNAME -e "noreply@$NEXTCLOUD_DOMAIN_NAME" -g nextcloud --public no
  517. ${PROJECT_NAME}-pass -u $MY_USERNAME -a nextcloud -p "$NEXTCLOUD_ADMIN_PASSWORD"
  518. cd /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs
  519. chown -R www-data:www-data config
  520. chown -R www-data:www-data data
  521. chmod +x occ
  522. ./occ maintenance:install --database-name nextcloud --admin-user ${MY_USERNAME} --admin-pass "${NEXTCLOUD_ADMIN_PASSWORD}" --database mysql --database-user root --database-pass "${MARIADB_PASSWORD}"
  523. ./occ check
  524. ./occ status
  525. ./occ app:list
  526. ./occ app:enable encryption
  527. ./occ config:system:set appstoreenabled --value=false
  528. ./occ config:system:set trusted_domains 1 --value=$NEXTCLOUD_DOMAIN_NAME
  529. chmod g+w /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config/config.php
  530. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs
  531. chmod 0644 .htaccess
  532. chmod 0750 data
  533. chown -R www-data:www-data /var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  534. sudo -u www-data ./occ files:cleanup
  535. sudo -u www-data ./occ files:scan --all
  536. sudo -u www-data ./occ maintenance:repair
  537. # move the data directory
  538. mv /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/data /var/www/${NEXTCLOUD_DOMAIN_NAME}/
  539. sed -i "s|'datadirectory'.*|'datadirectory' => '/var/www/$NEXTCLOUD_DOMAIN_NAME/data',|g" /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config/config.php
  540. set_completion_param "nextcloud domain" "$NEXTCLOUD_DOMAIN_NAME"
  541. install_completed nextcloud_main
  542. }
  543. function install_nextcloud {
  544. if [ ! $ONION_ONLY ]; then
  545. ONION_ONLY='no'
  546. fi
  547. install_nextcloud_main
  548. APP_INSTALLED=1
  549. }
  550. # NOTE: deliberately there is no "exit 0"