LoginSignup
0
2

More than 5 years have passed since last update.

nginx + letsencryptのssl化手順(備忘)

Last updated at Posted at 2017-07-07

環境

cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
nginx -v
nginx version: nginx/1.12.0

まずはletsencryptのインストール

※すでにnginxをインストール + ドメイン設定が済んだ状態から進めます

■gitからletsencryptのインストール

git コマンドをする前にletsencryptをインストールしたいディレクトリに移動
(今回はroot で/root配下で実行)

git clone https://github.com/letsencrypt/letsencrypt

■コマンド実行したディレクトリ配下にletsencryptのディレクトリが作成されるので移動する

cd letsencrypt

■証明書の作成
対象のドメインの数だけ -dで繋げる

certbot-auto certonly -d <domain> -d <domain> ...

上記コマンド実行後なんか聞かれるので答える
今回は1を押下

How would you like to authenticate with the ACME CA?
-------------------------------------------------------------------------------
1: Place files in webroot directory (webroot)
2: Spin up a temporary webserver (standalone)
-------------------------------------------------------------------------------

ドメインごとのdocumentrootを聞かれるので回答
(今回は/var/www/htmlと回答した)

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/<domain>/fullchain.pem. Your cert will
   expire on 2017-10-05. To obtain a new or tweaked version of this
   certificate in the future, simply run certbot again. To
   non-interactively renew *all* of your certificates, run "certbot
   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

上記文言がでれば成功

nginxの設定

■空かさずnginxの設定をする

もともとサブドメイン用のconfファイルを用意していたので
httpでアクセスした時もリダイレクトする設定を入れる
ssl_certificat には証明書 ---fullchain.pemを指定
ssl_certificate_key には秘密鍵 ---privkey.pemを指定

dev-example.conf
server {
    listen       80;
    server_name  <domain>;
    return 301 https://$host$request_uri; #リダイレクト設定
    }

server {
        listen 443 ssl;
    server_name  <domain>;

    ssl on;

        ssl_certificate       /etc/letsencrypt/live/<domain>/fullchain.pem;
        ssl_certificate_key   /etc/letsencrypt/live/<domain>/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

以下略

あとはnginxをrestartするのをお忘れなく

systemctl restart nginx

letsencryptの自動更新クーロンを仕込む

letsencryptは90日有効のため月一程度で更新する必要がある

■letsencryptの更新を月1回行うクーロン設定

crontab -e

毎月1日の5時1分に実行させる
・ドメインごとに更新する場合

00 05 01 * * /root/letsencrypt/certbot-auto certonly -d <domain> --renew-by-default && systemctl restart nginx

・一括で更新する場合

00 05 01 * * /root/letsencrypt/certbot-auto renew --force-renewal && systemctl restart nginx

以上

20170726追記:

サブドメインの追加対応

新しくサブドメインを追加しようとした時にエラーとなったため追記する

今まで通りに新しくサブドメインを追加する時にうまくいかなかった。

/root/letsencrypt/certbot-auto certonly -d <domain_name> -d ... -d <new_domain_name>

でまたいろいろ聞かれるのでdocumentrootとか答える

これでできると思ったが以下のエラーが

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: <new_domain_name>
   Type:   unauthorized
   Detail: Invalid response from
   http://<new_domain_name>/.well-known/acme-challenge/Yn0ptioSqQrlX8tRWVII4C_HJRj8D_-c0eGomUME8wM:
   "<html>
   <head><title>404 Not Found</title></head>
   <body bgcolor="white">
   <center><h1>404 Not Found</h1></center>
   <hr><center>"

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address.

ドメイン登録がおかしいとかいってるけどそんなことないし。。。

新規追加のドメインはまだvirturalhostの設定をしていないため一応してみた。
こないだの奴をコピーして作った

cp -pi /etc/nignx/conf.d/dev-example.comf /etc/nginx/conf.d/new.conf

結果以下で設定でいけた(のちほどssl化するためコメント等で対応する)

new.conf
#server {
#    listen       80;
#    server_name  <domain>;
#   return 301 https://$host$request_uri; #リダイレクト設定
#   }

server {
#        listen 443 ssl;
    listen       80;
    server_name  <domain>;

#   ssl on;

#        ssl_certificate       /etc/letsencrypt/live/<domain>/fullchain.pem;
#        ssl_certificate_key   /etc/letsencrypt/live/<domain>/privkey.pem;

#   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

以下略

port80で通してやる必要があった。
よくよく考えればそうだろ。。。

まぁ今後同じことを繰り返さないために記載する

0
2
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
0
2