LoginSignup
38

More than 3 years have passed since last update.

Amazon Linux2でLet's Encrypt使おうとしたらコケた話

Last updated at Posted at 2018-03-23

2019/06/27更新

本記事は内容が古いため以下を参照してください
Amazon Linux2とLet's EncryptでSSL対応サーバを0から爆速構築

遭遇した問題

AWSのEC2でAmazon linux2のサーバに対してLet's Encryptの証明書取得をしようとしたらちょっとコケた。
具体的にはcertbot-autoで証明書取得時に次のエラーが出た。

Sorry, I don't know how to bootstrap Certbot on your operating system!

You will need to install OS dependencies, configure virtualenv, and run pip install manually.
Please see https://letsencrypt.readthedocs.org/en/latest/contributing.html#prerequisites
for more info.

これはcertbot-autoにAmazon Linux2の定義がないために起こっているエラー。
手動でインストールやら設定やらのあれこれをしなければいけないとのこと。
わー、めんどくさい。

サクッとやる方法

ちょーっと調べてみると見事に解決されている記事が…!!

Amazon Linux 2でLet’s Encryptが使えない

パッケージの自動インストール関連で問題が起きてもおかしくないけど、
新規構築なのでインスタンス潰せばいいや!ってノリでやってみます。

1. certbot-autoを引っ張ってきて実行可能にする

これはおなじみですね。
公式ではchmod a+x certbot-autoだけど、
root以外で動かしてほしくないのでchmod 700 certbot-autoにしときます。

wget https://dl.eff.org/certbot-auto
chmod 700 certbot-auto

2. certbot-autoを書き換える

まず、バックアップを取ります。

cp ./certbot-auto ./certbot-auto.bak

エディタで開き、次の記述を探します。(Amazonで検索をかけるといいと思います)
試した際の環境では843行目にありました。

elif [ -f /etc/issue ] && grep -iq "Amazon Linux" /etc/issue ; then
  Bootstrap() {
    ExperimentalBootstrap "Amazon Linux" BootstrapRpmCommon
  }
  BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION"

これを次のように書き換えてしまいます。

 elif grep -i "Amazon Linux" /etc/issue > /dev/null 2>&1 || \
   grep 'cpe:.*:amazon_linux:2' /etc/os-release > /dev/null 2>&1; then
  Bootstrap() {
    ExperimentalBootstrap "Amazon Linux" BootstrapRpmCommon
  }
  BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION"

3. certbot-auto実行

好みもあるとは思いますが、コマンドとして使えるように/usr/local/binに移してから証明書を取得します。

sudo mv ./certbot-auto /usr/local/bin
certbot-auto certonly --webroot -w /var/www/html -d your.domain --email you@your.domain -n --agree-tos --debug

次のようなメッセージが出れば完了。

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
 ...

4. その他雑記

問題なく動いており、cronでの更新も出来ている。
しかしながらcertbot-auto公式に次のような文言が。

Note:
certbot-auto will always try to fetch the newest version of itself from its most recent release. If you want it to be locked to a specific version and not receive automatic updates, run it with the --no-self-upgrade flag. Also, if you're nervous about downloading and running scripts from the network, some extra verification steps are available.

(意)訳:
certbot-autoは常に最新のバージョンを取得するよ。 バージョンをロックしたり、自動アップデートをしたくなかったら、--no-self-upgradeフラグを付けて実行してね。 外部ネットワークからスクリプトをダウンロードして実行することに慎重になってる人向けに、他の認証手順も用意してるよ。

ひえー。
気づかないところだった。

ということで、紹介した手順かつcronで自動更新したい人は--no-self-upgradeフラグもつけときましょう。

/**そもそもAmazon Linux2に手を出す段階じゃないのかも…。**/

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
38