Ubuntu19.10にcaddyをインストールする

Ubuntu19.10にcaddyをインストールする

自動で簡単にLet’s Encrypt からSSL証明書を発行してくれるwebサーバーcaddyをUbuntuにインストールして、利用してみる

環境

  • OS  Ubuntu19.10
  • Caddy 1.0.3

caddyとは

go言語で実装されたWEBサーバーで、簡単にhttps化が可能なのが特徴

インストール

curlでインストールする

## rootで作業
sudo su

## インストール
curl https://getcaddy.com | bash -s personal

<出力結果>
Command 'curl' not found, but can be installed with:

apt install curl

curlがなかったのでインストール

## curlインストール
apt-get install -y curl

## 再度実行
curl https://getcaddy.com | bash -s personal

## 確認
whereis caddy

<出力結果>
caddy: /usr/local/bin/caddy

caddyを実行してみる

実行してブラウザからhttp://localhost:2015にアクセス

## 実行
caddy

<出力結果>
Activating privacy features... done.

Serving HTTP on port 2015 
http://:2015

WARNING: File descriptor limit 1024 is too low for production servers. At least 8192 is recommended. Fix with `ulimit -n 8192`.

404 Not Found が表示されるので、htmlをファイルを読むように設定する

ディレクトリ作成

必要なディレクトリを作成し、権限を付与していく

mkdir /etc/caddy

chown -R root:www-data /etc/caddy

mkdir /etc/ssl/caddy

chown -R www-data /etc/ssl/caddy

chmod 0770 /etc/ssl/caddy

mkdir /var/www

chown www-data:www-data /var/www

Caddy.serviceをダウンロードする

## ダウンロード
curl -s https://raw.githubusercontent.com/mholt/caddy/master/dist/init/linux-systemd/caddy.service -o /etc/systemd/system/caddy.service

## 確認
cat /etc/systemd/system/caddy.service

<出力結果>
[Unit]
Description=Caddy HTTP/2 web server
Documentation=https://caddyserver.com/docs
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service

[Service]
Restart=on-abnormal

; Do not allow the process to be restarted in a tight loop. If the
; process fails to start, something critical needs to be fixed.
StartLimitIntervalSec=14400
StartLimitBurst=10

; User and group the process will run as.
User=www-data
Group=www-data

; Letsencrypt-issued certificates will be written to this directory.
Environment=CADDYPATH=/etc/ssl/caddy

; Always set "-root" to something safe in case it gets forgotten in the Caddyfile.
ExecStart=/usr/local/bin/caddy -log stdout -agree=true -conf=/etc/caddy/Caddyfile -root=/var/tmp
ExecReload=/bin/kill -USR1 $MAINPID

; Use graceful shutdown with a reasonable timeout
KillMode=mixed
KillSignal=SIGQUIT
TimeoutStopSec=5s

; Limit the number of file descriptors; see `man systemd.exec` for more limit settings.
LimitNOFILE=1048576
; Unmodified caddy is not expected to use more than that.
LimitNPROC=512

; Use private /tmp and /var/tmp, which are discarded after caddy stops.
PrivateTmp=true
; Use a minimal /dev (May bring additional security if switched to 'true', but it may not work on Raspberry Pi's or other devices, so it has been disabled in this dist.)
PrivateDevices=false

Caddyfileを作成する

## ファイル作成
touch /etc/caddy/Caddyfile

caddyを実行する

## 設定ファイルの再読込
systemctl daemon-reload

## 自動起動
systemctl enable caddy

## 確認
systemctl status caddy

<出力結果>
● caddy.service - Caddy HTTP/2 web server
   Loaded: loaded (/etc/systemd/system/caddy.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: https://caddyserver.com/docs

Firewall設定

firewallを設定する

## http許可
ufw allow http

## 一応httpsも
ufw allow https

Caddyfile編集

## 編集
vi /etc/caddy/Caddyfile

<編集>
プライベートIP {
 root /var/www
 gzip
}

caddy実行

htmlファイルを作成して、caddyを実行する

## index.htmlを作成
echo '<h1>Hello World</h1>' | sudo tee /var/www/index.html

## caddy実行
systemctl start caddy

ブラウザからhttp://プライベートIP:2015にアクセス

Hello Worldが表示される