[Linux] Cent OS 6 で sudo 無しの Let's Encrypt

注意

2016年6月現在 Let's Encrypt はベータ期間が終了し、公式のクライアントは certbot というものに置き換わっている。
自分では Perl で実装したモジュールである Protocol::ACME を使っているので公式クライアントの状態は不明だが、以下の情報はまったく役に立たない可能性がある。

前書き

Let's Encrypt を使ってみようとしたが、インストールに sudo 必須なのが微妙。CentOS 6なので正式にはサポートもしていないらしい。
そこをなんとか sudo 無しでがんばってみた。

ただ、後で知ったが実際には純正クライアントを使わなければいけないということではないようだ。クライアント実装一覧にライブラリも含めたくさん存在する。クライアントでは ACME Tiny が手軽そうだ。

Python 2.7 等必要なパッケージを入れる。

https://github.com/letsencrypt/letsencrypt/tree/master/bootstrap に必要なパッケージ類があるので参考にした。

yum install centos-release-SCL
yum install python27 python27-devel python27-virtualenv gcc \
    dialog augeas-libs openssl-devel libffi-devel redhat-rpm-config ca-certificates

これで、scl enable python27 'コマンド ...' のようにして Python 2.7 が使える環境を実行できる((scl enable python27 -- コマンド ...だとクオートが不要。))。
普通はシェルを実行して環境を切り替えるが、自動化の時に困らないように scl コマンドを常に使って作業する。

Let's Encrypt を用意する

リポジトリを clone する。--depth 1で履歴は省略する。

git clone --depth 1 https://github.com/letsencrypt/letsencrypt
cd letsencrypt

virtualenv で Let's Encrypt の環境を用意する。

$ scl enable python27 'virtualenv --no-site-packages --python python ~/.local/share/letsencrypt'
Already using interpreter /opt/rh/python27/root/usr/bin/python
New python executable in /home/user/.local/share/letsencrypt/bin/python
Installing Setuptools...done.
Installing Pip...done.

help を表示してみると

$ scl enable python27 './letsencrypt-auto --help'
Updating letsencrypt and virtual environment dependencies...../home/user/.local/share/letsencrypt/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
./home/user/.local/share/letsencrypt/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
.
Running with virtualenv: sudo /home/user/.local/share/letsencrypt/bin/letsencrypt --help
[sudo] password for user:
^C

何やら SSL の警告が出たり失敗しているように見えるが、表示されている URL と pip を確認した限りではこれで必要な環境は整った。
パスワードは入力せずに Ctrl-C で中断する。

コマンドの実行

次のようにして letsencrypt が実行できる。

scl enable python27 '~/.local/share/letsencrypt/bin/letsencrypt --help'

次に証明書の作業用ディレクトリを作る。

mkdir -m 700 cert
cd cert
mkdir conf log tmp

証明書を取得する。document root にファイルを置く方法を取る。
また /.well-known/acme-challenge/base64_encoded_stringのような URL にアクセスするのでドットファイルへのアクセスを禁止している場合緩める必要がある。正直このネーミングは今一つだ。*1

httpd.conf
-RedirectMatch 404 /\.
+RedirectMatch 404 /\.(?!well-known/)

fail2ban にも引っかかってしまったのでそれもホワイトリストした。

fail2ban-client set my-httpd-filter addignoreregex '^\S+ \S+ \S+ \[[^]]*\] "GET /\.well-known/'

ようやく実行できる。ドメインごとに doc root が違う場合は -d の前に都度 -w を指定すれば証明書が共用(SAN)ができる。

scl enable python27 '~/.local/share/letsencrypt/bin/letsencrypt certonly
    --webroot --config-dir=conf --work-dir=tmp --logs-dir=log
    -w /var/www/document_root/ -d example.com -d www.example.com
    -w /var/www/docroot_2/ -d another.example.com' # 1行で入力

初回は設定画面が出てくる。((--text --agree-tos --email= のようなオプションが代替である))
Root (sudo) is required to run most of letsencrypt functionality. などと表示されるが気にしない。
しばらくすると Congratulations! と証明書 fullchain.pem のパスが表示される。
同じディレクトリに cert.pem, chain.pem, privkey.pem と分割した形式もあるので Apache 2.2 ではこの3つを使うことになる。

最初はREADMEだけ見て sudo 必須の時点で諦めたのでいていろいろ分からなかったのだがドキュメントの使い方にいろいろと書いてあったようだ。

*1:フォーラムによると意図せずファイルを置かせないためらしい。