環境
- CentOS7
- nginx 1.12.2
nginx + Subsonicで、Subsonicを動かしたいドメインで起動できるようになっていること前提。
subsonic.example.com
というドメインでアクセスしたいとする。
導入方法
Certbotクライアント入手
Let's Encryptの導入のため、Certbotクライアントを入手。
curl https://dl.eff.org/certbot-auto -o /usr/bin/certbot-auto
chmod 700 /usr/bin/certbot-auto
nginx設定 (認証用のpathへのアクセス)
Let's Encryptは /.well-known/acme-challenge
に一時ファイルを作成して認証を行うため
subsonic.example.com/.well-known/
以下にアクセスがあった場合はSubsonic側を見に行かないようにしておく。
今回はSubsonic用のconfを/etc/nginx/conf.d/subsonic.conf
に記載しているので、該当ファイルに下記のように記載。
/etc/nginx/conf.d/subsonic.conf
server {
listen 80;
server_name subsonic.example.com;
root /usr/share/nginx/html/subsonic.example.com;
location /.well-known/ {
alias /usr/share/nginx/html/subsonic.example.com/.well-known/;
}
}
証明書発行
Certbotクライアントを実行して、証明書を発行する。
成功すると /etc/letsencrypt/live/subsonic.example.com/
以下に cert.pem
chain.pem
fullchain.pem
privkey.pem
のシンボリックリンクが作成される。
certbot-auto certonly --webroot -w /usr/share/nginx/html/subsonic.example.com -d subsonic.example.com --email youraddress@example.com
ls -l /etc/letsencrypt/live/subsonic.example.com/
# シンボリックリンクの作成確認
nginx設定 (SSL設定)
/etc/nginx/conf.d/subsonic.conf
にSSLの設定を行う。
/.well-known
以下は更新時にもアクセスするディレクトリなので、設定はそのままにしておく。
/etc/nginx/conf.d/subsonic.conf
server {
listen 443 ssl;
server_name subsonic.example.com;
root /usr/share/nginx/html/subsonic.example.com;
ssl_certificate /etc/letsencrypt/live/subsonic.example.com/cert.pem;
ssl_certificate_key /etc/letsencrypt/live/subsonic.example.com/privkey.pem;
location /.well-known/ {
alias /usr/share/nginx/html/subsonic.example.com/.well-known/;
}
location / {
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 https;
proxy_set_header Host $http_host;
proxy_max_temp_file_size 0;
proxy_pass http://127.0.0.1:4040;
proxy_redirect http:// https://;
}
}
諸々完了したらnginxを再起動。
sudo systemctl restart nginx
自動更新
証明書の更新は下記のコマンドで実行可能なので、これをcronによきタイミングで設定すればOK。
certbot-auto renew --post-hook "systemctl restart nginx"
# `--dry-run` で起動すると動作のみが確認出来る
certbot-auto renew --dry-run