freedombone-app-koel 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # koel application
  12. # https://gist.github.com/bplower/613a99156d603abac083
  13. #
  14. # License
  15. # =======
  16. #
  17. # Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
  18. #
  19. # This program is free software: you can redistribute it and/or modify
  20. # it under the terms of the GNU Affero General Public License as published by
  21. # the Free Software Foundation, either version 3 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU Affero General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU Affero General Public License
  30. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. VARIANTS='full full-vim media'
  32. IN_DEFAULT_INSTALL=0
  33. SHOW_ON_ABOUT=1
  34. KOEL_DOMAIN_NAME=
  35. KOEL_CODE=
  36. KOEL_ONION_PORT=8118
  37. KOEL_PORT=9002
  38. KOEL_REPO="https://github.com/phanan/koel"
  39. KOEL_COMMIT='70464a8977b1058f3bd0a4ec77877fe7894d8d84'
  40. KOEL_ADMIN_PASSWORD=
  41. koel_variables=(ONION_ONLY
  42. KOEL_DOMAIN_NAME
  43. KOEL_CODE
  44. DDNS_PROVIDER
  45. MY_EMAIL_ADDRESS
  46. MY_USERNAME)
  47. function koel_remove_gravatar {
  48. cd /var/www/${KOEL_DOMAIN_NAME}/htdocs
  49. sed -i "s|www.gravatar.com|${KOEL_DOMAIN_NAME}|g" node_modules/browser-sync-ui/public/js/app.js.map
  50. sed -i "s|www.gravatar.com|${KOEL_DOMAIN_NAME}|g" public/js/app.*.js
  51. if ! grep -q "//Vue.set(user, 'avatar'" resources/assets/js/stores/user.js; then
  52. sed -i "s|Vue.set(user, 'avatar'|//Vue.set(user, 'avatar'|g" resources/assets/js/stores/user.js
  53. fi
  54. }
  55. function remove_user_koel {
  56. remove_username="$1"
  57. ${PROJECT_NAME}-pass -u $remove_username --rmapp koel
  58. }
  59. function add_user_koel {
  60. new_username="$1"
  61. new_user_password="$2"
  62. ${PROJECT_NAME}-pass -u $new_username -a koel -p "$new_user_password"
  63. echo '0'
  64. }
  65. function install_interactive_koel {
  66. if [ ! $ONION_ONLY ]; then
  67. ONION_ONLY='no'
  68. fi
  69. if [[ $ONION_ONLY != "no" ]]; then
  70. KOEL_DOMAIN_NAME='koel.local'
  71. else
  72. KOEL_DETAILS_COMPLETE=
  73. while [ ! $KOEL_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 $"Koel Configuration" \
  80. --form $"\nPlease enter your Koel details. The background image URL can be left blank.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 16 65 3 \
  81. $"Domain:" 1 1 "$(grep 'KOEL_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  82. $"Code:" 2 1 "$(grep 'KOEL_CODE' temp.cfg | awk -F '=' '{print $2}')" 2 25 33 255 \
  83. 2> $data
  84. else
  85. dialog --backtitle $"Freedombone Configuration" \
  86. --title $"Koel Configuration" \
  87. --form $"\nPlease enter your Koel details. The background image URL can be left blank.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 16 65 3 \
  88. $"Domain:" 1 1 "$(grep 'KOEL_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  89. 2> $data
  90. fi
  91. sel=$?
  92. case $sel in
  93. 1) exit 1;;
  94. 255) exit 1;;
  95. esac
  96. KOEL_DOMAIN_NAME=$(cat $data | sed -n 1p)
  97. if [ $KOEL_DOMAIN_NAME ]; then
  98. if [[ $KOEL_DOMAIN_NAME == "$HUBZILLA_DOMAIN_NAME" ]]; then
  99. KOEL_DOMAIN_NAME=""
  100. fi
  101. TEST_DOMAIN_NAME=$KOEL_DOMAIN_NAME
  102. validate_domain_name
  103. if [[ $TEST_DOMAIN_NAME != $KOEL_DOMAIN_NAME ]]; then
  104. KOEL_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. KOEL_CODE=$(cat $data | sed -n 2p)
  109. validate_freedns_code "$KOEL_CODE"
  110. if [ ! $VALID_CODE ]; then
  111. KOEL_DOMAIN_NAME=
  112. fi
  113. fi
  114. fi
  115. fi
  116. if [ $KOEL_DOMAIN_NAME ]; then
  117. KOEL_DETAILS_COMPLETE="yes"
  118. fi
  119. done
  120. # save the results in the config file
  121. write_config_param "KOEL_CODE" "$KOEL_CODE"
  122. fi
  123. write_config_param "KOEL_DOMAIN_NAME" "$KOEL_DOMAIN_NAME"
  124. APP_INSTALLED=1
  125. }
  126. function change_password_koel {
  127. curr_username="$1"
  128. new_user_password="$2"
  129. read_config_param 'KOEL_DOMAIN_NAME'
  130. ${PROJECT_NAME}-pass -u "$curr_username" -a koel -p "$new_user_password"
  131. }
  132. function koel_create_database {
  133. if [ -f $IMAGE_PASSWORD_FILE ]; then
  134. KOEL_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  135. else
  136. if [ ! $KOEL_ADMIN_PASSWORD ]; then
  137. KOEL_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  138. fi
  139. fi
  140. if [ ! $KOEL_ADMIN_PASSWORD ]; then
  141. return
  142. fi
  143. function_check create_database
  144. create_database koel "$KOEL_ADMIN_PASSWORD" $MY_USERNAME
  145. }
  146. function reconfigure_koel {
  147. echo -n ''
  148. }
  149. function koel_import_from_directory {
  150. data=$(tempfile 2>/dev/null)
  151. dialog --title "test" --dselect /home/$MY_USERNAME/ 30 60 2> $data
  152. selected_dir=$(cat $data)
  153. rm $data
  154. if [[ "$selected_dir" == '/music' ]]; then
  155. return
  156. fi
  157. if [ ! -d $selected_dir ]; then
  158. return
  159. fi
  160. if [[ "$selected_dir" == "/home/$MY_USERNAME/" ]]; then
  161. return
  162. fi
  163. if [[ "$selected_dir" == "/home/$MY_USERNAME/."* ]]; then
  164. return
  165. fi
  166. if [[ "$selected_dir" == *"/Maildir" || "$selected_dir" == *"/Sync" ]]; then
  167. return
  168. fi
  169. dialog --title $"Import music directory" \
  170. --backtitle $"Freedombone Control Panel" \
  171. --defaultno \
  172. --yesno $"\nImport the directory:\n\n $selected_dir" 12 75
  173. sel=$?
  174. case $sel in
  175. 1) return;;
  176. 255) return;;
  177. esac
  178. mv "$selected_dir" /music
  179. dialog --title $"Import music directory" \
  180. --msgbox $"Import success. You may need to re-sync within Koel." 6 40
  181. }
  182. function koel_import_from_usb {
  183. clear
  184. detect_usb_drive
  185. if [ ! -b $USB_DRIVE ]; then
  186. dialog --title $"Import music from USB drive" --msgbox $'No USB drive found' 6 50
  187. return
  188. fi
  189. backup_mount_drive ${USB_DRIVE}
  190. if [ ! -d $USB_MOUNT/Music ]; then
  191. dialog --title $"Import music from USB drive" --msgbox $'No Music directory found on USB drive' 6 50
  192. backup_unmount_drive ${USB_DRIVE}
  193. fi
  194. cp -ru $USB_MOUNT/Music/* /music
  195. backup_unmount_drive ${USB_DRIVE}
  196. dialog --title $"Import music from USB drive" --msgbox $'Import complete. You may now remove the USB drive' 6 50
  197. }
  198. function koel_export_to_usb {
  199. clear
  200. detect_usb_drive
  201. if [ ! -b $USB_DRIVE ]; then
  202. dialog --title $"Export music to USB drive" --msgbox $'No USB drive found' 6 50
  203. return
  204. fi
  205. backup_mount_drive ${USB_DRIVE}
  206. if [ ! -d $USB_MOUNT/Music ]; then
  207. mkdir -p $USB_MOUNT/Music
  208. fi
  209. cp -ru /music/* $USB_MOUNT/Music
  210. backup_unmount_drive ${USB_DRIVE}
  211. dialog --title $"Export music to USB drive" --msgbox $'Export complete. You may now remove the USB drive' 6 50
  212. }
  213. function format_music_drive {
  214. detect_usb_drive
  215. data=$(tempfile 2>/dev/null)
  216. trap "rm -f $data" 0 1 2 5 15
  217. dialog --title $"Format USB drive $USB_DRIVE for music storage" \
  218. --backtitle $"Freedombone Control Panel" \
  219. --defaultno \
  220. --yesno $"\nPlease confirm that you wish to format drive\n\n ${USB_DRIVE}\n\nAll current data on the drive will be lost, and you will be prompted to give a password used to encrypt the drive.\n\nDANGER: If you screw up here and format the wrong drive it's your own fault!" 16 60
  221. sel=$?
  222. case $sel in
  223. 1) return;;
  224. 255) return;;
  225. esac
  226. clear
  227. echo ''
  228. echo $"Formatting drive $USB_DRIVE. ALL CONTENTS WILL BE LOST."
  229. echo ''
  230. ${PROJECT_NAME}-format $USB_DRIVE
  231. dialog --title $"Format USB drive $USB_DRIVE for music storage" --msgbox $'Format complete. You may now export music or remove the USB drive' 6 50
  232. }
  233. function configure_interactive_koel {
  234. while true
  235. do
  236. data=$(tempfile 2>/dev/null)
  237. trap "rm -f $data" 0 1 2 5 15
  238. dialog --backtitle $"Freedombone Control Panel" \
  239. --title $"Koel" \
  240. --radiolist $"Choose an operation:" 12 70 5 \
  241. 1 $"Import music from directory" off \
  242. 2 $"Import music from USB drive" off \
  243. 3 $"Export music to USB drive" off \
  244. 4 $"Format a USB drive for music storage" off \
  245. 5 $"Exit" on 2> $data
  246. sel=$?
  247. case $sel in
  248. 1) break;;
  249. 255) break;;
  250. esac
  251. case $(cat $data) in
  252. 1) koel_import_from_directory;;
  253. 2) koel_import_from_usb;;
  254. 3) koel_export_to_usb;;
  255. 4) format_music_drive;;
  256. 5) break;;
  257. esac
  258. done
  259. }
  260. function upgrade_koel {
  261. if grep -q "koel domain" $COMPLETION_FILE; then
  262. KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
  263. fi
  264. CURR_KOEL_COMMIT=$(get_completion_param "koel commit")
  265. if [[ "$CURR_KOEL_COMMIT" == "$KOEL_COMMIT" ]]; then
  266. return
  267. fi
  268. systemctl stop koel
  269. # update to the next commit
  270. function_check set_repo_commit
  271. set_repo_commit /var/www/$KOEL_DOMAIN_NAME/htdocs "koel commit" "$KOEL_COMMIT" $KOEL_REPO
  272. cd /var/www/${KOEL_DOMAIN_NAME}/htdocs
  273. php artisan koel:init
  274. koel_remove_gravatar
  275. chown -R www-data:www-data *
  276. systemctl start koel
  277. }
  278. function backup_local_koel {
  279. KOEL_DOMAIN_NAME='koel'
  280. if grep -q "koel domain" $COMPLETION_FILE; then
  281. KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
  282. fi
  283. source_directory=/var/www/${KOEL_DOMAIN_NAME}/htdocs
  284. if [ -d $source_directory ]; then
  285. systemctl stop koal
  286. dest_directory=koel
  287. function_check suspend_site
  288. suspend_site ${KOEL_DOMAIN_NAME}
  289. function_check backup_directory_to_usb
  290. backup_directory_to_usb $source_directory $dest_directory
  291. function_check backup_database_to_usb
  292. backup_database_to_usb koel
  293. function_check restart_site
  294. restart_site
  295. systemctl start koal
  296. fi
  297. }
  298. function restore_local_koel {
  299. if ! grep -q "koel domain" $COMPLETION_FILE; then
  300. return
  301. fi
  302. KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
  303. if [ $KOEL_DOMAIN_NAME ]; then
  304. echo $"Restoring koel"
  305. systemctl stop koal
  306. temp_restore_dir=/root/tempkoel
  307. koel_dir=/var/www/${KOEL_DOMAIN_NAME}/htdocs
  308. function_check koel_create_database
  309. koel_create_database
  310. restore_database koel ${KOEL_DOMAIN_NAME}
  311. if [ -d $temp_restore_dir ]; then
  312. rm -rf $temp_restore_dir
  313. fi
  314. systemctl start koal
  315. fi
  316. }
  317. function backup_remote_koel {
  318. if grep -q "koel domain" $COMPLETION_FILE; then
  319. KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
  320. temp_backup_dir=/var/www/${KOEL_DOMAIN_NAME}/htdocs
  321. if [ -d $temp_backup_dir ]; then
  322. systemctl stop koal
  323. function_check suspend_site
  324. suspend_site ${KOEL_DOMAIN_NAME}
  325. function_check backup_database_to_friend
  326. backup_database_to_friend koel
  327. echo $"Backing up Koel installation"
  328. function_check backup_directory_to_friend
  329. backup_directory_to_friend $temp_backup_dir koel
  330. function_check restart_site
  331. restart_site
  332. systemctl start koal
  333. else
  334. echo $"koel domain specified but not found in ${temp_backup_dir}"
  335. fi
  336. fi
  337. }
  338. function restore_remote_koel {
  339. if grep -q "koel domain" $COMPLETION_FILE; then
  340. echo $"Restoring koel"
  341. systemctl stop koal
  342. KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
  343. function_check koel_create_database
  344. koel_create_database
  345. function_check restore_database_from_friend
  346. restore_database_from_friend koel ${KOEL_DOMAIN_NAME}
  347. if [ -d /root/tempkoel ]; then
  348. rm -rf /root/tempkoel
  349. fi
  350. systemctl start koal
  351. echo $"Restore of koel complete"
  352. fi
  353. }
  354. function remove_koel {
  355. if [ ${#KOEL_DOMAIN_NAME} -eq 0 ]; then
  356. return
  357. fi
  358. systemctl stop koel
  359. systemctl disable koel
  360. if [ -f /etc/systemd/system/koel.service ]; then
  361. rm /etc/systemd/system/koel.service
  362. fi
  363. function_check remove_nodejs
  364. remove_nodejs koel
  365. read_config_param "KOEL_DOMAIN_NAME"
  366. read_config_param "MY_USERNAME"
  367. echo "Removing $KOEL_DOMAIN_NAME"
  368. nginx_dissite $KOEL_DOMAIN_NAME
  369. remove_certs $KOEL_DOMAIN_NAME
  370. if [ -d /var/www/$KOEL_DOMAIN_NAME ]; then
  371. rm -rf /var/www/$KOEL_DOMAIN_NAME
  372. fi
  373. if [ -f /etc/nginx/sites-available/$KOEL_DOMAIN_NAME ]; then
  374. rm /etc/nginx/sites-available/$KOEL_DOMAIN_NAME
  375. fi
  376. function_check drop_database
  377. drop_database koel
  378. function_check remove_onion_service
  379. remove_onion_service koel ${KOEL_ONION_PORT}
  380. if grep -q "koel" /etc/crontab; then
  381. sed -i "/koel/d" /etc/crontab
  382. fi
  383. remove_app koel
  384. remove_completion_param install_koel
  385. sed -i '/koel/d' $COMPLETION_FILE
  386. remove_backup_database_local koel
  387. sed -i '/koel-firewall/d' /etc/crontab
  388. function_check remove_ddns_domain
  389. remove_ddns_domain $KOEL_DOMAIN_NAME
  390. }
  391. function install_koel_main {
  392. if [ ! $KOEL_DOMAIN_NAME ]; then
  393. echo $'No domain name was given for koel'
  394. exit 7359
  395. fi
  396. if [[ $(app_is_installed koel_main) == "1" ]]; then
  397. return
  398. fi
  399. function_check install_mariadb
  400. install_mariadb
  401. function_check get_mariadb_password
  402. get_mariadb_password
  403. function_check repair_databases_script
  404. repair_databases_script
  405. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
  406. apt-get -yq install php-memcached php-intl exiftool
  407. if [ ! -d /var/www/$KOEL_DOMAIN_NAME ]; then
  408. mkdir /var/www/$KOEL_DOMAIN_NAME
  409. fi
  410. if [ ! -d /var/www/$KOEL_DOMAIN_NAME/htdocs ]; then
  411. function_check git_clone
  412. git_clone $KOEL_REPO /var/www/$KOEL_DOMAIN_NAME/htdocs
  413. if [ ! -d /var/www/$KOEL_DOMAIN_NAME/htdocs ]; then
  414. echo $'Unable to clone koel repo'
  415. exit 365735
  416. fi
  417. fi
  418. cd /var/www/$KOEL_DOMAIN_NAME/htdocs
  419. git checkout $KOEL_COMMIT -b $KOEL_COMMIT
  420. set_completion_param "koel commit" "$KOEL_COMMIT"
  421. chown -R www-data:www-data /var/www/$KOEL_DOMAIN_NAME/htdocs
  422. function_check koel_create_database
  423. koel_create_database
  424. function_check add_ddns_domain
  425. add_ddns_domain $KOEL_DOMAIN_NAME
  426. koel_nginx_site=/etc/nginx/sites-available/$KOEL_DOMAIN_NAME
  427. if [[ $ONION_ONLY == "no" ]]; then
  428. function_check nginx_http_redirect
  429. nginx_http_redirect $KOEL_DOMAIN_NAME "index index.php"
  430. echo 'server {' >> $koel_nginx_site
  431. echo ' listen 443 ssl;' >> $koel_nginx_site
  432. echo ' listen [::]:443 ssl;' >> $koel_nginx_site
  433. echo " server_name $KOEL_DOMAIN_NAME;" >> $koel_nginx_site
  434. echo '' >> $koel_nginx_site
  435. function_check nginx_compress
  436. nginx_compress $KOEL_DOMAIN_NAME
  437. echo ' gzip_comp_level 9;' >> $koel_nginx_site
  438. echo '' >> $koel_nginx_site
  439. echo ' # Security' >> $koel_nginx_site
  440. function_check nginx_ssl mobile
  441. nginx_ssl $KOEL_DOMAIN_NAME
  442. function_check nginx_disable_sniffing
  443. nginx_disable_sniffing $KOEL_DOMAIN_NAME
  444. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $koel_nginx_site
  445. echo '' >> $koel_nginx_site
  446. echo ' # Logs' >> $koel_nginx_site
  447. echo ' access_log /dev/null;' >> $koel_nginx_site
  448. echo ' error_log /dev/null;' >> $koel_nginx_site
  449. echo '' >> $koel_nginx_site
  450. echo ' # Root' >> $koel_nginx_site
  451. echo " root /var/www/$KOEL_DOMAIN_NAME/htdocs;" >> $koel_nginx_site
  452. echo '' >> $koel_nginx_site
  453. echo ' # Index' >> $koel_nginx_site
  454. echo ' index index.php;' >> $koel_nginx_site
  455. echo '' >> $koel_nginx_site
  456. echo ' # Whitelist only index.php, robots.txt, and those start with public/ or api/' >> $koel_nginx_site
  457. echo ' if ($request_uri !~ ^/$|index\.php|robots\.txt|api/|public/) {' >> $koel_nginx_site
  458. echo ' return 404;' >> $koel_nginx_site
  459. echo ' }' >> $koel_nginx_site
  460. echo '' >> $koel_nginx_site
  461. echo ' location /media/ {' >> $koel_nginx_site
  462. echo ' internal;' >> $koel_nginx_site
  463. echo '' >> $koel_nginx_site
  464. echo ' # A X-Media-Root should be set to media_path settings from upstream' >> $koel_nginx_site
  465. echo ' alias $upstream_http_x_media_root;' >> $koel_nginx_site
  466. echo '' >> $koel_nginx_site
  467. echo ' }' >> $koel_nginx_site
  468. echo '' >> $koel_nginx_site
  469. echo ' # PHP' >> $koel_nginx_site
  470. echo ' location ~ \.php {' >> $koel_nginx_site
  471. echo ' include snippets/fastcgi-php.conf;' >> $koel_nginx_site
  472. echo ' fastcgi_pass unix:/var/run/php-fpm.sock;' >> $koel_nginx_site
  473. echo ' }' >> $koel_nginx_site
  474. echo '' >> $koel_nginx_site
  475. echo ' # Location' >> $koel_nginx_site
  476. echo ' location / {' >> $koel_nginx_site
  477. function_check nginx_limits
  478. nginx_limits $KOEL_DOMAIN_NAME '15m'
  479. echo ' try_files $uri $uri/ @koel;' >> $koel_nginx_site
  480. echo ' }' >> $koel_nginx_site
  481. echo '' >> $koel_nginx_site
  482. echo ' # Fancy URLs' >> $koel_nginx_site
  483. echo ' location @koel {' >> $koel_nginx_site
  484. echo ' rewrite ^(.*)$ /index.php?p=$1 last;' >> $koel_nginx_site
  485. echo ' }' >> $koel_nginx_site
  486. echo '' >> $koel_nginx_site
  487. echo ' # Restrict access that is unnecessary anyway' >> $koel_nginx_site
  488. echo ' location ~ /\.(ht|git) {' >> $koel_nginx_site
  489. echo ' deny all;' >> $koel_nginx_site
  490. echo ' }' >> $koel_nginx_site
  491. echo '' >> $koel_nginx_site
  492. echo '}' >> $koel_nginx_site
  493. else
  494. echo -n '' > $koel_nginx_site
  495. fi
  496. echo 'server {' >> $koel_nginx_site
  497. echo " listen 127.0.0.1:$KOEL_ONION_PORT default_server;" >> $koel_nginx_site
  498. echo " server_name $KOEL_DOMAIN_NAME;" >> $koel_nginx_site
  499. echo '' >> $koel_nginx_site
  500. function_check nginx_compress
  501. nginx_compress $KOEL_DOMAIN_NAME
  502. echo ' gzip_comp_level 9;' >> $koel_nginx_site
  503. echo '' >> $koel_nginx_site
  504. echo ' # Logs' >> $koel_nginx_site
  505. echo ' access_log /dev/null;' >> $koel_nginx_site
  506. echo ' error_log /dev/null;' >> $koel_nginx_site
  507. echo '' >> $koel_nginx_site
  508. echo ' # Root' >> $koel_nginx_site
  509. echo " root /var/www/$KOEL_DOMAIN_NAME/htdocs;" >> $koel_nginx_site
  510. echo '' >> $koel_nginx_site
  511. echo ' # Index' >> $koel_nginx_site
  512. echo ' index index.php;' >> $koel_nginx_site
  513. echo '' >> $koel_nginx_site
  514. echo ' # Whitelist only index.php, robots.txt, and those start with public/ or api/' >> $koel_nginx_site
  515. echo ' if ($request_uri !~ ^/$|index\.php|robots\.txt|api/|public/) {' >> $koel_nginx_site
  516. echo ' return 404;' >> $koel_nginx_site
  517. echo ' }' >> $koel_nginx_site
  518. echo '' >> $koel_nginx_site
  519. echo ' location /media/ {' >> $koel_nginx_site
  520. echo ' internal;' >> $koel_nginx_site
  521. echo '' >> $koel_nginx_site
  522. echo ' # A X-Media-Root should be set to media_path settings from upstream' >> $koel_nginx_site
  523. echo ' alias $upstream_http_x_media_root;' >> $koel_nginx_site
  524. echo '' >> $koel_nginx_site
  525. echo ' }' >> $koel_nginx_site
  526. echo '' >> $koel_nginx_site
  527. echo ' # PHP' >> $koel_nginx_site
  528. echo ' location ~ \.php {' >> $koel_nginx_site
  529. echo ' include snippets/fastcgi-php.conf;' >> $koel_nginx_site
  530. echo ' fastcgi_pass unix:/var/run/php-fpm.sock;' >> $koel_nginx_site
  531. echo ' }' >> $koel_nginx_site
  532. echo '' >> $koel_nginx_site
  533. echo ' # Location' >> $koel_nginx_site
  534. echo ' location / {' >> $koel_nginx_site
  535. function_check nginx_limits
  536. nginx_limits $KOEL_DOMAIN_NAME '15m'
  537. echo ' try_files $uri $uri/ @koel;' >> $koel_nginx_site
  538. echo ' }' >> $koel_nginx_site
  539. echo '' >> $koel_nginx_site
  540. echo ' # Fancy URLs' >> $koel_nginx_site
  541. echo ' location @koel {' >> $koel_nginx_site
  542. echo ' rewrite ^(.*)$ /index.php?p=$1 last;' >> $koel_nginx_site
  543. echo ' }' >> $koel_nginx_site
  544. echo '' >> $koel_nginx_site
  545. echo ' # Restrict access that is unnecessary anyway' >> $koel_nginx_site
  546. echo ' location ~ /\.(ht|git) {' >> $koel_nginx_site
  547. echo ' deny all;' >> $koel_nginx_site
  548. echo ' }' >> $koel_nginx_site
  549. echo '' >> $koel_nginx_site
  550. echo '}' >> $koel_nginx_site
  551. sed -i 's|gzip_types.*|gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;|g' $koel_nginx_site
  552. sed -i 's|DENY;|SAMEORIGIN;|g' $koel_nginx_site
  553. function_check configure_php
  554. configure_php
  555. function_check create_site_certificate
  556. create_site_certificate $KOEL_DOMAIN_NAME 'yes'
  557. # Ensure that the database gets backed up locally, if remote
  558. # backups are not being used
  559. function_check backup_databases_script_header
  560. backup_databases_script_header
  561. function_check backup_database_local
  562. backup_database_local koel
  563. function_check nginx_ensite
  564. nginx_ensite $KOEL_DOMAIN_NAME
  565. KOEL_ONION_HOSTNAME=$(add_onion_service koel 80 ${KOEL_ONION_PORT})
  566. KOEL_SERVER=${KOEL_DOMAIN_NAME}
  567. if [[ $ONION_ONLY != 'no' ]]; then
  568. KOEL_SERVER=${KOEL_ONION_HOSTNAME}
  569. fi
  570. systemctl restart php7.0-fpm
  571. systemctl restart nginx
  572. ${PROJECT_NAME}-pass -u $MY_USERNAME -a koel -p "$KOEL_ADMIN_PASSWORD"
  573. set_completion_param "koel domain" "$KOEL_DOMAIN_NAME"
  574. install_completed koel_main
  575. }
  576. function install_koel {
  577. if [ ! $ONION_ONLY ]; then
  578. ONION_ONLY='no'
  579. fi
  580. function_check install_nodejs
  581. install_nodejs koel
  582. install_koel_main
  583. cd /var/www/$KOEL_DOMAIN_NAME/htdocs
  584. # curl -sS https://getcomposer.org/installer | php
  585. if [ -f ~/freedombone/image_build/composer_install ]; then
  586. cat ~/freedombone/image_build/composer_install | php
  587. else
  588. if [ -f /home/$MY_USERNAME/freedombone/image_build/composer_install ]; then
  589. cat /home/$MY_USERNAME/freedombone/image_build/composer_install | php
  590. fi
  591. fi
  592. npm install
  593. php composer.phar install
  594. function_check get_mariadb_password
  595. get_mariadb_password
  596. cp .env.example .env
  597. sed -i "s/ADMIN_EMAIL=.*/ADMIN_EMAIL=$MY_EMAIL_ADDRESS/g" .env
  598. sed -i "s/ADMIN_NAME=.*/ADMIN_NAME=$MY_USERNAME/g" .env
  599. sed -i "s/ADMIN_PASSWORD=.*/ADMIN_PASSWORD=$KOEL_ADMIN_PASSWORD/g" .env
  600. sed -i 's/DB_CONNECTION=.*/DB_CONNECTION=mysql/g' .env
  601. sed -i 's/DB_HOST=.*/DB_HOST=127.0.0.1/g' .env
  602. sed -i 's/DB_DATABASE=.*/DB_DATABASE=koel/g' .env
  603. sed -i 's/DB_USERNAME=.*/DB_USERNAME=root/g' .env
  604. sed -i "s|DB_PASSWORD=.*|DB_PASSWORD=$MARIADB_PASSWORD|g" .env
  605. sed -i 's/MAIL_HOST=.*/MAIL_HOST=localhost/g' .env
  606. sed -i 's/MAIL_PORT=.*/MAIL_PORT=25/g' .env
  607. php artisan koel:init
  608. koel_remove_gravatar
  609. chown -R www-data:www-data /var/www/$KOEL_DOMAIN_NAME/htdocs
  610. # daemon
  611. echo '[Unit]' > /etc/systemd/system/koel.service
  612. echo 'Description=Koel (music player)' >> /etc/systemd/system/koel.service
  613. echo 'After=syslog.target' >> /etc/systemd/system/koel.service
  614. echo 'After=network.target' >> /etc/systemd/system/koel.service
  615. echo '' >> /etc/systemd/system/koel.service
  616. echo '[Service]' >> /etc/systemd/system/koel.service
  617. echo 'Type=simple' >> /etc/systemd/system/koel.service
  618. echo 'User=www-data' >> /etc/systemd/system/koel.service
  619. echo 'Group=www-data' >> /etc/systemd/system/koel.service
  620. echo "WorkingDirectory=/var/www/$KOEL_DOMAIN_NAME/htdocs" >> /etc/systemd/system/koel.service
  621. echo "ExecStart=/usr/bin/php artisan serve --port=$KOEL_PORT" >> /etc/systemd/system/koel.service
  622. echo 'Restart=on-failure' >> /etc/systemd/system/koel.service
  623. echo '' >> /etc/systemd/system/koel.service
  624. echo '[Install]' >> /etc/systemd/system/koel.service
  625. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/koel.service
  626. systemctl enable koel.service
  627. systemctl daemon-reload
  628. systemctl start koel.service
  629. if [ ! -d /music ]; then
  630. mkdir /music
  631. fi
  632. chown -R www-data:www-data /music
  633. systemctl restart nginx
  634. APP_INSTALLED=1
  635. }
  636. # NOTE: deliberately there is no "exit 0"