freedombone-app-koel 24KB

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