本教程适用于以下操作系统版本

  • Ubuntu 22.04 (Jammy Jellyfish)
  • Ubuntu 20.04 (Focal Fossa)

本页内容

  • 先决条件
  • 开始使用
  • 安装 Jellyfin
  • 配置 Nginx 作为反向代理
  • 访问 Jellyfin Web UI
  • 使用 Let's Encrypt 保护 Jellyfin
  • 结论

Jellyfin 是一款免费且开源的媒体流解决方案,可让你托管自己的媒体服务器。它可以安装在 Linux、Windows 和 macOS 上。你可以使用 Jellyfin 管理电影、电视节目、音乐和照片等媒体,并在多个设备之间共享。它还为 Android、Android TV 和 Amazon Fire TV 提供应用程序。它提供多项功能,包括:支持 DLNA、无播放限制、自动从 TheTVDB、TheMovieDB 和 Rotten Tomatoes 获取元数据、自动录制、支持硬件加速等等。

在本教程中,我们将讲解如何在 Ubuntu 20.04 上使用 Jellyfin 安装并设置媒体服务器。

先决条件

  • 一台运行 Ubuntu 20.04 的服务器。
  • 一个指向你服务器 IP 的有效域名。
  • 服务器上已配置 root 密码。

开始使用

开始之前,使用以下命令更新系统软件包:

apt-get update -y

所有软件包更新完成后,使用以下命令安装其他所需的依赖项:

apt-get install apt-transport-https ca-certificates gnupg2 -y

安装完所有依赖项后,即可继续下一步。

安装 Jellyfin

默认情况下,Ubuntu 20.04 默认仓库中不提供 Jellyfin 软件包。因此你需要将 Jellyfin 仓库添加到系统中。首先,使用以下命令下载并添加 GPG 密钥:

wget -O - https://repo.jellyfin.org/jellyfin_team.gpg.key | apt-key add -

添加 GPG 密钥后,使用以下命令将 Jellyfin 仓库添加到 APT:

echo "deb [arch=$( dpkg --print-architecture )] https://repo.jellyfin.org/ubuntu focal main" | tee /etc/apt/sources.list.d/jellyfin.list

接下来,使用以下命令更新仓库缓存并安装 Jellyfin:

apt-get update -y
apt-get install jellyfin -y

安装 Jellyfin 后,你可以使用以下命令查看 Jellyfin 服务的状态:

systemctl status jellyfin

你应该会看到以下输出:

