CentOS7にwordpressを移行

前の記事で初期設定を行ったサーバにwordpressを移設します。
wordpressの移設は案外簡単で、以下の手順で完了します。
・移設元サーバのwordpressのアプリケーションルートをコピー
・移設元サーバのDBからダンプ取得
・二つを新サーバに配置/取り込み

元サーバデータを取得

まずはアプリそのものの圧縮とダンプ取得を行います。

# mysqldump -u root -p wordpress > /home/hogehoge/wordpress.dump  
# cd /var/www/public/projects/  
# zip -r /home/hogehoge/wordpress.zip wordpress  

まずはDBのダンプを取得します。上のコマンドではrootユーザにパスワード認証を行い、wordpressという名前のDBのダンプを取得しています。
次はプロジェクトルートへ移動し、wordpressフォルダごとコピーしています。
tarコマンドでも良いですし、zipコマンドでも良いです。

今回はrsyncなどは利用せずに旧サーバ -> クライアント -> 新サーバへ転送しました。
今思えばscpでもrsyncでも良かったですね。
アホですね。

MariaDBの設定

ここから新サーバの設定です。
まずはデータベースを用意します。CentOS7からMySQLの変わりにMariaDBが利用されるようになっています。
微妙に違うらしいのですが基本はMySQLと同じです。

インストールと初期化

[root@metal ~]# yum -y install mariadb mariadb-server  
[root@metal ~]# systemctl enable mariadb //chkconfig mariadb onと同じ  
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'  
[root@metal ~]# systemctl start mariadb  
[root@metal ~]# mysql_secure_installation  

ここまででインストールと初期化が完了です。
systemctl enable mariadbmariadbをサーバ起動時に起動するように設定を行っています。
mysql_secure_installationではrootにパスワードを設定し、他は全てyesで良いです。
不要なDBを削除してくれたりしているだけです。

wordpress用DB作成

続いてwordpress用にDBを作成します。
DB名やユーザは各々好きに設定してください。
以下はサンプル。

[root@metal ~]# mysql -u root -p  
MariaDB [(none)]> create database wordpress;  
Query OK, 1 row affected (0.01 sec)  
  
# wordpressユーザがローカルホストからパスワード******でwordpressDBに対して全ての操作が出来るようにユーザを作成しつつ設定。  
MariaDB [(none)]> grant all privileges on wordpress.* to wordpress@localhost identified by '********';  
Query OK, 0 rows affected (0.01 sec)  
  
MariaDB [(none)]> flush privileges;  
Query OK, 0 rows affected (0.00 sec)  
  
MariaDB [mysql]> quit  
Bye  

これでDBの作成が完了です。

MariaDB設定

次にMariaDB自体の設定を行います。
今回は特に大したことはせず、文字コードの指定を行っています。ただ動かしている分にはこの程度でも十分だと思います。(まだそんなちゃんと見れていないけど)

[root@metal ~]# cp /etc/my.cnf /etc/my.cnf.org  
[root@metal ~]# vi /etc/my.cnf  
[root@metal ~]# diff /etc/my.cnf.org /etc/my.cnf  
0a1,3  
> [client]  
> default-character-set = utf8  
>   
9a13,14  
> character-set-server  = utf8  
> default-storage-engine  = innodb  
19a25,26  
> [mysql]  
> default-character-set=utf8  
[root@metal ~]# systemctl restart mariadb  
[root@metal ~]# systemctl status mariadb  

文字コードの指定は各々の環境に合わせてください。
wordpressutf-8をデフォで利用していると思います。

ダンプの復元

ダンプデータからデータを復元します。

[root@metal ~]# mysql -u wordpress -p wordpress < /home/hogehoge/wordpress.dump  
Enter password:   
[root@metal ~]# mysql -u root -p  
MariaDB [(none)]> use wordpress  
Reading table information for completion of table and column names  
You can turn off this feature to get a quicker startup with -A  
  
Database changed  
MariaDB [wordpress]> show tables;  
+-----------------------+  
| Tables_in_wordpress   |  
+-----------------------+  
| wp_blc_filters        |  
| wp_blc_instances      |  
| wp_blc_links          |  
| wp_blc_synch          |  
| wp_commentmeta        |  
| wp_comments           |  
| wp_ewwwio_images      |  
| wp_links              |  
| wp_options            |  
| wp_postmeta           |  
| wp_posts              |  
| wp_term_relationships |  
| wp_term_taxonomy      |  
| wp_terms              |  
| wp_usermeta           |  
| wp_users              |  
+-----------------------+  
16 rows in set (0.00 sec)  
  
