LoginSignup
84
109

More than 5 years have passed since last update.

nginxでwordpressを動かすには

Last updated at Posted at 2015-11-24

ちょいとサーバにwordpressを入れてみようかと思ったので、そこでの備忘録として書きます。

まあ、言いたい事はphp-fpm忘れんなよの一言ですが・・・
apacheを使えば無問題なんですが・・・

目次

  • 環境説明
  • ミドルウェアインストール
  • wordpressインストール
  • 各種設定
  • サービス起動
  • 次の目的+参考資料

環境説明

awsにてCentOS 6.5を使っています。

sudo 書くの面倒くさいんで、全てのコマンドはroot権限で実行している想定です。rootへのsu権限もらってない人は、随時sudoつけてやってください。

あと、【】で囲いをつけたところは、各自で適当に置き換えてください。
間違ってもそのまま使わないでくださいね。

ミドルウェアインストール

sedでのレポジトリの設定ファイルの編集は、飛ばしてもらっても構いません。
yumを使う際に、最低限のレポジトリへのアクセスにするためのものです。

nginxをインストールします。

nginxのレポジトリはインストールしたままだと “Stable version” であり、ミッションクリティカルな用途で用いるのでなければ “Mainline version” を用いても問題ないので、それについての手順も含めています。

# yum install http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# sed -i "s/enabled=1/enabled=0/g" /etc/yum.repos.d/nginx.repo
# sed -i "s/nginx.org\/packages\/centos/nginx.org\/packages\/mainline\/centos/" /etc/yum.repos.d/nginx.repo
# yum install nginx --enablerepo=nginx

mysql をインストールします。

今回はver5.6を用います。

# yum install http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
# sed -i "s/enabled=1/enabled=0/g" /etc/yum.repos.d/mysql-community.repo
# yum -y install mysql-community-server --enablerepo=mysql56-community

phpをインストールします。

今回はver5.6を用います。
mysqlと合わせているわけではありませんので、必要があればバージョンアップしても問題ありません。

# yum install http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# yum install php --enablerepo=remi-php56

php のバージョンの確認します。

# php -v

5.6になっているか確認しましょう。

php-fpmをインストールします。

今回、php5.6を用いているためphp56のレポジトリからインストールします。

# yum install php-fpm --enablerepo=remi-php56

wordpressインストール

今回はver 4.3.1を用います。
特に理由が無ければ、”4.3.1” の部分を “latest”に変更して最新をインストールしても大丈夫だと思います。

自分がやったのは以下の通りです。

# cd /usr/local/src/
# wget https://ja.wordpress.org/wordpress-4.3.1-ja.tar.gz
# tar zxvf wordpress-4.3.1-ja.tar.gz

最新版をインストールする場合は以下を使ってください。

# cd /usr/local/src/
# wget https://ja.wordpress.org/wordpress-latest-ja.tar.gz
# tar zxvf wordpress-【インストールしたバージョン】-ja.tar.gz

wordpressのディレクトリを適当な所に移動させます。

# mv wordpress /var/www/

各種設定

mysqlにwordpress用のデータベースを作成します。

まずは起動します。すでに起動している場合はスキップしてください

# service mysqld start

次に、mysqlの初期設定を行います。こちらも、すでに実行済みの方はスキップしてください。
対話式でコマンドライン上に質問が出てくるので、下記のように対応してください。
また、mysqlのrootユーザのパスワードを決めておいてください。

# mysql_secure_installation

Enter current password for root (enter for none): 【ENTERキーを入力】

Set root password? [Y/n] Y
New password:【mysqlのrootユーザのパスワード】
Re-enter new password:【mysqlのrootユーザのパスワード】

Remove anonymous users? [Y/n] Y

Disallow root login remotely? [Y/n] Y 

Remove test database and access to it? [Y/n] Y

Reload privilege tables now? [Y/n] Y

次に、データベースを作成します。
ここでは、wordpressのシステムがmysqlにアクセスする用のデータベースとユーザを作成するため、パスワードを決めておいてください。

# mysql -uroot -p
Enter password: 【mysqlのrootユーザのパスワード】

mysql> create database wp;

mysql> grant all privileges on wp.* to wp@localhost identified by '【mysqlのwpユーザのパスワード】';

mysql> FLUSH PRIVILEGES;

nginxの設定ファイルを編集します。

/wp-config.php はmysqlのパスワードがべた書きされているので、ちゃんと見えなくなるようにしましょう。
http タグ内に下記を記述してください。

    server {
        listen 80;
        server_name 【あなたのサーバのホスト名】;
        index index.php ;
        root /var/www/wordpress ;
        location ~* /wp-config.php {
             deny all;
        }
        location ~ \.php$ {
             root           /var/www/wordpress ;
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             include        fastcgi_params;
        }
    }

編集後は文法チェックを忘れずに。

# service nginx configtest

以下のような出力が出ればOKです。

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful`

php-fpmの設定をします。

設定と言ってもphp-fpmのユーザとグループをnginxに変えるだけです。

# sed -i "s/user = apache/user = nginx/" /etc/php-fpm.d/www.conf
# sed -i "s/group = apache/group = nginx/" /etc/php-fpm.d/www.conf

php-fpmも文法チェックをしましょう。

# service php-fpm configtest

wordpressの設定を変更します。

下記のような記述があるので、それぞれ書き換えていってください。

/var/www/wordpress/wp-config.php
/** WordPress のためのデータベース名 */
define('DB_NAME', 'wp');

/** MySQL データベースのユーザー名 */
define('DB_USER', 'wp');

/** MySQL データベースのパスワード */
define('DB_PASSWORD', '【mysqlのwpユーザのパスワード】');

/** MySQL のホスト名 */
define('DB_HOST', 'localhost');

/** データベースのテーブルを作成する際のデータベースの文字セット */
define('DB_CHARSET', 'utf8');

/** データベースの照合順序 (ほとんどの場合変更する必要はありません) */
define('DB_COLLATE', '');

認証用ユニークキーの部分は、公式がよいサービスを提供してくれているので、下記を削除してそのまま利用しましょう。
https://api.wordpress.org/secret-key/1.1/salt/

define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

サービス起動

mysqlは設定時に起動しましたので、nginxとphp-fpmを起動させましょう。

# service nginx start
# service php-fpm start

サーバ再起動時の自動起動設定もしましょう。

# chkconfig mysqld on
# chkconfig nginx on
# chkconfig php-fpm on

あとは、ブラウザからの作業になります。
それは、以下を参照してください。(手抜き)
http://webkaru.net/linux/wordpress-install-centos/

次の目的+参考資料

次の目的

wordpressでbootstrapが使えるようにしたいので、それが出来るようなテーマでも作成しようかなと。
とは言え、公式のテーマのページがあるので、そこを見て自分が欲しいものが無かったらやろうかと思います。

参照サイト

http://qiita.com/utano320/items/36b6eac2bbd5bb5657f6
http://qiita.com/MiyaseTakurou/items/923c28f7ac60b2ce761a
http://qiita.com/utano320/items/36b6eac2bbd5bb5657f6
http://www.atmarkit.co.jp/ait/articles/1407/24/news003.html

84
109
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
84
109