ec2インスタンスにWordPressをインストールする

2012.03.13

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

今回の記事も前回から引き続いております。

WordPressで管理されているコンテンツをあるサーバソフトウェアで意図した通りに変換できるか?を検証したいと思います。

まずはEC2インスタンス上にWordPressをインストールしなければなりませんが、その前に以下のサーバプログラムが必要になりますので、事前にセットアップすることにします。

  • Apache(httpd)のインストール
  • MySQLのインストール
  • PHPのインストール

Apacheについては既にインストール済なので、未インストールの方はこちらを参考にしてください。

それではyumでMySQLのインストールを行い、その後viで/etc/my.cnfを編集します。

yum –y install mysql-server
vi /etc/my.cnf

viでファイルを開いたら、以下の項目を追加します。

default-character-set = utf8
skip-character-set-client-handshake
character-set-server = utf8
collation-server = utf8_general_ci
init-connect = SET NAMES utf8

[client] 
default-character-set = utf8 

[mysqldump] 
default-character-set = utf8 

[mysql] 
default-character-set = utf8

mysqlの起動及び自動起動設定を行います。

/etc/rc.d/init.d/mysqld start
chkconfig mysqld on

次にmysql_secure_installationコマンドを使ってmysqlの設定を行います。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):    ←rootのパスワードを設定します。Enterを押下
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n]
New password:    ←rootパスワードを入力
Re-enter new password:    ←rootパスワードを再入力
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]     ←匿名ユーザの削除?[Y/n] Enterを押下
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]      ←リモートホストからのrootログイン禁止?[Y/n] Enterを押下
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]       ←testデータベースを削除する?[Y/n] Enterを押下
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]        ←ユーザ権限が保存されているテーブル(mysqlデータベース内にある)をリロードする?[Y/n] Enterを押下
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

次にPHPのインストールを行います。(日本語を利用するためのモジュール、MySQLを利用するためのモジュール、暗号・復号化に関する関数を利用するためのモジュールも合わせてインストールしておきます。)

yum -y install php php-mbstring php-mysql php-mcrypt

次にApacheのconfファイルを編集して、PHPが利用できるように設定します。

vi /usr/local/apache2/conf/httpd.conf

LoadModuleでPHP5の組み込みを行う必要がありますので、下記の行を追加します。

LoadModule php5_module        /usr/lib64/httpd/modules/libphp5.so ←64Bit版なので32Bit版の場合はパスが異なります。

拡張子が".php"のファイルをPHPスクリプトだと認識するように以下の行を追加します。

AddType application/x-httpd-php .php

Apacheの停止・起動を行い、phpが起動しているかのサンプルファイルを作ります。

/usr/local/apache2/bin/apachectl stop
/usr/local/apache2/bin/apachectl start

echo "<?php phpinfo(); ?>" > /usr/local/apache2/htdocs/phpinfo.php ←PHP動作確認用ファイル

http://xxx.xxx.xxx.xxx/phpinfo.phpにアクセスしてPHPの設定が表示されるphpinfo()ページが表示されれば動作確認は完了です。

それではWordPressのインストールを行います。どうせインストールするなら最新版のWordPressをインストールしたいと思います。(latest-ja.tar.gz)

wget http://ja.wordpress.org/latest-ja.tar.gz
tar xvfz latest-ja.tar.gz
mv wordpress /usr/local/apache2/htdocs
chown –R apache:apache /usr/local/apache2/htdocs/wordpress/

touch /etc/httpd/conf.d/wordpress.conf
vi /etc/httpd/conf.d/wordpress.conf

以下の行をwordpress.confに追加します。

Alias /wordpress /usr/local/apache2/htdocs/wordpress

/usr/local/apache2/bin/apachectl stop
/usr/local/apache2/bin/apachectl start

mysqlにログインして、wordpressと言うデータベースを作成します。

mysql -uroot -p    ←MySQLにログイン
Enter password:    ←mysql_secure_installationコマンドで設定したパスワードを入力

mysql> create database wordpress; ←WordPress という名前のデータベースを作成
mysql> grant all privileges on wordpress.* to wordpress@localhost identified by 'パスワード'; WordPress用のユーザを作成

WordPressの設定ファイルを作成します。てっとり早くサンプルからコピーして必要な部分を編集します。

cd /usr/local/apache2/htdocs/wordpress
cp wp-config-sample.php wp-config.php
chown apache:apache wp-config.php
vi wp-config.php

viでwp-config.phpを開いたら、以下の部分を編集します。

// 注意:
// Windows の "メモ帳" でこのファイルを編集しないでください !
// 問題なく使えるテキストエディタ
// (http://wpdocs.sourceforge.jp/Codex:%E8%AB%87%E8%A9%B1%E5%AE%A4 参照)
// を使用し、必ず UTF-8 の BOM なし (UTF-8N) で保存してください。

// ** MySQL 設定 - こちらの情報はホスティング先から入手してください。 ** //
/** WordPress のためのデータベース名 */
define('DB_NAME', 'wordpress'); ←database_name_hereからwordpressに置き換えます。

/** MySQL データベースのユーザー名 */ ←username_hereからwordpressに置き換えます。
define('DB_USER', 'wordpress');

/** MySQL データベースのパスワード */ ←password_hereをMySQLで設定したパスワードに置き換えます。
define('DB_PASSWORD', 'パスワード');

最後にWordPressのインストールを行います。

http://xxx.xxx.xxx.xxx/wordpress/ をブラウザで開きます。

ブログのタイトルやメールアドレスを入力し、"WordPressをインストール"というボタンがあると思いますので、それを押すとインストールが実行されます。インストールが成功すると、"成功しました!"という文字とアカウント情報が表示されるので、ログインして記事が投稿できるかを確認します。入力したメールアドレスには、アカウント情報が記載されたメールが届きます。 以上で WordPress のインストールは完了です。

次回は実際にWordPressでコンテンツを登録して、あるサーバソフトウェアで意図した通りにコンテンツ変換できるか?を実際に検証します。