Let's Encrypt でサイトを SSL 化する
2017/10/02 22:34
  • ずいぶん今更感もありますが、さくらの VPS 環境に Let's Encrypt で発行した証明書を
    使ってサイトを SSL 化したときのまとめです。

    Let's Encrypt とは


    Let's Encrypt は、認証局(CA)として「SSL/TLSサーバ証明書」を無料で発行するとともに、証明書の発行・インストール・更新のプロセスを自動化することにより、TLS や HTTPS(TLSプロトコルによって提供されるセキュアな接続の上でのHTTP通信)を普及させることを目的としているプロジェクトです。2016年4月12日 に正式サービスが開始されました。
    出典: Let's Encrypt 総合ポータル

    Let's Encrypt 自体はプロジェクトの名称のようですが、ぼくたち利用者にとっては無料で SSL 証明書を発行するためのツールと言ってしまっても過言ではないような、ちょっと乱暴すぎるような気もしなくもないです。

    対応している証明書の種類

    現在のところ、ドメインごとの個別の証明書発行のみ対応となっており、
    ワイルドカード証明書については非対応のようです。

    しかしながら、2018年1月からワイルドカード証明書にも対応することが公式サイトで発表されました。
    ますます便利になり、来年が待ち遠しいです。笑

    certbot-auto のセットアップ

    前置きが長くなりましたが、 Let's Encrypt を使って証明書を発行するには、
    専用ツール certbot-auto のインストールが必要となります。

    $ sudo curl https://dl.eff.org/certbot-auto -o /usr/bin/certbot-auto
    $ sudo chmod 700 /usr/bin/cerbot-auto

    /usr/bin だけでなく、 /usr/local/bin も sudo 権限が必要のようです。/usr/local/bin にはデフォルトではパスが通っていないので、もし /usr/local/bin に置いた場合はパスを通してやる必要があります。

    証明書を発行する


    続いて、 certbot-auto コマンドを使って証明書を発行します。


    $ certbot-auto certonly \
    > --webroot \
    > -w /path/to/webroot \
    > -d your-domain.com \
    > --email=test@test.com

    Requesting to rerun /usr/local/bin/certbot-auto with root privileges...
    Bootstrapping dependencies for RedHat-based OSes... (you can skip this with --no-bootstrap)
    yum is /usr/bin/yum
    Loaded plugins: fastestmirror, security
    Setting up Install Process
    Loading mirror speeds from cached hostfile
    * base: ftp.iij.ad.jp
    * epel: ftp.riken.jp
    * extras: ftp.iij.ad.jp
    * remi-safe: mirrors.tuna.tsinghua.edu.cn
    * updates: ftp.iij.ad.jp
    Package augeas-libs-1.0.0-10.el6.x86_64 already installed and latest version
    Package libffi-devel-3.0.5-3.2.el6.x86_64 already installed and latest version
    Package redhat-rpm-config-9.0.3-51.el6.centos.noarch already installed and latest version
    Resolving Dependencies
    --> Running transaction check
    ... 以下略

    上記のように、 Webroot のパス、ドメイン名、メールアドレスを指定します。


    Install 7 Package(s)
    Upgrade 19 Package(s)

    Total download size: 42 M
    Is this ok [y/N]: y


    始めてコマンドを叩いたときは、必要なパッケージの追加インストールおよびアップデートが始まると思うので確認した上で y しましょう。Let's Encrypt 使いたかったら嫌でもインストールしなきゃだめなので、選択権はないですが…
    (ぼくは pyenv 派なのに python-virtualenv がインストールされて悲しくなったのは秘密)

    また、オプションは他にも様々あるので


    $ cerbot-auto --help

    などで参照すると良いと思います。

    続いて、規約に同意します。


    -------------------------------------------------------------------------------
    Please read the Terms of Service at
    https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.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 <--- 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 <--- いかなる理由でもメールが来るのは嫌だったので強気の N を指定ww
    Obtaining a new certificate
    Performing the following challenges:
    http-01 challenge for your-domain.com
    Using the webroot path /path/to/webroot for all unmatched domains.
    Waiting for verification...
    Cleaning up challenges

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

    以上で各種証明書の発行が完了しました。


    生成された各種ファイルの確認

    各種証明書は /etc/letsencrypt/live/指定したドメイン名 以下に生成されるようです。

    $ sudo ls -l /etc/letsencrypt/live/your-domain.com/
    total 4
    -rw-r--r-- 1 root root 543 Oct 1 19:07 README
    lrwxrwxrwx 1 root root 45 Oct 1 19:07 cert.pem -> ../../archive/your-domain.com/cert1.pem
    lrwxrwxrwx 1 root root 46 Oct 1 19:07 chain.pem -> ../../archive/your-domain.com/chain1.pem
    lrwxrwxrwx 1 root root 50 Oct 1 19:07 fullchain.pem -> ../../archive/your-domain.com/fullchain1.pem
    lrwxrwxrwx 1 root root 48 Oct 1 19:07 privkey.pem -> ../../archive/your-domain.com/privkey1.pem

    パスをシンボリックリンクにすることで、スムーズに証明書を更新できる仕組みになっているようです。
    証明書の更新は certbot-auto renew するだけ。
    管理するすべての証明書が更新されるようです。(シンボリックリンクなので ssl.conf ファイルは変更不要)
    もう一度インストール完了後の IMPORTANT NOTES を抜粋


    Your cert will expire on 2017-12-30. To obtain a new or tweaked
    version of this certificate in the future, simply run certbot-auto
    again. To non-interactively renew *all* of your certificates, run
    "certbot-auto renew"


    Apache の設定をする

    最後に、いま発行した各種証明書を Apache の conf ファイル内で指定します。

    ssl.conf で必要なディレクティブに対応するファイルは以下です。

    SSLCertificateFile cert.pem (SSLサーバ証明書ファイル)
    SSLCertificateKeyFile privkey.pem (SSLサーバ証明書とペアになる秘密鍵)
    SSLCertificateChainFile chain.pem (CSPSSLの中間証明書ファイルを指定)

    ssl.conf の例
    ぼくの環境だと apache version 2.2 系なので、 /etc/httpd/conf.d/ssl.conf 内に記載します。

    <VirtualHost *:443>
        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/your-domain.com/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/your-domain.com/privkey.pem
        SSLCertificateChainFile /etc/letsencrypt/live/your-domain.com/chain.pem
    
        # 諸々省略します…
    </VirtualHost>
    

    最後に、 apache を再起動すれば https://your-domain.com/ にアクセスできるようになっていると思います。
    http -> https への強制リダイレクトも入れておいたほうが吉だと思います。

    具体的な方法については以前紹介したのでそちらをご覧ください。

    サイトを SSL 化したときに行ったこと

    証明書の自動更新

    最後に、ついつい忘れがちになる証明書の更新ですが、前述したとおり、
    cerbot-auto コマンドには自動更新のオプションが用意されているので、 cron に仕込んであげると安心です。※ルートユーザーの crontab に登録しましょう。


    0 1 * * 0 certbot-auto renew --post-hook "/etc/init.d/httpd graceful" >/dev/null 2>&1


    更新間隔は適当ですが、いずれにせよ定期的に実行してあげれば、必要なタイミングになれば更新されます。

    参考サイト

    Let’s EncryptのSSL証明書で、安全なウェブサイトを公開

    人気ブログランキングへ ブログランキング・にほんブログ村へ
    ↑応援よろしくお願いします!m(_ _)m

  • <2017/10/02 22:34>
  • サーバー
  • Let's EncryptSSL証明書認証局CAHTTPSVPSさくらcerbot-auto
  • 新しい記事へ
    JPG, PNG画像を適切に圧縮できるWebサービス

    古い記事へ
    [CentOS] CentOS 6 に PHP 5.6 をインストールする

profile picture

自己紹介的な何か

@wkmettyでついったーやってます。時々。 6年間勤めたゲーム会社を2018年2月に退職しフリーランスのプログラマに。 WordPress Core, WP-CLI コントリビューター。 お仕事募集中です。