はじめに

前回構築した環境にLet’s EncryptでSSLを設定したときの作業履歴とメモ。

Firewall設定

httpsのポートを開けます。

# vi /etc/firewalld/zones/public.xml
<zone>
  …省略
  <service name="https"/>
</zone>

設定変更したらサービスを再起動します。

# systemctl restart firewalld

certbotのインストール

Let’s Encryptのクライアントをインストールします。今回はSSHでログインしたユーザーのホームディレクトリで行いました。

$ git clone https://github.com/certbot/certbot

証明書の取得

standaloneプラグイン(自前のHTTPサーバーを起動)で行うため、Nginxを止めておきます。

# systemctl stop nginx

インストールしたクライアントのディレクトリに移動して

$ cd certbot
$ ./certbot-auto certonly --standalone -t

まず、必要なパッケージのインストールについて確認が入るので「y」でインストールする。

次にメールアドレスを聞かれるので入力する

Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): メールアドレス

次に規約とメール送信について聞かれるので以下のように入力しました。

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A

-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: N

最後にドメインを入力します。

Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): ドメイン

成功すると以下のように表示されます。

IMPORTANT NOTES:
 - Congratulations!〜以下省略

Nginxの設定

証明書の設定と、httpへのアクセスをhttpsに転送する設定、あと後述する自動更新のための設定を行います。

# vi /etc/nginx/conf.d/設定ファイル名.conf
server {
    listen 443 ssl; # 80から変更
    proxy_set_header X-Forwarded-Proto https;
   # 以下省略
}
# vi /etc/nginx/conf.d/default.conf
server {
    # 自動更新のための設定
    location ^~ /.well-known/acme-challenge/ {
      default_type "text/plain";
      root        /usr/share/nginx/html;
    }

    # http =&gt; https
    location / {
        return 301 https://$host$request_uri;
    }
}

設定したらサービスを起動してhttpsで接続できるか、httpでアクセスしたらhttpsに転送されるか確認します。

# systemctl start nginx

証明書の更新

証明書を作成するときに指定したモードで更新も行われるけど、standaloneプラグインだとNginxを停止させなければいけないので、webrootプラグインに変更します。

以下のコマンドで実行がてら設定ファイルも更新されます。

$ ./certbot-auto certonly --webroot -w /usr/share/nginx/html -d ドメイン--agree-tos --force-renewal -n

とココまで書いてて気がついたけどNginxプラグインというのがあったので最初からそれを使っていたら良さげ。

更新を自動化する

cronを設定します。

0 4 1 * * /home/ユーザー名/certbot/certbot-auto renew && /bin/systemctl reload nginx

参考

https://qiita.com/HeRo/items/f9eb8d8a08d4d5b63ee9