:::: MENU ::::

Let's Encryptで証明書を発行してWordPressをSSL化する

物は試しに証明書もらって当サイトを SSL 化してみました。
以下に手順を載せておきます。

証明書の発行手順

python2.7 が必須みたいなので入れておく

yum で入れてもいいけど、2.6 もソースから入れていたのでソースから入れる。

# wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz
# tar xvfz Python-2.7.11.tgz
# ./configure --prefix=/usr/local/python2.7.11 --enable-shared
# make && make install

# vi /etc/ld.so.conf.d/python2.7.11.conf
# ldconfig
ライブラリの共有は別にいらないかも

# vi ~/.bash_profile
# source ~/.bash_profile
パスはちゃんと通しておく

# python -V

クライアントを取ってきて証明書を発行

# git clone https://github.com/letsencrypt/letsencrypt
# cd letsencrypt
# ./letsencrypt-auto --help
ヘルプでコマンド見ようかと思ったら、yumで必須要素インストールを勝手に始めた……

# ./letsencrypt-auto certonly --webroot -w /var/www/wordpress/ -d 14code.com
Apacheに自動で入れてくれるっぽいオプションあるけど、
なんかこけたら怖いので証明書の発行だけを行う。
GUIでメールアドレスと規約の同意を聞かれるので、
それぞれ入力すれば証明書が発行される

Enter email address (used for urgent notices and lost key recovery)
-> [email protected]

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

IMPORTANT NOTES:
- If you lose your account credentials, you can recover through
e-mails sent to [email protected].
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/14code.com/fullchain.pem. Your cert will
expire on 2016-05-08. 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

# ll /etc/letsencrypt/live/14code.com/
合計 0
lrwxrwxrwx 1 root root 34  2月  8 22:34 2016 cert.pem -> ../../archive/14code.com/cert1.pem
lrwxrwxrwx 1 root root 35  2月  8 22:34 2016 chain.pem -> ../../archive/14code.com/chain1.pem
lrwxrwxrwx 1 root root 39  2月  8 22:34 2016 fullchain.pem -> ../../archive/14code.com/fullchain1.pem
lrwxrwxrwx 1 root root 37  2月  8 22:34 2016 privkey.pem -> ../../archive/14code.com/privkey1.pem

証明書を Apache に設定

# cd /usr/local/apache2/ssl.crt/
# cp -a /etc/letsencrypt/archive/14code.com/cert1.pem ./server.crt
# cp -a /etc/letsencrypt/archive/14code.com/chain1.pem ./server-ca.crt
# cp -a /etc/letsencrypt/archive/14code.com/privkey1.pem ./server.key

# vi ../conf/extra/httpd-ssl.conf
<VirtualHost *:443>
ログの出力場所以外は80のときと同じなので割愛
...
SSLCertificateFile "/usr/local/apache2/conf/ssl.crt/server.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/ssl.crt/server.key"
SSLCertificateChainFile "/usr/local/apache2/conf/ssl.crt/server-ca.crt"

# /usr/local/apache2/bin/apachectl configtest
# /usr/local/apache2/bin/apachectl restart

サイトを https でアクセスできるか確認。
ちゃんと証明書をブラウザが認識しているか確認。

WordPress の設定を https 化

WordPress のベース URL を https にする。
これを変更すれば WordPress 上のだいたいのリンクは https になる。

# vi /var/www/wordpress/wp-config.php
define('WP_SITEURL', 'https://14code.com/blog');
define('WP_HOME', 'https://14code.com/blog');

https サイト内で http で読んでいる物があったらブラウザによってエラーになるので、
それらを見つけて「 http://domain.com 」を「 //domain.com 」で呼ぶように変更する。
見つける方法はブラウザが教えてくれたり grep だったり。

一通りアクセスして問題なければリダイレクト処理を入れる。

# vi /var/www/wordpress/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress

リダイレクトが正常に働いているか確認。
問題なければ GA のクローラーにリクエストを送ったりとか。

証明書発行の自動化

Let's Encrypt の証明書は 3 ヶ月しか有効ではないので、
取得と設置を自動化しないと実用向きではない。

作ってないけど shell 書いて cron で回せばいいかな。