まとめも

大事だと思ったこと、困ったところのメモ

Let's Encrypt で WEBサーバーのSSL対応 (Ubuntu + Apache)

ubuntu + Apacheの環境でLet's Encryptを使っている情報が少なかったのでメモ

手順

ダウンロード

admin@ubuntu:~$ cd /home/admin/

まず、ダウンロードしたいディレクトリに移動します。 ここでは、自分のホームディレクトリにダウンロードします。

クローン

admin@ubuntu:~$ git clone https://github.com/certbot/certbot

次に、Let's Encrypt のリポジトリをクローンします。 正式なサービスがスタートしてから certbot という名前に変わったようです。

ディレクトリの移動

admin@ubuntu:~$ cd certbot/

処理が終わったらcertbotディレクトリに移動します。

letsencript-autoを実行

root@ubuntu:/home/admin/certbot# ./letsencrypt-auto

実行してみますが、私の環境では、最初の数行で0%から動かなくなりました。 10分程度待つと処理が進みました。途中で中断せずのんびりと待ちます。

certbotクライアントの実行

root@ubuntu:/home/admin/certbot# ./certbot-auto certonly --standalone -d exapmle.hoge.com -d www.exapmle.hoge.com

certbotクライアントを実行すると以下のような画面が表示されます。 ここで入力したメールアドレスは、緊急の通知、鍵を紛失したときの復旧、証明書の有効期限が近付いた場合の通知に使用されるそうです。

   ┌──────────────────────────────────────────────────────────────────────┐
   │ Enter email address (used for urgent notices and lost key recovery)  │
   │ ┌──────────────────────────────────────────────────────────────────┐ │
   │ │hogehoge@example.com                                              │ │
   │ └──────────────────────────────────────────────────────────────────┘ │
   ├──────────────────────────────────────────────────────────────────────┤
   │                     <  OK  >           <Cancel>                      │
   └──────────────────────────────────────────────────────────────────────┘

利用規約の確認

   ┌──────────────────────────────────────────────────────────────────────┐
   │ Please read the Terms of Service at                                  │
   │ https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf. You │
   │ must agree in order to register with the ACME server at              │
   │ https://acme-v01.api.letsencrypt.org/directory                       │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   ├──────────────────────────────────────────────────────────────────────┤
   │                     <Agree >           <Cancel>                      │
   └──────────────────────────────────────────────────────────────────────┘

メールアドレスの入力後、OKを選択すると、利用規約に同意するか尋ねられるので、内容を確認し同意(Agree)を選択します。

証明書の取得

   ┌──────────────────────────────────────────────────────────────────────┐
   │ The program apache2 (process ID 27582) is already listening on TCP   │
   │ port 80. This will prevent us from binding to that port. Please stop │
   │ the apache2 program temporarily and then try again.                  │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   ├──────────────────────────────────────────────────────────────────────┤
   │                               <  OK  >                               │
   └──────────────────────────────────────────────────────────────────────┘

At least one of the (possibly) required ports is already taken.

IMPORTANT NOTES:
 - If you lose your account credentials, you can recover through
   e-mails sent to hogehoge@exapmle.com.
 - 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.

Apacheが80番ポートを使用してますよ。Apacheを止めてもう一度実行してくださいね。という警告が出ました。 Apacheを止めます。

Apacheの停止

root@ubuntu:/home/admin/certbot# apachectl stop

再実行する

root@ubuntu:/home/admin/certbot# ./certbot-auto certonly --standalone -d exapmle.hoge.com -d www.exapmle.hoge.com

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

   Domain: www.exapmle.hoge.com
   Type:   connection
   Detail: DNS problem: NXDOMAIN looking up A for www.exapmle.hoge.com

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A record(s) for that domain
   contain(s) the right IP address. Additionally, please check that
   your computer has a publicly routable IP address and that no
   firewalls are preventing the server from communicating with the
   client. If you're using the webroot plugin, you should also verify
   that you are serving files from the webroot path you provided.

停止後、再実行してみます。サーバー証明書の取得ができたようです。

鍵の確認

root@ubuntu:~# cd /etc/letsencrypt/live/exapmle.hoge.com/
root@ubuntu:/etc/letsencrypt/live/exapmle.hoge.com# cat cert.pem
-----BEGIN CERTIFICATE-----

