LoginSignup
26
28

More than 5 years have passed since last update.

Let's Encryptで複数サブドメインの証明書を発行して自動更新を設定する

Posted at

動作環境

CentOS 7.2
nginx 1.9.15

ドメイン1つの場合は

こちらを参考に。
Let's Encrypt で手軽に HTTPS サーバを設定する - Qiita

複数サブドメインのときの変更点

各ドメインのHTTPに対して、以下のように設定する。

server {
    include letsencrypt.conf;

    listen 80;
    listen [::]:80;
    server_name your.domain;

    # 証明書作成後に書いてもよい。くくっておかないとwebrootもhttpsに飛ばされる
    # locationは1つしか当たらないことを利用して、webrootについてはhttpで解決するようにしておく
    location / {
        return 301 https://$host$request_uri;
    }
}

/etc/nginx/letsencrypt.confを作成する。
下で指定している/var/www/html/letscnryptはディレクトリを作成し、nginxが読み書きできるようにしておく。

location ^~ /.well-known/acme-challenge {
    default_type "text/plain";
    root /var/www/html/letsencrypt;
    access_log /var/log/nginx/access_letsencrypt.log;
    error_log /var/log/nginx/error_letsencrypt.log;
}

ここでリロード。

nginx -t && systemctl reload nginx

証明書作成のコマンドは、たとえばこのようになる。

/path/to/letsencrypt-auto certonly --webroot -w /var/www/html/letsencrypt -d your.domain1 -d your.domain2 ……

一度通れば、更新はこのように。ここはドメイン1つのときと変わらない。

/path/to/letsencrypt-auto renew --force-renew

あとはこれをcronにセットしておく。

0 5 * * * /path/to/letsencrypt-auto renew --force-renew && systemctl reload nginx
26
28
0

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
  3. You can use dark theme
What you can do with signing up
26
28