スポンサーリンク

さくらVPS内のwordpressにSSL証明書(Let’s Encrypt)を導入

Let’s Encryptを当wordpress環境に導入したときのメモです。

あくまで自分の環境での導入時の操作になるので、ほかの環境でうまくいく保障はないのと、本番環境で実施する前にVM環境やクローン環境で動作検証をしてから実施すると良いと思います。

以下の記事を参考に導入しました。
CentOS6 で Let’s Encrypt #letsencrypt

Let’s Encryptを導入するのにpython2.7が必要とのことなのでインストール。既存の環境を壊さないためにCentOSのSCLという仕組みを使用。
[bash]
# yum install centos-release-SCL
# yum install python27 phthon27-python-tools
[/bash]

sclコマンドでpython2.7を有効化したところエラーが出るようになってしまいました。エラーで出ているファイルをfindしてシンボリックリンクを設定
[bash]
# python -V
Python 2.6.6

# scl enable python27 bash
# python -V
python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

# cd /lib64
# ln -s /opt/rh/python27/root/usr/lib64/libpython2.7.so.1.0 libpython2.7.so.1.0
# python -V
Python 2.7.8
[/bash]

letencryptのクライアントソフトを導入して証明書を発行
[bash]
# cd /usr/local/src/
# git clone https://github.com/certbot/certbot
# cd certbot
# /etc/init.d/httpd stop
# ./certbot-auto –help
# ./certbot-auto certonly –standalone -d blog.imo-tikuwa.com
[/bash]
証明書の発行を行う前にapacheを停止しておく必要あり。
現在はクライアント名がletsencryptからcertbotに変更されたとのこと。
gitでチェックアウトしたリポジトリ内を見ると同じファイルサイズのletsencrypt-auto、certbot-autoというスクリプトが見つかりますが、中身は一緒でどっちでも動作しました。

初回の./certbot-auto –helpで必要なライブラリがインストールされた後、ヘルプが表示されました。

https://letsencrypt.jp/faq/#Wildcard
↑ワイルドカード証明書は現状は未対応とのこと。

証明書が正常に生成されたメッセージが表示されると/etc/letsencrypt/の中に色々出来上がりました。
現行の証明書は/etc/letsencrypt/live/[発行時に入力したドメイン名]/にシンボリックリンクで設定されているのであとはそれをssl.confで設定すればOKでした。


httpd.confを編集(mod_rewriteを有効にし、httpsへリダイレクトするよう設定)
[xml]
<VirtualHost *:80>
ServerName blog.imo-tikuwa.com
DocumentRoot /var/www/html/wordpress/
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# <Directory /var/www/html/wordpress/>
# AllowOverride All
# Options FollowSymLinks
# Order allow,deny
# Allow from all
# </Directory>
</VirtualHost>
[/xml]

ssl.confを編集
[xml]
<VirtualHost *:443>
DocumentRoot /var/www/html/wordpress/
ServerName blog.imo-tikuwa.com
SetEnv HTTPS on
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/blog.imo-tikuwa.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/blog.imo-tikuwa.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/blog.imo-tikuwa.com/chain.pem
<Directory /var/www/html/wordpress/>
AllowOverride All
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
[/xml]


wordpressでhttpsのときに混在コンテンツの読み込みのアラートが表示されてしまうためプログラムを改修。
[bash]
# vi /var/www/wp/webmemo/wp-content/themes/simplicity/functions.php
[/bash]

以下のサイトのスクリプトを追加
http://qiita.com/white_aspara25/items/f514def597d7de19b5e7

simplicityというwordpressテーマのfunctions.phpを編集
[php]
// http => https —————————————————–
function fix_ssl_attachment_url( $url ) {
if( is_ssl() ){
$url = preg_replace(‘/^http:/’, ‘https:’, $url);
}
return $url;
}
add_filter(‘wp_get_attachment_url’, ‘fix_ssl_attachment_url’);
[/php]

記事内の画像リンクがhttpで始まってしまっているので一括で記事内の画像リンクを置き換え
[sql]
# mysql wordpress
> update wp_posts set post_content = replace(post_content, ‘https://blog.imo-tikuwa.com/webmemo/wp-content/uploads’, ‘//blog.imo-tikuwa.com/webmemo/wp-content/uploads’);
[/sql]


Let’s Encryptの証明書は期限が3ヶ月のため、2ヶ月毎に証明書を更新するシェルを作成してcronに登録。
[bash]
# SCLを有効化
scl enable python27 bash

# httpdを停止
/etc/init.d/httpd stop

# 証明書を更新
/usr/local/src/letsencrypt/letsencrypt-auto renew –force-renew

# httpdを開始
/etc/init.d/httpd start
[/bash]
上記のようなスクリプトとなりましたが、SCLでpython2.7を有効化しなくても証明書の更新ができました。
python2.7がうまくインストールできてないあたりが関係しているのか、対処として行った操作に問題があるのかも。

2ヶ月毎の月初に証明書の更新を実施
* * 1 1-12/2 * /root/sh/letsencrypt_renew.sh >> /root/sh/letsencrypt_renew.log

タイトルとURLをコピーしました