MariaDB [wordpress]> quit  
Bye  

ダンプから復元しつつデータの確認。
ワードプレスってあんまりテーブルの数ないんですね。

以上でMariaDBの準備は完了です。

Apache2.4の設定

続いてアプリを乗せるapacheの設定を行います。
CentOS6以下ではyumで2.2系が入っていましたが、CentOS7からは2.4系です。

apacheのインストール

まずはインストールを行い、起動設定を行います。

[root@metal ~]# yum install -y httpd mod_ssl  
[root@metal ~]# systemctl is-enabled httpd  
disabled  
[root@metal ~]# systemctl enable httpd  
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'  
[root@metal ~]# systemctl is-enabled httpd  
enabled  

apacheの設定

次にapacheの設定を変更します。

[root@heavy-metal-explorer ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org  
[root@metal ~]# diff /etc/httpd/conf/httpd.conf.org /etc/httpd/conf/httpd.conf  
86c86,87  
< ServerAdmin root@localhost  
---  
> #ServerAdmin root@localhost  
> ServerAdmin admin@heavy-metal-explorer.com  
95a97,101  
> ServerName heavy-metal-explorer.com:80  
  
131c137  
< <Directory "/var/www/html">  
---  
> #<Directory "/var/www/html">  
144c150  
<     Options Indexes FollowSymLinks  
---  
> #    Options FollowSymLinks  
151c157  
<     AllowOverride None  
---  
> #    AllowOverride None  
156,157c162,163  
<     Require all granted  
< </Directory>  
---  
> #    Require all granted  
> #</Directory>  
164c170  
<     DirectoryIndex index.html  
---  
>     DirectoryIndex index.html index.php  
190a197,202  
> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined  
> LogFormat "%h %l %u %t \"%r\" %>s %b" common  
> LogFormat "%{Referer}i -> %U" referer  
> LogFormat "%{User-agent}i" agent  
> CustomLog logs/access_log combined  
>   
247c259  
<     ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"  
---  
>     #ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"  
255,259c267,271  
< <Directory "/var/www/cgi-bin">  
<     AllowOverride None  
<     Options None  
<     Require all granted  
< </Directory>  
---  
> #<Directory "/var/www/cgi-bin">  
> #    AllowOverride None  
> #    Options None  
> #    Require all granted  
> #</Directory>  
316c328  
< AddDefaultCharset UTF-8  
---  
> #AddDefaultCharset UTF-8  

<2015/11/20 追記>
非常に大切な設定を忘れていたという致命的なミスに気づきました。

353a366,369  
>   
> ServerTokens Prod  
> ServerSignature Off  
> TraceEnable Off  

これを設定することで404ページなどにサーバ情報が表示されることを防ぎます。
忘れてはいけない設定ですね!(忘れてました!)

長々と表示されていますが、利用しないDirectory設定を無効化しているのとindex.phpをdirectory indexに追加している程度です。
次に簡単なセキュリティ設定を以下で行います。

[root@metal ~]# cat /etc/httpd/conf.d/security.conf  
# Hide Apache Version  
ServerTokens Prod  
ServerSignature off  
  
# Hide Header X-Powered-By  
Header always unset X-Powered-By  
  
# Anti POODLE  
SSLProtocol All -SSLv3  
[root@metal ~]# rm /etc/httpd/conf.d/welcome.conf   
rm: remove regular file ‘/etc/httpd/conf.d/welcome.conf’? y  
[root@metal ~]# apachectl configtest  
Syntax OK  

apacheの情報や、phpの情報を表示しないように設定しているのが上3つです。
sslの部分は今年やたらと発生しているssl系障害用の対応です。
こちらから参考(流用)させていただきました。

wordpress用virtual設定

wordpress用の設定ファイルを作成します。

[root@metal ~]# vi /etc/httpd/conf.d/virtual-wordpress.conf   
[root@metal ~]# cat /etc/httpd/conf.d/virtual-wordpress.conf   
<VirtualHost *:80>  
  ServerName heavy-metal-explorer.com:80  
  DocumentRoot /var/www/public/projects/wordpress  
    
  <Directory /var/www/public/projects/wordpress>  
    AllowOverride All  
    Options FollowSymLinks  
  </Directory>  
</VirtualHost>  
  
<VirtualHost *:443>  
  ServerName heavy-metal-explorer.com:443  
  
  DocumentRoot /var/www/public/projects/wordpress  
    
  <Directory /var/www/public/projects/wordpress>  
    AllowOverride All  
  </Directory>  
  
  ErrorLog logs/wordpress_ssl_error_log  
  TransferLog logs/wordpress_ssl_access_log  
  LogLevel warn  
  
  # SSL系の設定  
</VirtualHost>   
  
[root@metal ~]# cp /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.org  
[root@metal ~]# vi /etc/httpd/conf.d/ssl.conf  
[root@metal ~]# diff /etc/httpd/conf.d/ssl.conf.org /etc/httpd/conf.d/ssl.conf  
# 省略  

ここではwordpress用に設定をしています。
ssl系の設定はssl.confから必要な部分を抜き出しているだけになります。
またssl.confに設定されているvirtualホスト設定を抜き出して設定しているためssl.confの不要になった設定をまるっと削除しています。

鍵の設置

SSL用の鍵を設置します。これは旧サーバから持ってきました。
各々鍵ファイルをssl設定を行った位置に起きましょう。
/etc/pki/tlsの下にフォルダ切っても良いですし、デフォの位置においても良いです。

アプリ設置

設定が完了したらアプリの配置です。
ディレクトリ構成などはご自由に。

[root@metal ~]# mkdir -p /var/www/public/projects  
[root@metal ~]# cp /home/hogehoge/wordpress.zip /var/www/public/projects/  
[root@metal ~]# cd/var/www/public/projects  
[root@metal projects]# unzip  
[root@metal projects]# rm wordpress.zip  
[root@metal projects]# chown apache:apache -R /var/www/public  
[root@metal projects]# cd  

上記では転送してきたファイルを所定の位置へ展開しています。
展開したファイルは権限が正しくないため、apacheの管理下に権限を変更しています。
ディレクトリは755, ファイルは644という部分は圧縮しても保持しているようでしたのでそのままにしています。

これで一通りのapache設定が完了です。

PHP5.6系インストール

次にwordpressを動かすためにphpをインストールします。
現在最新が5.6系なのでそれを入れましょう。5.6系上でもwordpressは動きます。

[root@metal ~]# rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm  
[root@metal ~]# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm  
[root@metal ~]# yum install -y --enablerepo=remi --enablerepo=remi-php56 php php-mcrypt php-mbstring php-pdo php-mysqlnd php-tokenizer php-xml  
[root@metal ~]# php -v   
PHP 5.6.13 (cli) (built: Sep  3 2015 14:08:58)   
Copyright (c) 1997-2015 The PHP Group  
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies  
  
[root@metal ~]# mkdir /var/log/php  
[root@metal ~]# chmod 755 /var/log/php  
[root@metal ~]# cp /etc/php.ini /etc/php.ini.org  
[root@metal ~]# vi /etc/php.ini  
[root@metal ~]# diff /etc/php.ini.org /etc/php.ini  
577a578  
> error_log = /var/log/php/error.log  
889a891  
> date.timezone = "Asia/Tokyo"  
[root@metal ~]# systemctl restart httpd  

php5.6系はそのままでは入らないのでリポジトリを追加しています。
ついでにいくつか必要そうなパッケージもインストールしていますが、おそらく不要なものもあります。
上記はlaravel5を動かすのに必要なパッケージたちです。

phpをインストールしたら簡単にphp.iniを修正して、apacheを再起動します。
php.iniの変更はapacheの再起動を行わないと反映されないようです。

firewall設定

最後にポートの解放を行います。
このままでは誰も外部からアクセスできませんからね。

[root@metal ~]# firewall-cmd --add-service=http --permanent  
success  
[root@metal ~]# firewall-cmd --add-service=https --permanent  
success  
[root@metal ~]# firewall-cmd --reload  
success  

http, httpsのサービスを追加します。
各々80 443のポートの設定になります。

これにて移行完了です。

あとがき

apache, mariadbあたりの設定手順を乗せるのは自分の設定を後悔するのとほぼ同義なので怖いですね。
上記の設定では実際行っている設定のうち細かい設定は省いていますが、基本こんな感じだと思います。

みなさんもサーバを設定する際には、手順を残すようにしましょう。
手順の蓄積=ノウハウの蓄積です。というか、ないと忘れます。
サーバのことなんて全部覚えてられないので、諦めてそのときに記録しましょう。