ここに鍵が表示されています。

-----END CERTIFICATE-----

Apacheを動かす

root@ubuntu:~# apachectl start

止めたApacheを動かします。

ApacheSSLモジュールを有効化

root@ubuntu:~# a2enmod ssl

バーチャルホストの有効化

root@ubuntu:~# a2ensite default-ssl

Apacheの再起動

root@ubuntu:~# service apache2 restart

証明書の再インストール

root@ubuntu:/home/admin/certbot# ./letsencrypt-auto

   ┌──────────────────────────────────────────────────────────────────────┐
   │ No names were found in your configuration files.                     │
   │ You should specify ServerNames in your config files in order to      │
   │ allow for accurate installation of your certificate.                 │
   │ If you do use the default vhost, you may specify the name manually.  │
   │ Would you like to continue?                                          │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   │                                                                      │
   ├──────────────────────────────────────────────────────────────────────┤
   │                     < Yes >           < No  >                        │
   └──────────────────────────────────────────────────────────────────────┘

上記のような画面が表示されるので、 Yes を選択します。

証明書のドメイン名を入力

   ┌──────────────────────────────────────────────────────────────────────┐
   │ Please enter in your domain name(s) (comma and/or space separated)   │
   │ ┌──────────────────────────────────────────────────────────────────┐ │
   │ │exapmle.hoge.com                                                  │ │
   │ └──────────────────────────────────────────────────────────────────┘ │
   ├──────────────────────────────────────────────────────────────────────┤
   │                     <  OK  >           <Cancel>                      │
   └──────────────────────────────────────────────────────────────────────┘
   ┌──────────────────────────────────────────────────────────────────────┐
   │ You have an existing certificate that contains exactly the same      │
   │ domains you requested and isn't close to expiry.                     │
   │ (ref: /etc/letsencrypt/renewal/exapmle.hoge.com.conf)                │
   │                                                                      │
   │ What would you like to do?                                           │
   │ ┌──────────────────────────────────────────────────────────────────┐ │
   │ │        1  Attempt to reinstall this existing certificate         │ │
   │ │        2  Renew & replace the cert (limit ~5 per 7 days)         │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ └──────────────────────────────────────────────────────────────────┘ │
   ├──────────────────────────────────────────────────────────────────────┤
   │                     <  OK  >           <Cancel>                      │
   └──────────────────────────────────────────────────────────────────────┘

同じドメイン名の証明書を持ってますよね。どの作業をしたいのですか。と聞かれるので、1を選択します。

   ┌──────────────────────────────────────────────────────────────────────┐
   │ Please choose whether HTTPS access is required or optional.          │
   │ ┌──────────────────────────────────────────────────────────────────┐ │
   │ │    Easy    Allow both HTTP and HTTPS access to these sites       │ │
   │ │    Secure  Make all requests redirect to secure HTTPS access     │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ │                                                                  │ │
   │ └──────────────────────────────────────────────────────────────────┘ │
   ├──────────────────────────────────────────────────────────────────────┤
   │                     <  OK  >           <Cancel>                      │
   └──────────────────────────────────────────────────────────────────────┘

HTTP通信とHTTPS通信の両方を許可する場合はEasyを、常時HTTPS通信を行う場合はSecureを選択します。

   ┌──────────────────────────────────────────────────────────────────────┐
   │ Congratulations! You have successfully enabled                       │
   │ https://exapmle.hoge.com                                             │
   │                                                                      │
   │ You should test your configuration at:                               │
   │ https://www.ssllabs.com/ssltest/analyze.html?d=exapmle.hoge.com      │
   │                                                                      │
   │                                                                      │
   ├──────────────────────────────────────────────────────────────────────┤
   │                               <  OK  >                               │
   └──────────────────────────────────────────────────────────────────────┘

上記のようなが画面が表示されれば、成功です! HTTPSで通信しているはずです。ブラウザで確認してみてください。

証明書の自動更新

li-one.hatenablog.jp

お知らせ

このページで削った実行結果の細かい情報はリンク先に書いています。 li-one.hatenablog.jp