LoginSignup
6
5

More than 5 years have passed since last update.

Ubuntu+nginxの組み合わせで、Let's encryptのワイルドカード証明書取得

Last updated at Posted at 2018-07-12

まず最初に断っておきます。
Let's encryptのワイルドカード証明書はDNS-01認証が必要になります。
なので、動的なDNS書き換えが出来ない環境では、証明書の自動更新が出来ません。
うちでは、DNSをCloudflareにしてやってみようと思っています(この記事では自動更新には触れていません)。

環境

  • OS:Ubuntu 14.04.5 LTS

大雑把な手順

  1. 必要なパッケージのインストール
  2. 証明書の要求
  3. 証明書要求の途中で、SSL証明書の対象ドメインのDNSに、TXTレコードでワンタイムトークンを返すように設定
  4. TXTレコードが引けるようになったら証明書要求継続
  5. nginxの設定に証明書とポート443の設定を追記
  6. nginx起動

必要なパッケージのインストールと事前準備

  1. certbotをインストールするために、レポジトリを登録
    $ sudo add-apt-repository ppa:certbot/certbot
  2. APTをアップデート
    $ sudo apt-get update
  3. certbotをインストール
    $ sudo apt-get install -y python-certbot-nginx
  4. nginxを停止
    $ sudo service nginx stop

証明書要求

$ certbot certonly --manual -d *.ドメイン名 --preferred-challenges dns-01 --server https://acme-v02.api.letsencrypt.org/directory

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
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-v02.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 our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
-------------------------------------------------------------------------------
(Y)es/(N)o:

と訊かれるので、通常は「N」→リターン

Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for ドメイン名

-------------------------------------------------------------------------------
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
-------------------------------------------------------------------------------
(Y)es/(N)o:

と訊かれるので、DNS修正の準備が出来たら「Y」→リターン

-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.ドメイン名 with the following value:

[ワンタイムトークン]

Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue

と出るので、対象ドメインのDNSに、表示されているワンタイムトークンを返すようにTXTレコードを追加し、DNS再起動。
例えば「example.com」にSSL証明書を発行する場合は、bind9の「example.com」ゾーン設定ファイルに、

_acme-challenge IN TXT [ワンタイムトークン]

を追記して、DNSを再起動し情報が更新されたタイミングでTXTレコードを引くとワンタイムトークンを返すようになる。
例)

# dig -t txt _acme-challenge.example.com @8.8.8.8

; <<>> DiG 9.9.5-3ubuntu0.17-Ubuntu <<>> -t txt _acme-challenge.example.com @8.8.8.8
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61376
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;_acme-challenge.example.com. IN      TXT

;; ANSWER SECTION:
_acme-challenge.example.com. 20411 IN TXT     "ワンタイムトークン"

;; Query time: 2 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Thu Jul 12 11:55:25 JST 2018
;; MSG SIZE  rcvd: 114

と返ってくるようになったらOK。
;; ANSWER SECTION:」にワンタイムトークンが返ってこないうちに先に進むと失敗する。
返ってくるようになったら、リターンを押す。

Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/ドメイン名/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/ドメイン名/privkey.pem
   Your cert will expire on 2018-10-10. 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"
 - 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.
 - 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

するとTXTレコードのチェックが行われ、正しく引ければ「Congratulations!」となる。
/etc/letsencrypt 配下に証明書ファイルが出来ているか確認する。

nginx設定ファイルに証明書設定

ポート80番の設定を、

server {
  listen 80;
  server_name 受け付けるホスト名;

  location / {
    return 301 https://$server_name$request_uri;
  }
}

このようにすると、301リダイレクトで今後のポート80番へのアクセスは全てhttpsへリダイレクトされるようになる。
そして、ポート443番の設定を、

server {
  listen 443 ssl;
  server_name 受け付けるホスト名;

  ssl_certificate /etc/letsencrypt/live/ドメイン名/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/ドメイン名/privkey.pem;
  ssl_session_tickets on;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers AESGCM:HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers on;

  access_log  /var/log/nginx/access-ssl.log  main;
  index index.html;

  location ~ ^/~(.+?)(/.*)?$ {
    alias /home/$1/public_html$2;
  }

  location / {
    root /var/www/html;
  }
}

PHPを使う場合は、適時設定を行う(下記はPHP7.2の場合)。

  location ~ ^/~([^/]+)/(.+\.php)$ {
    alias /home/$1/public_html/$2;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

  location ~ \.php$ {
    alias /var/www/html;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

そして、nginxを起動。

$ sudo service nginx start
6
5
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
6
5