tasukentのブログ

Objective-c、PHP、Javaとか色々試してアウトプットしていきます。

Nginx+MySQL+PHP+WordpressをCentOS6.4にセットアップ

phpインストール

環境OS:CentOS 6.4 64bit Plain まずはすべてのファイルをアップデートしてPHPをインストールする

$yum update -y
$yum -y install php-mysql php-common php php-cgi php-fpm php-gd php-mbstring

完了したらインストールされているかチェック

$ php -v 
PHP 5.3.3 (cli) (built: Aug 6 2014 05:54:27) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

PHP-FPM(FastCGI Process Manager)の設定

www.confを編集します

$cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.back
$vim /etc/php-fpm.d/www.conf
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
;user = apache //nginxに変更
user = nginx
; RPM: Keep a group allowed to write in log dir.
;group = apache //nginxに変更
group = nginx 

mysqlをインストール

完了したらインストールされているか確認

$yum install mysql mysql-server -y
$mysql --version 
mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1

mysqlデーモンを起動

$/etc/init.d/mysqld start 
MySQL データベースを初期化中: Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

 [ OK ]
mysqld を起動中: [ OK ]

問題がなければ自動起動設定

$chkconfig mysqld on 

mysqlをセットアップ

設定を新規ファイルに書き出す

$ vim ~/work/wordpress.sql
set password for root@localhost=password('ここにrootのパスワード');
insert into user set user="ここに新規ユーザ名", password=password("新規ユーザのパスワード"), host="localhost";
/* wprdpress用にwordpressというデータベースを作る。名前は自由だが、後で使うのでメモる
create database wordpress;
grant all on wordpress.* to ユーザ名;
/* 上で宣言した新規ユーザにデータベースwordpressへの全権限を与える
FLUSH PRIVILEGES;

msysqlに流し込む

$ mysql -u root -D mysql < work/wordpress.sql 

設定が反映されているか確認する。 まず、rootでログインできる事を確認

$ mysql -p
Enter password: 設定したrootのパスワード
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit 
Bye

新規DBヘ新規ユーザでログインできる事を確認

$ mysql -u 新規ユーザ名 -D DB名 -p
Enter password: 新規ユーザのパスワード
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit 
Bye

niginxをインストールする

$ rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
$ yum install nginx -y

nginxの設定を行う

ドキュメントルートを変更し、phpの設定も追加する

$ vim /etc/nginx/conf.d/default.conf

 location / {
 #root /usr/share/nginx/html;
 root /var/www/html;
 index index.html index.htm;
 }


location ~ \.php$ {
 # root html;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
 include fastcgi_params;
 }

php-fpmのデーモンを起動する

$ /etc/init.d/php-fpm start 
php-fpm を起動中: [ OK ]
nginxのデーモンを起動する

$ /etc/init.d/nginx start 
nginx を起動中: [ OK ]
デーモンを起動し、自動起動設定

$ chkconfig php-fpm on
$ chkconfig nginx on

適当にphpファイルをドキュメントルートに設置して動作確認。 問題なければWordPressをインストールをします。

WordPressをインストール

$ wget http://ja.wordpress.org/wordpress-3.9.2-ja.zip

変更したドキュメントルートへ移動して解凍する。 ドキュメントルートに余計なファイルがあると上手く行かないので削除してから行う方が良い。*1

$ mv wordpress-0.9.2-ja.zip /var/www/html/.
$ cd /var/www/html/
$ unzip -qq wordpress-3.9.2-ja.zip 
$ mv wordpress/* .
$ rm -fR wordpress

あとはブラウザからアクセスして画面上で設定すれば完了。

*1:ログイン画面したのちに403エラー、TOPページも表示されませんでした。DBのテーブルをすべて削除して、ドキュメントルートのファイルをすべて消してからWordPressを再インストールしたら上手くいきました