使用Plex搭建在线音乐库

使用Plex搭建在线音乐库

IT, 其他

使用Plex搭建在线音乐库

我的无损音乐主要存放于家中的 NAS,但是考虑到上行带宽问题,以及不稳定的网络,我决定使用 Plex 与 Rclone 搭建一个在线的曲库。

准备工作

  • Plex Server
  • Rclone
  • GDrive 账号
  • Cloudflare 域名(可选)

安装 Plex Server

通常来说你可以通过 https://www.plex.tv/media-server-downloads/ 下载你所需要的软件包,并安装。如果你需要自动更新,那么建议使用该项目更新 https://github.com/mrworf/plexupdate

bash -c "$(wget -qO - https://raw.githubusercontent.com/mrworf/plexupdate/master/extras/installer.sh)"

只要根据脚本引导进行简单设置即可使用。

安装并配置 Rclone

Rclone 可以简单的通过脚本安装:

curl https://rclone.org/install.sh | sudo bash

安装完成后即配置相应的 GDrive

rclone config

具体的配置方法可以参考https://rclone.org/drive/

配置 Rclone 挂载

我们设置完 Rclone 账号后即可使用 fuse 的方式挂载。

首先选择一个合适的用户/用户组,获取它的 GID、UID

id [username]

新建一个脚本 /etc/systemd/system/rclone-lib.service,注意修改 GID、UID,挂载GDrive名称,目标地址。


[Unit]
Description=Google Drive (rclone)
AssertPathIsDirectory=/home/plex
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/rclone mount GD:PlexMusicLib /home/plex/Lib \
--config=/root/.config/rclone/rclone.conf \
--allow-other \
--buffer-size 512M \
--dir-cache-time 12h \
--drive-chunk-size 64M \
--umask 002 \
--vfs-read-chunk-size 512M \
--vfs-read-chunk-size-limit off \
--user-agent user \
--gid $GID \
--uid $UID
ExecStop=/bin/fusermount -u /home/plex/Lib
Restart=always
RestartSec=10

[Install]
WantedBy=default.target

其中,挂载参数可以根据自己的系统进行设置 https://rclone.org/commands/rclone_mount/

配置 Plex 库

此时你就可以添加一个 Plex 音乐库

这里建议创建多个音乐库分开类型,逐个扫描。Plex 在扫描文件的时候,Rclone 需要拉取所有的文件,因此这样的配置将会比普通硬盘扫描耗费更长的时间。

配置 CDN

Plex 可以配合免费的 Clondflare CDN 使用,这种方法不仅可以保护原站 IP 还可以优化服务器到客户端之间的路由。对于音乐库这样,对延迟不太敏感的应用场景,这将是一个不错的用法。

首先在 Cloudflare 新建一个 A 记录,指向服务器 IP 地址,选择 Proxied (已代理)。如果你想要为服务器签发一个 Let’s Encrypt 证书则需要如下的 Nginx 配置


server {
    listen 80;
    listen [::]:80;
    server_name plex.mysite.net;

    location /.well-known {
      alias /var/www/html/.well-known;
      allow all;
      default_type "text/plain";
      autoindex    on;
    }

    location / {
      return 301 https://$host$request_uri;
    }
}

