Let’s Encryptで証明書を発行、httpsアクセスを可能にする (Ubuntu 16.04 + Apache2)
Let’s Encrypt(レッツ・エンクリプト)とは SSL/TLS の暗号化通信に用いる証明書の認証局(CA; Certificate Authority)の1つです。世界中には様々な認証局がありますが、Let’s Encrypt には次の特長があります。
- 自由に使える
- 証明書の署名は自動的
- オープン
Let’s Encryptの運営母体は電子フロンティア財団(EFF; Electronic Frontier Foundation)であり、1990年に設立されています。2009 年に制定された長期的なミッションが「ウェブの暗号化(Encrypting the web)」でした。安全ではない平文の HTTP 通信を、すべて暗号化した HTTPS に置き換えようという野心的な目標が掲げられました。
- httpsで通信を行うため、ルーターにport 443のポートフォワードの設定をしておきます。
WAN 443 → LAN 443 サーバーIP - iptablesもport 443が通るようにしておきます。
- /sbin/iptables -I INPUT 5 -p tcp -m tcp –dport 80 -j ACCEPT
- /sbin/iptables -I INPUT 6 -p tcp -m tcp –dport 443 -j ACCEPT ←追加して、設定を保存します。
- gitのインストールとletsencryptのclone
現時点でubuntu用のパッケージは無いので、gitをインストールしてGitHubからcloneします。
apt-get install -y git
GitHub から clone します。
cd /opt
git clone https://github.com/letsencrypt/letsencrypt - ヘルプを表示してみます。
cd letsencrypt/
./letsencrypt-auto –help
以下のようにヘルプが表示されれば OK です。
# ./letsencrypt-auto –help
(省略)
letsencrypt-auto [SUBCOMMAND] [options] [-d domain] [-d domain] …Certbot 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:
(省略) - 証明書を発行します。
Webサーバが起動していると以下のように失敗してしまいます。
The program apache2 (process ID 19478) 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.
Apacheを停止します。←port 80を解放します。
systemctl stop apache2
証明書を発行します。証明するドメインは「www.example.com」とします。
./letsencrypt-auto certonly -a standalone -d www.example.com
ドメインや連絡用のメールアドレスなどを登録、証明書が発行されると以下のメッセージが表示されます。
(省略)
IMPORTANT NOTES:
– Congratulations! Your certificate and chain have been saved at ←おめでとうメッセージ
/etc/letsencrypt/live/www.example.com/fullchain.pem. Your cert
will expire on 2017-08-09. To obtain a new or tweaked version of
this certificate in the future, simply run letsencrypt-auto again.
To non-interactively renew *all* of your certificates, run
“letsencrypt-auto 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
このように表示されればOKです。
発行された証明書は /etc/letsencrypt/live/www.example.com/ 以下に保存されています。
ls /etc/letsencrypt/live/www.example.com/
cert.pem chain.pem fullchain.pem privkey.pem ←3つあります。 - Apache が証明書を参照出来るように /etc/apache2/sites-available/default-ssl.conf (port 443用のvhost)を以下のように修正します。
DocumentRoot = /home/www/example.com ←ドキュメントルート#SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
#SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
SSLCertificateFile /etc/letsencrypt/live/www.example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/www.example.com/chain.pem<Directory /home/www/example.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted ←grant設定!!
</Directory> - ApacheにSSLの設定をします。
SSL を利用したサイトを有効化しておきます。
a2ensite default-ssl
SSL/TLS を利用するのでモジュールをロードしておきます。
a2enmod ssl
Apacheを起動します。
systemctl start apache2 - https(port 443)でサイトアクセスが出来るか確認します。
https://www.example.com で、鍵マーク付きでサイトが表示されたら、OKです。
※httpsに対応していないブログパーツなどは非表示になります。 - メールアドレスの確認通知
翌日くらいに、以下の内容で登録したメールアドレス宛に確認通知が来ますので、リンクをクリックして確認完了です。
Hi there,
It looks like you signed up for the Electronic Frontier Foundation mailing list while using our Certbot tool. Before you start getting email from us, we need you to confirm your email address.
Click this link to confirm your email:
https://supporters.eff.org/subscription-confirmation/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ←リンクをクリックします。
If you don’t confirm your email, you won’t be subscribed to our email list. It’s that simple.
The Electronic Frontier Foundation is a nonprofit based in San Francisco that focuses on how new technologies affect our civil liberties. In our mailings, you’ll learn about free speech, privacy, innovation, and the law. Read more about our work at https://eff.org.
You can also learn about our work by following us on social media:
Twitter: https://twitter.com/eff
Facebook: https://www.facebook.com/eff
Google+: https://plus.google.com/+eff
Thanks for your interest in digital rights,
Rainey Reitman
EFF Activism Director
Support EFF projects like Certbot! - .htaccessにhttp→httpsの転送設定を追加
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L] - 証明書の自動更新設定
Let’s Encryptの証明書の有効期間は3ヶ月と短いため、更新を忘れないようcrontabに更新ジョブを登録します。
crontab -e
30 3 15 */2 * /opt/letsencrypt/letsencrypt-auto certonly –text –renew-by-default –webroot -w /home/www/example.com -d www.example.com > /var/log/letsencrypt/renew-cert.log 2>1 && service apache2 restart
※奇数月の15日3時30分に更新処理が行われます。
以上です。
(Visited 351 times, 1 visits today)