freedombone-app-koel 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. W=(1 $"Import music from directory"
  242. 2 $"Import music from USB drive"
  243. 3 $"Export music to USB drive"
  244. 4 $"Format a USB drive for music storage")
  245. while true
  246. do
  247. # shellcheck disable=SC2068
  248. 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)
  249. if [ ! "$selection" ]; then
  250. break
  251. fi
  252. case $selection in
  253. 1) koel_import_from_directory;;
  254. 2) koel_import_from_usb;;
  255. 3) koel_export_to_usb;;
  256. 4) format_music_drive;;
  257. esac
  258. done
  259. }
  260. function upgrade_koel {
  261. if grep -q "koel domain" "$COMPLETION_FILE"; then
  262. KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
  263. fi
  264. CURR_KOEL_COMMIT=$(get_completion_param "koel commit")
  265. if [[ "$CURR_KOEL_COMMIT" == "$KOEL_COMMIT" ]]; then
  266. return
  267. fi
  268. systemctl stop koel
  269. # update to the next commit
  270. function_check set_repo_commit
  271. set_repo_commit "/var/www/$KOEL_DOMAIN_NAME/htdocs" "koel commit" "$KOEL_COMMIT" $KOEL_REPO
  272. cd "/var/www/${KOEL_DOMAIN_NAME}/htdocs" || exit 7345346358
  273. php artisan koel:init
  274. koel_remove_gravatar
  275. chown -R www-data:www-data "/var/www/${KOEL_DOMAIN_NAME}/htdocs/"*
  276. systemctl start koel
  277. }
  278. function backup_local_koel {
  279. KOEL_DOMAIN_NAME='koel'
  280. if grep -q "koel domain" "$COMPLETION_FILE"; then
  281. KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
  282. fi
  283. source_directory="/var/www/${KOEL_DOMAIN_NAME}/htdocs"
  284. if [ -d "$source_directory" ]; then
  285. systemctl stop koel
  286. dest_directory=koel
  287. function_check suspend_site
  288. suspend_site "${KOEL_DOMAIN_NAME}"
  289. function_check backup_directory_to_usb
  290. backup_directory_to_usb "$source_directory" "$dest_directory"
  291. function_check backup_database_to_usb
  292. backup_database_to_usb koel
  293. function_check restart_site
  294. restart_site
  295. systemctl start koel
  296. fi
  297. }
  298. function restore_local_koel {
  299. if ! grep -q "koel domain" "$COMPLETION_FILE"; then
  300. return
  301. fi
  302. KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
  303. if [ "$KOEL_DOMAIN_NAME" ]; then
  304. echo $"Restoring koel"
  305. systemctl stop koel
  306. temp_restore_dir=/root/tempkoel
  307. function_check koel_create_database
  308. koel_create_database
  309. restore_database koel "${KOEL_DOMAIN_NAME}"
  310. if [ -d $temp_restore_dir ]; then
  311. rm -rf $temp_restore_dir
  312. fi
  313. MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  314. cd "/var/www/$KOEL_DOMAIN_NAME/htdocs" || exit 274825424
  315. sed -i "s|DB_PASSWORD=.*|DB_PASSWORD=$MARIADB_PASSWORD|g" .env
  316. MARIADB_PASSWORD=
  317. systemctl start koel
  318. fi
  319. }
  320. function backup_remote_koel {
  321. if grep -q "koel domain" "$COMPLETION_FILE"; then
  322. KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
  323. temp_backup_dir=/var/www/${KOEL_DOMAIN_NAME}/htdocs
  324. if [ -d "$temp_backup_dir" ]; then
  325. systemctl stop koel
  326. function_check suspend_site
  327. suspend_site "${KOEL_DOMAIN_NAME}"
  328. function_check backup_database_to_friend
  329. backup_database_to_friend koel
  330. echo $"Backing up Koel installation"
  331. function_check backup_directory_to_friend
  332. backup_directory_to_friend "$temp_backup_dir" koel
  333. function_check restart_site
  334. restart_site
  335. systemctl start koel
  336. else
  337. echo $"koel domain specified but not found in ${temp_backup_dir}"
  338. fi
  339. fi
  340. }
  341. function restore_remote_koel {
  342. if grep -q "koel domain" "$COMPLETION_FILE"; then
  343. echo $"Restoring koel"
  344. systemctl stop koel
  345. KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
  346. function_check koel_create_database
  347. koel_create_database
  348. function_check restore_database_from_friend
  349. restore_database_from_friend koel "${KOEL_DOMAIN_NAME}"
  350. if [ -d /root/tempkoel ]; then
  351. rm -rf /root/tempkoel
  352. fi
  353. MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  354. cd "/var/www/$KOEL_DOMAIN_NAME/htdocs" || exit 53743682
  355. sed -i "s|DB_PASSWORD=.*|DB_PASSWORD=$MARIADB_PASSWORD|g" .env
  356. MARIADB_PASSWORD=
  357. systemctl start koel
  358. echo $"Restore of koel complete"
  359. fi
  360. }
  361. function remove_koel {
  362. if [ ${#KOEL_DOMAIN_NAME} -eq 0 ]; then
  363. return
  364. fi
  365. systemctl stop koel
  366. systemctl disable koel
  367. if [ -f /etc/systemd/system/koel.service ]; then
  368. rm /etc/systemd/system/koel.service
  369. fi
  370. systemctl daemon-reload
  371. function_check remove_nodejs
  372. remove_nodejs koel
  373. read_config_param "KOEL_DOMAIN_NAME"
  374. read_config_param "MY_USERNAME"
  375. echo "Removing $KOEL_DOMAIN_NAME"
  376. nginx_dissite "$KOEL_DOMAIN_NAME"
  377. remove_certs "$KOEL_DOMAIN_NAME"
  378. if [ -d "/var/www/$KOEL_DOMAIN_NAME" ]; then
  379. rm -rf "/var/www/$KOEL_DOMAIN_NAME"
  380. fi
  381. if [ -f "/etc/nginx/sites-available/$KOEL_DOMAIN_NAME" ]; then
  382. rm "/etc/nginx/sites-available/$KOEL_DOMAIN_NAME"
  383. fi
  384. function_check drop_database
  385. drop_database koel
  386. function_check remove_onion_service
  387. remove_onion_service koel ${KOEL_ONION_PORT}
  388. remove_app koel
  389. remove_completion_param install_koel
  390. sed -i '/koel/d' "$COMPLETION_FILE"
  391. remove_backup_database_local koel
  392. function_check remove_ddns_domain
  393. remove_ddns_domain "$KOEL_DOMAIN_NAME"
  394. }
  395. function install_koel_main {
  396. if [ ! "$KOEL_DOMAIN_NAME" ]; then
  397. echo $'No domain name was given for koel'
  398. exit 7359
  399. fi
  400. if [[ $(app_is_installed koel_main) == "1" ]]; then
  401. return
  402. fi
  403. function_check install_mariadb
  404. install_mariadb
  405. function_check get_mariadb_password
  406. get_mariadb_password
  407. function_check repair_databases_script
  408. repair_databases_script
  409. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl php-zip
  410. apt-get -yq install php-memcached php-intl exiftool libfcgi0ldbl
  411. apt-get -yq install ffmpeg
  412. if [ ! -d "/var/www/$KOEL_DOMAIN_NAME" ]; then
  413. mkdir "/var/www/$KOEL_DOMAIN_NAME"
  414. fi
  415. if [ ! -d "/var/www/$KOEL_DOMAIN_NAME/htdocs" ]; then
  416. if [ -d /repos/koel ]; then
  417. mkdir "/var/www/$KOEL_DOMAIN_NAME/htdocs"
  418. cp -r -p /repos/koel/. "/var/www/$KOEL_DOMAIN_NAME/htdocs"
  419. cd "/var/www/$KOEL_DOMAIN_NAME/htdocs" || exit 23924295
  420. git pull
  421. else
  422. function_check git_clone
  423. git_clone "$KOEL_REPO" "/var/www/$KOEL_DOMAIN_NAME/htdocs"
  424. fi
  425. if [ ! -d "/var/www/$KOEL_DOMAIN_NAME/htdocs" ]; then
  426. echo $'Unable to clone koel repo'
  427. exit 365735
  428. fi
  429. fi
  430. cd "/var/www/$KOEL_DOMAIN_NAME/htdocs" || exit 834567242
  431. git checkout "$KOEL_COMMIT" -b "$KOEL_COMMIT"
  432. set_completion_param "koel commit" "$KOEL_COMMIT"
  433. chown -R www-data:www-data "/var/www/$KOEL_DOMAIN_NAME/htdocs"
  434. function_check koel_create_database
  435. koel_create_database
  436. function_check add_ddns_domain
  437. add_ddns_domain "$KOEL_DOMAIN_NAME"
  438. KOEL_ONION_HOSTNAME=$(add_onion_service koel 80 ${KOEL_ONION_PORT})
  439. koel_nginx_site=/etc/nginx/sites-available/$KOEL_DOMAIN_NAME
  440. if [[ $ONION_ONLY == "no" ]]; then
  441. function_check nginx_http_redirect
  442. nginx_http_redirect "$KOEL_DOMAIN_NAME" "index index.php"
  443. { echo 'server {';
  444. echo ' listen 443 ssl;';
  445. echo ' #listen [::]:443 ssl;';
  446. echo " server_name $KOEL_DOMAIN_NAME;";
  447. echo ''; } >> "$koel_nginx_site"
  448. function_check nginx_compress
  449. nginx_compress "$KOEL_DOMAIN_NAME"
  450. { echo ' gzip_comp_level 9;';
  451. echo '';
  452. echo ' # Security'; } >> "$koel_nginx_site"
  453. function_check nginx_ssl mobile
  454. nginx_ssl "$KOEL_DOMAIN_NAME"
  455. function_check nginx_security_options
  456. nginx_security_options "$KOEL_DOMAIN_NAME"
  457. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  458. echo '';
  459. echo ' # Logs';
  460. echo ' access_log /dev/null;';
  461. echo ' error_log /dev/null;';
  462. echo '';
  463. echo ' # Root';
  464. echo " root /var/www/$KOEL_DOMAIN_NAME/htdocs;";
  465. echo '';
  466. echo ' # Index';
  467. echo ' index index.php;';
  468. echo '';
  469. echo ' # Whitelist only index.php, robots.txt, and those start with public/ or api/';
  470. echo " if (\$request_uri !~ ^/\$|index\\.php|robots\\.txt|api/|public/) {";
  471. echo ' return 404;';
  472. echo ' }';
  473. echo '';
  474. echo ' location /media/ {';
  475. echo ' internal;';
  476. echo '';
  477. echo ' # A X-Media-Root should be set to media_path settings from upstream';
  478. echo " alias \$upstream_http_x_media_root;";
  479. echo '';
  480. echo ' }';
  481. echo '';
  482. echo ' # PHP';
  483. echo ' location ~ \.php {';
  484. echo ' include snippets/fastcgi-php.conf;';
  485. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  486. echo ' fastcgi_read_timeout 30;';
  487. echo ' }';
  488. echo '';
  489. echo ' # Location';
  490. echo ' location / {'; } >> "$koel_nginx_site"
  491. function_check nginx_limits
  492. nginx_limits "$KOEL_DOMAIN_NAME" '15m'
  493. { echo " try_files \$uri \$uri/ @koel;";
  494. echo ' }';
  495. echo '';
  496. echo ' # Fancy URLs';
  497. echo ' location @koel {';
  498. echo " rewrite ^(.*)\$ /index.php?p=\$1 last;";
  499. echo ' }';
  500. echo '';
  501. echo ' # Restrict access that is unnecessary anyway';
  502. echo ' location ~ /\.(ht|git) {';
  503. echo ' deny all;';
  504. echo ' }';
  505. echo '';
  506. echo '}'; } >> "$koel_nginx_site"
  507. else
  508. echo -n '' > "$koel_nginx_site"
  509. fi
  510. if [[ "$ONION_ONLY" == 'no' ]]; then
  511. { echo 'server {';
  512. echo " listen 127.0.0.1:$KOEL_ONION_PORT default_server;";
  513. echo " server_name $KOEL_DOMAIN_NAME;";
  514. echo ''; } >> "$koel_nginx_site"
  515. else
  516. { echo 'server {';
  517. echo " listen 127.0.0.1:$KOEL_ONION_PORT default_server;";
  518. echo " server_name $KOEL_ONION_HOSTNAME;";
  519. echo ''; } >> "$koel_nginx_site"
  520. fi
  521. function_check nginx_compress
  522. nginx_compress "$KOEL_DOMAIN_NAME"
  523. { echo ' gzip_comp_level 9;';
  524. echo '';
  525. echo ' # Logs';
  526. echo ' access_log /dev/null;';
  527. echo ' error_log /dev/null;';
  528. echo '';
  529. echo ' # Root';
  530. echo " root /var/www/$KOEL_DOMAIN_NAME/htdocs;";
  531. echo '';
  532. echo ' # Index';
  533. echo ' index index.php;';
  534. echo '';
  535. echo ' # Whitelist only index.php, robots.txt, and those start with public/ or api/';
  536. echo " if (\$request_uri !~ ^/\$|index\\.php|robots\\.txt|api/|public/) {";
  537. echo ' return 404;';
  538. echo ' }';
  539. echo '';
  540. echo ' location /media/ {';
  541. echo ' internal;';
  542. echo '';
  543. echo ' # A X-Media-Root should be set to media_path settings from upstream';
  544. echo " alias \$upstream_http_x_media_root;";
  545. echo '';
  546. echo ' }';
  547. echo '';
  548. echo ' # PHP';
  549. echo ' location ~ \.php {';
  550. echo ' include snippets/fastcgi-php.conf;';
  551. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  552. echo ' fastcgi_read_timeout 30;';
  553. echo ' }';
  554. echo '';
  555. echo ' # Location';
  556. echo ' location / {'; } >> "$koel_nginx_site"
  557. function_check nginx_limits
  558. nginx_limits "$KOEL_DOMAIN_NAME" '15m'
  559. { echo " try_files \$uri \$uri/ @koel;";
  560. echo ' }';
  561. echo '';
  562. echo ' # Fancy URLs';
  563. echo ' location @koel {';
  564. echo " rewrite ^(.*)\$ /index.php?p=\$1 last;";
  565. echo ' }';
  566. echo '';
  567. echo ' # Restrict access that is unnecessary anyway';
  568. echo ' location ~ /\.(ht|git) {';
  569. echo ' deny all;';
  570. echo ' }';
  571. echo '';
  572. echo '}'; } >> "$koel_nginx_site"
  573. 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"
  574. sed -i 's|DENY;|SAMEORIGIN;|g' "$koel_nginx_site"
  575. function_check configure_php
  576. configure_php
  577. function_check create_site_certificate
  578. create_site_certificate "$KOEL_DOMAIN_NAME" 'yes'
  579. # Ensure that the database gets backed up locally, if remote
  580. # backups are not being used
  581. function_check backup_databases_script_header
  582. backup_databases_script_header
  583. function_check backup_database_local
  584. backup_database_local koel
  585. function_check nginx_ensite
  586. nginx_ensite "$KOEL_DOMAIN_NAME"
  587. systemctl restart mariadb
  588. systemctl restart php7.0-fpm
  589. systemctl restart nginx
  590. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a koel -p "$KOEL_ADMIN_PASSWORD"
  591. set_completion_param "koel domain" "$KOEL_DOMAIN_NAME"
  592. install_completed koel_main
  593. }
  594. function install_koel {
  595. if [ ! $ONION_ONLY ]; then
  596. ONION_ONLY='no'
  597. fi
  598. function_check install_nodejs
  599. install_nodejs koel
  600. install_koel_main
  601. cd "/var/www/$KOEL_DOMAIN_NAME/htdocs" || exit 2432848
  602. install_composer
  603. npm install -g yarn
  604. npm install
  605. function_check get_mariadb_password
  606. get_mariadb_password
  607. cp .env.example .env
  608. sed -i "s/ADMIN_EMAIL=.*/ADMIN_EMAIL=$MY_EMAIL_ADDRESS/g" .env
  609. sed -i "s/ADMIN_NAME=.*/ADMIN_NAME=$MY_USERNAME/g" .env
  610. sed -i "s/ADMIN_PASSWORD=.*/ADMIN_PASSWORD=$KOEL_ADMIN_PASSWORD/g" .env
  611. sed -i 's/DB_CONNECTION=.*/DB_CONNECTION=mysql/g' .env
  612. sed -i 's/DB_HOST=.*/DB_HOST=127.0.0.1/g' .env
  613. sed -i 's/DB_DATABASE=.*/DB_DATABASE=koel/g' .env
  614. sed -i 's/DB_USERNAME=.*/DB_USERNAME=root/g' .env
  615. sed -i "s|DB_PASSWORD=.*|DB_PASSWORD=$MARIADB_PASSWORD|g" .env
  616. sed -i 's/MAIL_HOST=.*/MAIL_HOST=localhost/g' .env
  617. sed -i 's/MAIL_PORT=.*/MAIL_PORT=25/g' .env
  618. sed -i 's|FFMPEG_PATH=.*|FFMPEG_PATH=/usr/bin/ffmpeg|g' .env
  619. if ! php artisan koel:init; then
  620. echo $"Can't install koel:init"
  621. exit 78362
  622. fi
  623. koel_remove_gravatar
  624. chown -R www-data:www-data "/var/www/$KOEL_DOMAIN_NAME/htdocs"
  625. # daemon
  626. { echo '[Unit]';
  627. echo 'Description=Koel (music player)';
  628. echo 'After=syslog.target';
  629. echo 'After=network.target';
  630. echo '';
  631. echo '[Service]';
  632. echo 'Type=simple';
  633. echo 'User=www-data';
  634. echo 'Group=www-data';
  635. echo "WorkingDirectory=/var/www/$KOEL_DOMAIN_NAME/htdocs";
  636. echo "ExecStart=/usr/bin/php artisan serve --port=$KOEL_PORT";
  637. echo 'Restart=on-failure';
  638. echo '';
  639. echo '[Install]';
  640. echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/koel.service
  641. systemctl enable koel.service
  642. systemctl daemon-reload
  643. systemctl start koel.service
  644. if [ ! -d /music ]; then
  645. mkdir /music
  646. fi
  647. chown -R www-data:www-data /music
  648. systemctl restart mariadb
  649. systemctl restart nginx
  650. APP_INSTALLED=1
  651. }
  652. # NOTE: deliberately there is no "exit 0"