約半年前に社命でリモートを推奨されてからというものの、その後どっぷりリモートワーカーな自分は、
コロナ禍に右往左往する事なく、モクモクと仕事と勉強にハゲんでいるのです。とはいえこの状況にも飽きてきましたが。
Caddyのインストールしていて諸々に気づいたのですが、
CaddyにはV1とV2のバージョンがあって、設定やコマンドなどが結構刷新されていて、
その辺に気付かずV1の書き方とか紛れ込んでて、Caddy自体が起動しないという事象に若干ハマった。
なので、その辺をケアして進めないとダメねって話。
そんな感じでHere we go
■Caddy用のsystemdユニットファイルの作成
インストールが終わったら、お次はCaddyの起動スクリプトを用意します。
公式がgithubに上げているので、そちらを落としてくるかコピって作成をするスタイル。
https://github.com/caddyserver/dist/blob/master/init/caddy.service
$ sudo vim /etc/systemd/system/caddy.service
# caddy.service # # For using Caddy with a config file. # # Make sure the ExecStart and ExecReload commands are correct # for your installation. # # See https://caddyserver.com/docs/install for instructions. # # WARNING: This service does not use the --resume flag, so if you # use the API to make changes, they will be overwritten by the # Caddyfile next time the service is restarted. If you intend to # use Caddy's API to configure it, add the --resume flag to the # `caddy run` command or use the caddy-api.service file instead. [Unit] Description=Caddy Documentation=https://caddyserver.com/docs/ After=network.target [Service] User=caddy Group=caddy ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile TimeoutStopSec=5s LimitNOFILE=1048576 LimitNPROC=512 PrivateTmp=true ProtectSystem=full AmbientCapabilities=CAP_NET_BIND_SERVICE [Install] WantedBy=multi-user.target
caddy file-server --domain example.com
で動くんだろうけど、Caddy設定ファイルを読み込ませたり、
パラメータとかはUnitで設定した方が良いのかと。
お次はconfg周りCaddyfile書いていく。
構造はNginxに似てる感じかなと。
Caddyfile Concepts
https://caddyserver.com/docs/caddyfile/concepts#caddyfile-concepts
$ sudo vim /etc/caddy/Caddyfile
import /etc/caddy/*.caddy hogehoge.net, www.hogehoge.net { tls [email protected] root * /var/www/html encode zstd gzip templates file_server browse log { output file /var/log/caddy/access.log { rotate_size 50 rotate_age 5 rotate_keep 4 rotate_compress format single_field common_log } } # php-fpm listening via a unix socket: php_fastcgi unix//var/run/php-fpm/www.sock { index index.php } @try_files { file { try_files {path} {path}/ /index.php?{query}&p={path} } } rewrite @try_files {http.matchers.file.relative} }
バーチャルホストの設定は、
ドメイン毎にブロックで囲えば良い感じ。
自分はWordPressを使ってるので、
Apacheだとmod_rewite使って書いたりするお馴染みのヤツも、
Nginxと同じ感覚でtry_fileを書けたりします。
try_fileの記述はこのドキュメントに詳しく書いてあります。
https://caddyserver.com/docs/caddyfile/directives/try_files
Unitの自動起動の有効化とCaddyの起動とUnit設定ファイルの再読み込みを
$ sudo systemctl daemon-reload $ sudo systemctl enable caddy $ sudo systemctl start caddy
Caddyの起動に成功したら、
SSLなんかはLet’s Encryptから証明書を取得して自動で設定される様になってます。
楽ですね。
ただ、IP制限とかなんかまだわかんない部分があるので、
現状はNginxでHTTP/2という。。
次はLiteSpeedとかやってみようと思いながら就寝。