freedombone-app-koel 24KB

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