freedombone-app-koel 24KB

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