如何在 Ubuntu 18.04 上安装 Koel 音乐流媒体服务器
Koel 是一个基于 Web 的个人音频流应用程序,在客户端用 Vue.js 编写,在服务器端用 Laravel 编写。本教程将介绍如何在基于 Ubuntu 18.04 的服务器上安装 Koel。
先决条件
-
A server running Ubuntu 18.04.
-
A non-root sudo user.
-
Make sure everything is updated.
$ sudo apt update && sudo apt upgrade
-
Few packages that your system needs.
$ sudo apt install ca-certificates curl unzip build-essential libpng-dev gnupg2 lsb-release ufw -y
Some of these packages may already be installed on your system.
配置防火墙
$ sudo ufw allow OpenSSH
$ sudo ufw enable
$ sudo ufw allow http
$ sudo ufw allow https
$ sudo ufw status
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
安装 Git
$ sudo apt install git -y
$ git config --global user.name "Your Name"
$ git config --global user.email ""
安装 Node.js
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt install -y nodejs
$ node --version
v10.17.0
安装纱线
$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
$ sudo apt update && sudo apt install -y yarn
$ yarn --version
1.19.1
安装PHP
$ sudo apt install -y php7.2-fpm php7.2-mbstring php7.2-bcmath php7.2-xml php7.2-mysql php7.2-curl php7.2-zip
$ php --version
PHP 7.2.24-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct 24 2019 18:29:11) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
安装 MariaDB
$ sudo apt install mariadb-server
$ mysql --version
mysql Ver 15.1 Distrib 10.1.41-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
$ sudo mysql_secure_installation
$ sudo mysql -u root
use mysql;
update user set plugin='' where User='root';
flush privileges;
exit
$ sudo systemctl restart mariadb.service
$ sudo mysql -u root -p
为 Koel 配置 MariaDB
$ sudo mysql -u root
mysql> CREATE DATABASE koelmusic;
mysql> CREATE USER 'koeluser'@'localhost' IDENTIFIED BY 'yourpassword';
mysql> GRANT ALL PRIVILEGES ON koelmusic.* TO 'koeluser'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> exit
安装 Nginx
$ sudo apt install nginx
$ nginx -v
nginx version: nginx/1.14.0 (Ubuntu)
$ sudo nano /etc/nginx/sites-available/koel.conf
server {
listen *:80;
server_name koel.example.com;
root /var/www/koel;
index index.php;
gzip on;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;
gzip_comp_level 9;
# Whitelist only index.php, robots.txt, and some special routes
if ($request_uri !~ ^/$|index\.php|robots\.txt|(public|api)/|remote|api-docs|sw\.js) {
return 404;
}
location /media/ {
internal;
alias $upstream_http_x_media_root;
access_log /var/log/nginx/koel.access.log;
error_log /var/log/nginx/koel.error.log;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
$ sudo ln -s /etc/nginx/sites-available/koel.conf /etc/nginx/sites-enabled/
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo systemctl reload nginx
安装作曲家
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"
$ sudo mv composer.phar /usr/local/bin/composer
$ composer --version
Composer version 1.9.1 2019-11-01 17:20:17
安装 Koel
$ sudo mkdir -p /var/www/koel
$ cd /var/www/koel
$ sudo chown -R user:user /var/www/koel
$ git clone --recurse-submodules https://github.com/phanan/koel.git .
$ git checkout v4.1.1
$ composer install
$ php artisan koel:init
$ sudo chown -R www-data:www-data /var/www/koel
配置Koel
$ sudo nano /var/www/koel/.env
$ php artisan koel:admin:change-password
$ php artisan koel:sync
Koel syncing started.
953/953 [????????????????????????????] 100%
Completed! 944 new or updated song(s), 0 unchanged song(s), and 9 invalid file(s).
$ crontab -e
0 0 * * * cd /var/www/koel && /usr/bin/php artisan koel:sync >/dev/null 2>&1
使用 Lets Encrypt 设置 HTTPS
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt install python-certbot-nginx
$ sudo certbot --nginx -d koel.example.com
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):