無料SSL証明書 Let’s Encryptを導入する
2019/05/03
無料でSSL証明書を発行してくれる、Let’s Encyptがパブリックベータになりましたので導入してみます。
環境:CentOS6.7+Apache2.2.15
Let,s Encryptとは
- 無料でSSL証明書を取得できる
- ルート証明書に承認されている為、対応ブラウザが多い
- 取得できる証明書はドメイン認証
- 証明書の期間は90日間
- 60日での更新を推奨
- ACMEプロトコルにより証明書の自動更新を目指しているが、現時点では不完全
- 複数ドメイン(マルチドメイン)のSSL証明書に対応
- ワイルドカード証明書には対応していない
- ECSSL証明書には対応していない
- アカウントはメールアドレス
事前準備
以下のパッケージがインストールされている事を確認
[httpd][openssl][mod_ssl][git]
インストールされていなければインストールを行う
[root@test]# yum list installed | grep httpd←httpdの確認 httpd.x86_64 2.2.15-39.el6.centos←インストールされている [root@test]# yum list installed | grep openssl←opensslの確認 openssl.x86_64 1.0.1e-30.el6_6.7←インストールされている [root@test]# yum list installed | grep git←gitの確認 [root@test]#←インストールされていない
gitをインストールします。
[root@test]# yum -y install mod_ssl←mod_sslインストール [root@test]# vi /etc/yum.repos.d/epel.repo←epelのリポジトリを有効にする為設定ファイル編集 [epel] name=Extra Packages for Enterprise Linux 6 - $basearch #baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch failovermethod=priority enabled=1←1にする gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 [root@test]# yum -y install git←gitインストールepelのリポジトリを有効にしておく
Let’s Encypt Client のインストール
適当なディレクトリにLet’s Encrypt Clientをインストールします。
今回はhome/test/ディレクトリにインストールします。
[root@test]# cd /home/test←ディレクトリ移動 [root@test test]# git clone https://github.com/letsencrypt/letsencrypt←gitで取得 [root@test test]# cd letsencrypt←ディレクトリ移動 [root@test letsencrypt]# ./letsencrypt-auto --help←インストール
こうする事で、依存するパッケージもインストールや更新をしてくれます
The Let's Encrypt agent can obtain and install HTTPS/TLS/SSL certificates. By default, it will attempt to use a webserver both for obtaining and installing the cert. Major SUBCOMMANDS are: (default) run Obtain & install a cert in your current webserver certonly Obtain cert, but do not install it (aka "auth") install Install a previously obtained cert in a server revoke Revoke a previously obtained certificate rollback Rollback server configuration changes made during install config_changes Show changes made to server config during installation plugins Display information about installed plugins Choice of server plugins for obtaining and installing cert: --apache Use the Apache plugin for authentication & installation --standalone Run a standalone webserver for authentication (nginx support is experimental, buggy, and not installed by default) --webroot Place files in a server's webroot folder for authentication OR use different plugins to obtain (authenticate) the cert and then install it: --authenticator standalone --installer apache More detailed help: -h, --help [topic] print this message, or detailed help on a topic; the available topics are: all, automation, paths, security, testing, or any of the subcommands or plugins (certonly, install, nginx, apache, standalone, webroot, etc)
上のようにヘルプが表示されたら完了です。
–debugを付けないと、下記のようなエラーメッセージが出て完了しない事があるので、その時は–debugを付けてください。
WARNING: Python 2.6 support is very experimental at present... if you would like to work on improving it, please ensure you have backups and then run this script again with the --debug flag!
ただ、これでインストールできるかと思いきや、下記のようなエラーが表示される事があります。
Creating virtual environment... ./letsencrypt-auto: line 167: virtualenv: コマンドが見つかりません
その時は次のVirtualenvのインストールを行ってください。
Virtualenvのインストール
Let’s EncryptはPythonを利用していますが、VirtualenvはPythonで複数のPython実行環境を管理できるようにするパッケージです。
Virtualenvのインストールには、Pythonのパッケージ管理を入れる必要があります。
Pythonのパッケージ管理には「easy_install」や「pip」があり、どちらでもVirtualenvのインストールは可能ですが、
yumでeasy_installを入れるのが手っ取り早いので、その方法でインストールしたいと思います。
[root@test]# yum install python-setuptools←easy_install等Pythonのツールをインストール [root@test]# easy_install virtualenv←virtualenvのインストール
SSL証明書の作成と設定
Debian系のOSの場合は ./letsencrypt-auto –apache で作成から設定まで自動で行ってくれますが、CentOSではできないようなので、手順を追って行っていく必要があります。
[root@test letsencrypt]# service httpd stop ←Apacheの終了 [root@test letsencrypt]# ./letsencrypt-auto certonly --standalone -d www.hogehoge.com←サーバ名(例:hogehoge.com)を指定
※./letsencrypt-auto certonly --standalone -d www.hogehoge.com -d hogehoge.net←複数ドメインに対応させたい場合
メールアドレスの入力が求められるので、メールアドレスを入力して、<了解>を選択します。
利用規約に同意するか聞かれるので、<Agree>を選択します。
下記のようなメッセージが表示されたら完了です。
IMPORTANT NOTES: - If you lose your account credentials, you can recover through e-mails sent to test@hogehoge.com. - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/hogehoge.com/fullchain.pem. Your cert will expire on 2016-04-18. To obtain a new version of the certificate in the future, simply run Let's Encrypt again. - Your account credentials have been saved in your Let's Encrypt 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 Let's Encrypt so making regular backups of this folder is ideal. - If you like Let's Encrypt, 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/hogehoge.com/に証明書が作成されたようなので、確認します。
[root@test letsencrypt]# cd /etc/letsencrypt/live/hogehoge.com/ ←ディレクトリ移動 [root@test letsencrypt]# ls cert.pem←サーバ証明書 chain.pem←中間証明書 fullchain.pem←サーバ証明書+中間証明書 privkey.pem←サーバ秘密鍵
内容が確認できたので、ssl.confの用意とapacheの設定ファイルを修正します。
(Nginxの場合はこちら)
今回はヴァーチャルホストに設定を行います。
[root@test conf.d]# cp ssl.conf ssl.hogehoge.com.conf ←ヴァーチャルドメイン用のssl設定ファイル作成
[root@test conf.d]# vi ssl.conf←おおもとのssl.confファイルを編集
<virtualhost _default_:443>←virtualhostの項目を全て削除
~
</virtualhost>
NameVirtualHost *:443←最後に追記
:wq
[root@test conf.d]# vi ssl.hogehoge.com.conf←コピーしたヴァーチャルホストのssl設定ファイルを編集
先ほどと逆に、ssl.confに残した部分を削除します。
<virtualhost _default_:443>
~
</virtualhost>
の部分だけを残します。
<VirtualHost _default_:443>→<VirtualHost *:443>←_default_を*に変更
DocumentRoot “/var/htdocs/hogehoge.com/httpdocs”←コメントアウトを外して、ドキュメントルートのパスに変更
ServerName hogehoge.com:443←コメントアウトを外して、ドメインに変更
SSLCertificateFile /etc/letsencrypt/live/hogehoge.com/cert.pem←サーバ証明書のパスの変更
SSLCertificateKeyFile /etc/letsencrypt/live/hogehoge.com/privkey.pem←サーバ秘密鍵のパスの変更
SSLCertificateChainFile /etc/letsencrypt/live/hogehoge.com/chain.pem←コメントアウトを外して中間証明書のパスの変更
:wq←保存して終了
次にApacheの設定ファイルを編集します。
[root@test conf.d]# vi /etc/httpd/conf.d/hogehoge.com.conf ←Apacheの設定ファイルを編集(今回はヴァーチャルホストの設定ファイルを変更します。)
下記のように追記します。
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/hogehoge.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hogehoge.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/hogehoge.com/chain.pem
ServerAdmin root@hogehoge.com←管理者メールアドレス
DocumentRoot /var/htdocs/hogehoge.com/httpdocs←ドキュメントルートのパス
ServerName hogehoge.com←ドメイン
</VirtualHost>
:wq
[root@test conf.d]# httpd -t←設定ファイルテスト
Syntax OK←設定が間違っていると、Errorや、「 [warn] _default_ VirtualHost overlap on port 443, the first has precedence」等のWarningが出ます。
[root@test conf.d]# service httpd start←Apache起動
https://でアクセスしてみてください。
自動更新の設定
Let’s Encryptは3ヶ月の期限があるので、期限を迎える前に更新する必要があります。
cronで毎月自動更新するように設定しておくと便利です。
cronに設定する際は、更新の前にApacheの停止をして、更新後に再度Apacheを開始する必要があります。
[root@test]# crontab -e←cron設定画面
service service httpd stop && /home/test/letsencrypt/certbot-auto renew --force-renew && service httpd start
これで毎月1日に更新されるように設定されます。