Ubuntu10.04 で、EC-CUBE 2.4.4 と WordPress 3.0.1 が動いているという、今となってはかなり特殊な(レガシーな)環境で動作しているサイトの常時SSL化をいたします。(備忘録)

1.WordPressのデータダンプ

# mysqldump -u root -ppassword wordpress-db > wordpress-db-dump20180918.sql

2.データダンプの書き換え(http ⇒ https)

# 画像ファイルは、http://hoge.com/??? を取る
# URLは書き換え
# wp_options は文字長が入っているので要注意

3.Let’s Encrypt (dehydrated)を入れる

# curl -kLO https://github.com/lukas2511/dehydrated/releases/download/v0.6.2/dehydrated-0.6.2.tar.gz
# tar xf dehydrated-0.6.2.tar.gz
# cd dehydrated-0.6.2
# cp docs/examples/config config
# nano config
WELLKNOWN=”/var/www/dehydrated” #コメントを取る
# mkdir /var/www/dehydrated
# nano /etc/apache2/conf.d/dehydrated.conf
Alias /.well-known/acme-challenge /var/www/dehydrated


# nano domains.txt
hoge.com www.hoge.com
# ./dehydrated –register –accept-terms
# ./dehydrated -c –accept-terms
# crontab -e
0 3 1,15 * * /root/dehydrated-0.6.2/dehydrated –config /root/dehydrated-0.6.2/config –cron >> /tmp/ssl-update.log 2>&1

4.apache2 への組み込み

# nano /etc/apache2/sites-available/default-ssl

NameVirtualhost *:443

ServerName hoge.com
ServerAlias www.hoge.com
DocumentRoot /www/wordpress/
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/ssl_access.log combined
SSLEngine on
SSLCertificateFile /root/dehydrated-0.6.2/certs/hoge.com/fullchain.pem
SSLCertificateKeyFile /root/dehydrated-0.6.2/certs/hoge.com/privkey.pem
SSLCertificateChainFile /root/dehydrated-0.6.2/certs/hoge.com/chain.pem

# a2enmod ssl # sslモジュールを入れる
# a2ensite default-ssl
# service apache2 restart
# netstat -ltup4 # httpsが上がっていることを確認

5..htaccess の書き換え(http ⇒ https)

# nano /www/wordpress/.htaccess
RewriteEngine On
Options +FollowSymLinks
RewriteBase /
#RewriteRule ^index\.php$ – [L]
RewriteCond %{THE_REQUEST} ^.*/index.(html|php)
RewriteRule ^(.*)index.(html|php)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(hoge\.com)$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ – [L]
RewriteRule . index.php [L]

6.データダンプからデータベースを書き換え

# mysql -u root -p
use wordpress-db;
source db1.sql; # httpを書き換えたダンプ(UTFで保存)

7.WordPressテーマの書き換え

httpが直書きされてそうなファイル
# cd /www/wordpress/wp-content/themes/hoge/
functions.php
header.php
loop-index.php
style.css

8.EC-CUBEの書き換え

# nano /home/www/hoge/eccube-2.4.4/data/install.php
define (‘SITE_URL’, ‘https://shop.hoge.com/’);
define (‘SSL_URL’, ‘https://shop.hoge.com/’);

# httpが直書きされてそうなファイル
# nano /home/www/hoge/eccube-2.4.4/html/user_data/include/preview/bloc_preview.tpl
# nano /home/www/hoge/eccube-2.4.4/html/user_data/include/preview/footer.tpl
# nano /home/www/hoge/eccube-2.4.4/html/user_data/include/preview/header.tpl
# nano /home/www/hoge/eccube-2.4.4/html/user_data/packages/default/footer.tpl
# nano /home/www/hoge/eccube-2.4.4/html/user_data/packages/default/header.tpl
# nano /home/www/hoge/eccube-2.4.4/html/user_data/packages/default/bloc/optional.tpl
# nano /home/www/hoge/eccube-2.4.4/html/user_data/packages/default/bloc/other_menu.tpl
# nano /home/www/hoge/eccube-2.4.4/data/Smarty/templates/default/site_frame.tpl
# nano /home/www/hoge/eccube-2.4.4/data/Smarty/templates/default/detail.tpl
# nano /home/www/hoge/eccube-2.4.4/data/Smarty/templates/default/bloc/category.tpl

10.eth0がリネームされ、ネットワークが使えない時

# ifconfig
lo しか有効になっておらず、ネットワークアクセスできない。
# nano /etc/network/interfaces
eth0_rename になってた
# rm /etc/udev/rules.d/70-persistent-.rules
# reboot
これで直った。リネームされた原因不明

11.PostgreSQLが壊れていた場合(pg_xlog不整合でサービスが起動せず)

# su postgres
# cd /usr/lib/postgresql/8.4/bin
# ./pg_resetxlog -f /var/lib/postgresql/8.4/main/
# ./pg_ctl -D /var/lib/postgresql/8.4/main start
これで一応直る(でもDBがなくなってる)
# ./pg_ctl -D /var/lib/postgresql/8.4/main stop

# いろいろやったが結局、PostgreSQL を止めた状態で rsyncで正常に動いているサーバから丸ごと転送かけた
# rsync -rtlzvogpHAX –rsync-path=”/usr/bin/rsync” –block-size=4096 –delete -e “ssh -p 22” /var/lib/postgresql/8.4/ root@1.1.1.4:/var/lib/postgresql/8.4/

12.さくらのVPSでテスト環境を作る

12-1.OSはISOからインストールする
  ここ(http://old-releases.ubuntu.com/releases/10.04.0/)からダウンロード
  ubuntu-10.04.4-server-amd64.iso

12-2.sftpでサーバーへISOイメージをアップロードする
  コントロールパネルから「OSインストール」をクリックして「ISOイメージインストール」を選択
  「SFTPアカウントの作成を行う」を選択
  FTPで、アップロード

12-3.IPアドレスの設定
  Ubuntuの設定で、IPアドレスを設定します
  コントロールパネルの情報を転記(IPアドレス、ネットワークマスク、ゲートウェイ、DNSサーバー)
  あとで設定は上書きするので、「OpenSSH server」だけ選択

12-4.ログイン

12-5.rootのパスワードを設定
$ sudo passwd root
$ su –
#

12-6.visudo してsudo時にパスワードを聞かれないよう変更
# visudo
%sudo ALL=(ALL:ALL) NOPASSWD: ALL

12-7./etc/ssh/sshd_config を編集
# nano /etc/ssh/sshd_config
Port 22xx
PasswordAuthentication no

12-8.sshサーバを再起動
# service ssh restart

12-9.認証キーを作る
# cd
# mkdir .ssh
# nano .ssh/authorized_keys
# cp -a .ssh/ /home/myuser
# chown -R myuser:myuser /home/myuser/.ssh/

12-10.teratermからsshで入りなおす