? jellyfin.service - Jellyfin Media Server
     Loaded: loaded (/lib/systemd/system/jellyfin.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/jellyfin.service.d
             ??jellyfin.service.conf
     Active: active (running) since Sun 2020-12-27 06:15:40 UTC; 58s ago
   Main PID: 8454 (jellyfin)
      Tasks: 16 (limit: 4691)
     Memory: 92.3M
     CGroup: /system.slice/jellyfin.service
             ??8454 /usr/bin/jellyfin --webdir=/usr/share/jellyfin/web --restartpath=/usr/lib/jellyfin/restart.sh --ffmpeg=/usr/lib/jellyfin-f>

Dec 27 06:15:48 ubuntu2004 jellyfin[8454]: [06:15:48] [INF] Registering publisher for urn:schemas-upnp-org:device:MediaServer:1 on 104.245.33.>
Dec 27 06:15:48 ubuntu2004 jellyfin[8454]: [06:15:48] [INF] Executed all pre-startup entry points in 0:00:00.6715621
Dec 27 06:15:48 ubuntu2004 jellyfin[8454]: [06:15:48] [INF] Core startup complete
Dec 27 06:15:48 ubuntu2004 jellyfin[8454]: [06:15:48] [INF] Executed all post-startup entry points in 0:00:00.3885698
Dec 27 06:15:48 ubuntu2004 jellyfin[8454]: [06:15:48] [INF] Startup complete 0:00:08.0109863
Dec 27 06:15:50 ubuntu2004 jellyfin[8454]: [06:15:50] [INF] StartupTrigger fired for task: Update Plugins
Dec 27 06:15:50 ubuntu2004 jellyfin[8454]: [06:15:50] [INF] Queueing task PluginUpdateTask
Dec 27 06:15:50 ubuntu2004 jellyfin[8454]: [06:15:50] [INF] Executing Update Plugins
Dec 27 06:15:50 ubuntu2004 jellyfin[8454]: [06:15:50] [INF] Update Plugins Completed after 0 minute(s) and 0 seconds
Dec 27 06:15:51 ubuntu2004 jellyfin[8454]: [06:15:51] [INF] ExecuteQueuedTasks

默认情况下,Jellyfin 监听 8096 端口。你可以使用以下命令进行确认:

ss -antpl | grep 8096

你应该会看到以下输出:

LISTEN    0         512                      *:8096                   *:*        users:(("jellyfin",pid=8454,fd=285))                                           

至此,Jellyfin 已安装并正在运行。现在可以继续下一步。

配置 Nginx 作为反向代理

接下来,建议为 Jellyfin 配置 Nginx 作为反向代理。为此,首先使用以下命令安装 Nginx Web 服务器:

apt-get install nginx -y

Nginx 安装完成后,创建一个新的 Nginx 虚拟主机配置文件:

nano /etc/nginx/conf.d/jellyfin.conf

添加以下内容:

server {
      listen 80;
      server_name jellyfin.yourdomain.com;

      access_log /var/log/nginx/jellyfin.access;
      error_log /var/log/nginx/jellyfin.error;

      set $jellyfin 127.0.0.1;

      location / {
          proxy_pass http://127.0.0.1:8096;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;

          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Protocol $scheme;
          proxy_set_header X-Forwarded-Host $http_host;

          # Disable buffering when the nginx proxy gets very resource heavy upon streaming
          proxy_buffering off;
      }

      # location block for /web - This is purely for aesthetics so /web/#!/ works instead of having to go to /web/index.html/#!/
      location ~ ^/web/$ {
          # Proxy main Jellyfin traffic
          proxy_pass http://$jellyfin:8096/web/index.html/;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Protocol $scheme;
          proxy_set_header X-Forwarded-Host $http_host;
      }

      location /socket {
          # Proxy Jellyfin Websockets traffic
          proxy_pass http://$127.0.0.1:8096;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Protocol $scheme;
          proxy_set_header X-Forwarded-Host $http_host;
      }

        # Security / XSS Mitigation Headers
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";

}

完成后保存并关闭文件。然后使用以下命令检查 Nginx 是否存在语法错误:

nginx -t

如果一切正常,你应该会看到以下输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

接下来,重启 Nginx 服务以应用配置更改:

systemctl restart nginx

你也可以使用以下命令查看 Nginx 的状态:

systemctl status nginx

你应该会看到以下输出:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-12-27 06:18:13 UTC; 6s ago
       Docs: man:nginx(8)
    Process: 9865 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 9879 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 9883 (nginx)
      Tasks: 3 (limit: 4691)
     Memory: 3.6M
     CGroup: /system.slice/nginx.service
             ??9883 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??9884 nginx: worker process
             ??9885 nginx: worker process

Dec 27 06:18:13 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse proxy server...
Dec 27 06:18:13 ubuntu2004 systemd[1]: Started A high performance web server and a reverse proxy server.

至此,Nginx 已安装并配置为服务 Jellyfin。现在可以继续下一步。

访问 Jellyfin Web UI

现在,打开 Web 浏览器,使用 URL http://jellyfin.yourdomain.com 访问 Jellyfin Web UI。你将被重定向到以下界面:

在 Ubuntu 20.04 上安装配置 Jellyfin 媒体服务器完整教程

选择你的语言,然后点击 Next 按钮。你应该会看到以下界面:

在 Ubuntu 20.04 上安装配置 Jellyfin 媒体服务器完整教程

提供管理员用户名和密码,然后点击 Next 按钮。你应该会看到以下界面:

在 Ubuntu 20.04 上安装配置 Jellyfin 媒体服务器完整教程

点击 Next 按钮。你应该会看到以下界面:

在 Ubuntu 20.04 上安装配置 Jellyfin 媒体服务器完整教程

选择你的元数据语言和国家/地区,然后点击 Next 按钮。你应该会看到以下界面:

在 Ubuntu 20.04 上安装配置 Jellyfin 媒体服务器完整教程

选择你想要的选项,然后点击 Next 按钮。你应该会看到以下界面:

在 Ubuntu 20.04 上安装配置 Jellyfin 媒体服务器完整教程

点击 Finish 按钮。你应该会看到 Jellyfin 登录页面:

在 Ubuntu 20.04 上安装配置 Jellyfin 媒体服务器完整教程

提供你的用户名和密码,然后点击 Sign In 按钮。你应该会在以下界面看到 Jellyfin 仪表板:

在 Ubuntu 20.04 上安装配置 Jellyfin 媒体服务器完整教程

现在你可以将媒体添加到媒体库,并通过互联网访问它。

使用 Let's Encrypt 保护 Jellyfin

接下来,你需要安装 Certbot 客户端软件包来安装和管理 Let's Encrypt SSL。首先,使用以下命令安装 Certbot:

apt-get install python3-certbot-nginx -y

安装完成后,运行以下命令为你的网站安装 Let's Encrypt SSL:

certbot --nginx -d jellyfin.yourdomain.com

系统会要求你提供有效的电子邮件地址并接受服务条款,如下所示:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 
[email protected]


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for jellyfin.yourdomain.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/jellyfin.conf

接下来,选择是否将 HTTP 流量重定向到 HTTPS,如下所示:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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): 2

输入 2 并按 Enter 完成安装。你应该会看到以下输出:

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/jellyfin.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://jellyfin.yourdomain.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=jellyfin.yourdomain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/jellyfin.yourdomain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/jellyfin.yourdomain.com/privkey.pem
   Your cert will expire on 2020-10-30. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

 - We were unable to subscribe you the EFF mailing list because your
   e-mail address appears to be invalid. You can try again later by
   visiting https://act.eff.org.

现在,你的网站已通过 Let's Encrypt SSL 保护。你可以使用 URL https://jellyfin.yourdomain.com 安全地访问它。

结论

恭喜!你已在 Ubuntu 20.04 服务器上成功安装并配置了 Jellyfin,配合 Nginx 和 Let's Encrypt SSL。现在你可以流式播放媒体,并通过 Web 浏览器或 Jellyfin 应用程序访问它。如有任何问题,欢迎随时向我提问。