freedombone-app-koel 24KB

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