server {
    listen 443;
    server_name plex.mysite.net;
    rewrite     https://$host$request_uri?  permanent;
    error_log   /var/log/nginx/plex_error.log    error;
    access_log  /var/log/nginx/plex_access.log   combined;
    #ssl on;
    # These are the paths to your generated Let's Encrypt SSL certificates.
    ssl_certificate     /etc/nginx/ssl/plex.mysite.net-fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/plex.mysite.net-privkey.pem;
    # To generate your dhparam.pem file, run "openssl dhparam -out /etc/nginx/dhparam.pem 2048" (without the quotes) in your terminal.
    # ssl_dhparam         /etc/nginx/ssl/dhparam.pem;
    #Resovler set to CloudFlare. Timeout and nameservers may need to be adjusted for your location
    resolver 1.1.1.1 1.0.0.1 valid=300s;
    resolver_timeout 10s;
    #Plex has A LOT of javascript, xml and html. This helps a lot, but if it causes playback issues with devices turn it off. (Haven't encountered any yet)
    gzip on;
    gzip_vary on;
    gzip_min_length 1000;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml application/xml text/javascript application/x-javascript image/svg+xml;
    gzip_disable "MSIE [1-6]\.";
    location / {
        # IP address of Plex Media Server
        proxy_pass          http://127.0.0.1:32400;
        proxy_buffering     off;
        proxy_redirect      off;
        proxy_http_version  1.1;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    Upgrade         $http_upgrade;
        proxy_set_header    Connection      $http_connection;
        proxy_cookie_path   /web/           /;
        access_log          off;
    }
}

否则,直接使用自签证书并写成如下形式


server {
    listen 80;
    listen [::]:80;
    server_name plex.mysite.net;

    location / {
      return 301 https://$host$request_uri;
    }
}

server {
    listen 443;
    server_name plex.mysite.net;
    rewrite     https://$host$request_uri?  permanent;
    error_log   /var/log/nginx/plex_error.log    error;
    access_log  /var/log/nginx/plex_access.log   combined;
    #ssl on;
    # These are the paths to your generated Let's Encrypt SSL certificates.
    ssl_certificate     /etc/nginx/ssl/plex.mysite.net-fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/plex.mysite.net-privkey.pem;
    # To generate your dhparam.pem file, run "openssl dhparam -out /etc/nginx/dhparam.pem 2048" (without the quotes) in your terminal.
    # ssl_dhparam         /etc/nginx/ssl/dhparam.pem;
    #Resovler set to CloudFlare. Timeout and nameservers may need to be adjusted for your location
    resolver 1.1.1.1 1.0.0.1 valid=300s;
    resolver_timeout 10s;
    #Plex has A LOT of javascript, xml and html. This helps a lot, but if it causes playback issues with devices turn it off. (Haven't encountered any yet)
    gzip on;
    gzip_vary on;
    gzip_min_length 1000;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml application/xml text/javascript application/x-javascript image/svg+xml;
    gzip_disable "MSIE [1-6]\.";
    location / {
        # IP address of Plex Media Server
        proxy_pass          http://127.0.0.1:32400;
        proxy_buffering     off;
        proxy_redirect      off;
        proxy_http_version  1.1;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    Upgrade         $http_upgrade;
        proxy_set_header    Connection      $http_connection;
        proxy_cookie_path   /web/           /;
        access_log          off;
    }
}

如果在 Cloudflare 的 SSL/TLS 选项卡中选择“完全(严格)”,则必须要配置正确的 Let’s Encrypt 证书,并且 Cloudflare 的 SNI 证书不支持二级域名。

由于 Cloudflare 对这些经过代理的网站默认启用缓存,容易造成 Plex 库文件更新有问题,因此建议通过页面规则,对该域名的内容启用 bypass 的缓存策略。

最后将 CDN 域名填入 Plex 配置:

在 Plex 的服务器设置中,找到网络选项卡。在 Custom server access URLs 中填入加速域名:

https://plex.mysite.net:443

那么这个配置将通过 app.plex.tv 推送到你全部的客户端实现 CDN 加速。

参考资料

https://github.com/mrworf/plexupdate

https://rclone.org/commands/rclone_mount/

https://quickbox.io/knowledge-base/setting-up-cloudflare-and-plex-cdn/

Amefs, EFS, IT, Linux
上一篇文章
如何生成 HEIC 图片
下一篇文章
钢之炼金术师 FA BDRip 制作的碎碎念

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Fill out this field
Fill out this field
请输入有效的电子邮箱地址。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

keyboard_arrow_up