freedombone-app-nextcloud 24KB

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