Let’s Encrypt で SSL を設定する [CentOS + Nginx 編]

CentOS + Nginx な構成のサーバーに Let’s Encrypt で SSL を設定する方法をご紹介します。

Let’s Encrypt

certbot インストール

yum でインストールする方法もありますが Python の依存関係が面倒そうだったので、github から zip でダウンロードして使うことにしました。

cd /usr/local/
 
wget https://github.com/certbot/certbot/archive/master.zip
unzip master.zip
mv certbot-master certbot
 
cd certbot/

certbot-auto コマンドの確認

certbot-auto コマンドが実行できることを確認します。

$ ./certbot-auto --help
Usage: certbot-auto [OPTIONS]
A self-updating wrapper script for the Certbot ACME client. When run, updates
to both this script and certbot will be downloaded and installed. After
ensuring you have the latest versions installed, certbot will be invoked with
all arguments you have provided.
 
Help for certbot itself cannot be provided until it is installed.
 
  --debug                                   attempt experimental installation
  -h, --help                                print this help
  -n, --non-interactive, --noninteractive   run without asking for user input
  --no-bootstrap                            do not install OS dependencies
  --no-self-upgrade                         do not download updates
  --os-packages-only                        install OS dependencies and exit
  --install-only                            install certbot, upgrade if needed, and exit
  -v, --verbose                             provide more output
  -q, --quiet                               provide only update/error output;
                                            implies --non-interactive
 
All arguments are accepted and forwarded to the Certbot client when run.

SSL 証明書の取得

以下のように certbot-auto コマンドを実行しします。

./certbot-auto certonly --standalone \
-d example.com \
-m admin@example.com \
--agree-tos \
--non-interactive

失敗例

ちなみに Nginx を起動中に ./certbot-auto certonly –standalone を実行すると失敗します。

Problem binding to port 80: Could not bind to IPv4 or IPv6.
 
IMPORTANT NOTES:
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.

Nginx を停止して、

service nginx stop

再び ./certbot-auto certonly –standalone を実行すると、今度は成功しました。

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/codenote.net/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/codenote.net/privkey.pem
   Your cert will expire on 2018-09-09. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - If you like Certbot, please consider supporting our work by:
 
   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Let’s Encrypt 取得ファイルの確認

最後に、念のため ./certbot-auto certonly コマンドで取得した各ファイルを確認しておきます。

/etc/letsencrypt/
|-- accounts # アカウント情報
|-- archive # 取得した証明書ファイルがドメイン毎にディレクトリに保存される
|   `-- example.com
|       |-- cert1.pem
|       |-- chain1.pem
|       |-- fullchain1.pem
|       `-- privkey1.pem
|-- csr
|-- keys
|-- live # 最新の証明書へのシンボリックリンクが作成される
|   `-- example.com
|       |-- README
|       |-- cert.pem -> ../../archive/example.com/cert1.pem
|       |-- chain.pem -> ../../archive/example.com/chain1.pem
|       |-- fullchain.pem -> ../../archive/example.com/fullchain1.pem
|       `-- privkey.pem -> ../../archive/example.com/privkey1.pem
|-- renewal
|   `-- example.com.conf
`-- renewal-hooks

SSL 証明書を取得できたら、次は Nginx に SSL の設定を追加しないといけませんが、その方法は別の記事で説明します。

以上、Let’s Encrypt で SSL 証明書を取得した現場からお送りしました。

参考情報