Centos8にJPSonic 入れてみた

# Centos8にJPSonic 入れてみた

作業記録。
- [Jpsonicとは \| tesshu\.com](https://tesshu.com/jpsonic/what-is-jpsonic)


```plantuml

() "port:4040" as pt
() "jpsonic.hoge.com\nhttp/https" as st
st - [apache]
[apache] ..> pt : mod_proxy
pt - [jpsonic] 
```

こんな構成。ドメインは適当に。
Subsonic使ってたのでクセで4040ポートを使用。

## 環境作成

必要なパッケージをインストール
(面倒なのですべてrootで)

```bash
# apache java あたり
yum install httpd mod_ssl java-11-openjdk certbot
systemctl enable --now httpd

# ffmpeg関連
dnf install install https://download.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-8.noarch.rpm
dnf install http://rpmfind.net/linux/epel/7/x86_64/Packages/s/SDL2-2.0.10-1.el7.x86_64.rpm
dnf install ffmpeg -y
```

2021/6/23現在 java-latest-openjdkは java16で、jpsonicにはjava16用が無かったのでjava11を採用。

## jpsonic のインストール

```bash
# jpsonic用のユーザー作成
useradd jpsonic

# jpsonicのダウンロードと解凍
mkdir -p /var/jpsonic
cd /var/jpsonic
wget https://github.com/jpsonic/jpsonic/releases/download/v110.0.0/jpsonic-jetty-embedded-for-jdk11.zip 
unzip jpsonic-jetty-embedded-for-jdk11.zip 

# ffmpegのシンボリックリンク作成
mkdir /var/jpsonic/transcode
ln -s /usr/bin/ffmpeg /var/jpsonic/transcode/ffmpeg
ln -s /usr/bin/ffprobe /var/jpsonic/transcode/ffprobe

# ユーザーをjpsonicに
chown -R jpsonic /var/jpsonic
```

## systemd用のサービスファイル作成

- [Stand\-alone WAR installation \- Airsonic](https://airsonic.github.io/docs/install/war-standalone/) ここを参考に

```bash
vi /etc/systemd/system/jpsonic.service
```

```bash:/etc/systemd/system/jpsonic.service
[Unit]
Description=JPsonic Media Server
After=remote-fs.target network.target
AssertPathExists=/var/jpsonic

[Service]
Type=simple
Environment="JAVA_JAR=/var/jpsonic/jpsonic.war"
Environment="JAVA_OPTS=-Xmx700m"
Environment="JPSONIC_HOME=/var/jpsonic"
Environment="PORT=4040"
Environment="CONTEXT_PATH=/var/jpsonic"
Environment="JAVA_ARGS="
EnvironmentFile=-/etc/sysconfig/jpsonic
ExecStart=/usr/bin/java \
          $JAVA_OPTS \
          -Djpsonic.home=${JPSONIC_HOME} \
          -Dservlet.contextPath=${CONTEXT_PATH} \
          -Dserver.port=${PORT} \
          -jar ${JAVA_JAR} $JAVA_ARGS
User=jpsonic
Group=jpsonic

# See https://www.freedesktop.org/software/systemd/man/systemd.exec.html
# for details
DevicePolicy=closed
DeviceAllow=char-alsa rw
NoNewPrivileges=yes
PrivateTmp=yes
PrivateUsers=yes
ProtectControlGroups=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
RestrictNamespaces=yes
RestrictRealtime=yes
SystemCallFilter=~@clock @debug @module @mount @obsolete @privileged @reboot @setuid @swap
ReadWritePaths=/var/jpsonic

# You can uncomment the following line if you're not using the jukebox
# This will prevent airsonic from accessing any real (physical) devices
#PrivateDevices=yes

# You can change the following line to `strict` instead of `full`
# if you don't want airsonic to be able to
# write anything on your filesystem outside of AIRSONIC_HOME.
ProtectSystem=full

# You can uncomment the following line if you don't have any media
# in /home/…. This will prevent airsonic from ever reading/writing anything there.
#ProtectHome=true

# You can uncomment the following line if you're not using the OpenJDK.
# This will prevent processes from having a memory zone that is both writeable
# and executeable, making hacker's lifes a bit harder.
#MemoryDenyWriteExecute=yes


[Install]
WantedBy=multi-user.target
```

## sysconfig に設定テンプレートを

```bash
vi /etc/sysconfig/jpsonic
```

```bash:/etc/sysconfig/jpsonic
# Set the location of the standalone war to use
#JAVA_JAR=/var/jpsonic/jpsonic.war

# Set any java opts separated by spaces
#JAVA_OPTS=-Xmx700m

# Set a different location for jpsonic home. 
# If this path is /var/libresonic or even contains "libresonic",
# the data from a previous libresonic can be used as is (i.e. without
# renaming libresonic.properties,db/libresonic*,etc
#JPSONIC_HOME=/var/jpsonic

# Change the port to listen on
#PORT=8080

# Change the path that is listened on
#CONTEXT_PATH=/jpsonic

# Add any java args. These are different than JAVA_OPTS in that 
# they are passed directly to the program. The default is empty:
#JAVA_ARGS=

# Note that there are several settings for spring boot, not explicitly listed
# here, but can be used in either JAVA_OPTS or JAVA_ARGS. The full list
# can be found here: 
# https://docs.spring.io/spring-boot/docs/1.4.5.RELEASE/reference/htmlsingle/#common-application-properties
# For example to set debug across the board:
#JAVA_ARGS=--debug

# Or to change the ip address that is listened on:
#JAVA_ARGS=--server.address=127.0.0.1
```

## サービスの登録と起動

```bash
systemctl daemon-reload
systemctl enable --now jpsonic.service
```

## ApacheにVirtualHostsでModProxyの設定を

Let's Encryptで証明書つけた。

- [Let's Encrypt \- フリーな SSL/TLS 証明書](https://letsencrypt.org/ja/)

```bash:/etc/httpd/conf.d/vhosts.jpsonic.hoge.com.conf
<VirtualHost *:80>
        ServerName jpsonic.hoge.com

        RewriteEngine on
        RewriteCond %{SERVER_NAME} =jpsonic.hoge.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,NE,R=permanent]
</VirtualHost>

<VirtualHost *:443>
        ServerName      jpsonic.hoge.com
        SuexecUserGroup jpsonic jpsonic
        SSLProxyEngine  on
        ProxyPass        /      http://localhost:4040/
        ProxyPassReverse /      http://localhost:4040/
        RequestHeader    set    X-Forwarded-Proto "https"
        SSLCertificateFile      /etc/letsencrypt/live/jpsonic.hoge.com/fullchain.pem
        SSLCertificateKeyFile   /etc/letsencrypt/live/jpsonic.hoge.com/privkey.pem
        Include                 /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
```

```bash
service httpd reload
```

で、https://jpsonic.hoge.com にjpsonicができた。

あとは [Jpsonic の初期設定をしてみよう \| tesshu\.com](https://tesshu.com/jpsonic/lets-set-the-initial-setting) あたりを見ながら設定を。