LoginSignup
0

More than 5 years have passed since last update.

nginxとwordpressの連携

Last updated at Posted at 2016-05-15

やりたいこと

nginxとwordpressを連携させてとりあえずhtml表示させたい。
なるべくデフォルト設定にする。

(以下の手順は後からまとめたので再確認はまだしてない)

インストール

nginxを入れる

$ sudo yum install nginx
$ sudo chkconfig --level 345 nginx on 

documentrootは/var/www/htmlとする。
混乱すると嫌なので、デフォルトのほうは消す

$ sudo rm -rf /usr/share/nginx

以下confの設定。
documentrootを上のパスにする。
あとphp-fpmの設定も追加する。

 diff -u /etc/nginx/conf.d/default.conf.org /etc/nginx/conf.d/default.conf  
--- /etc/nginx/conf.d/default.conf.org  2015-06-17 06:34:15.000000000 +0900
+++ /etc/nginx/conf.d/default.conf  2016-05-15 18:26:16.437516988 +0900
@@ -13,20 +13,20 @@
     include /etc/nginx/default.d/*.conf;

     location / {
-        root   /usr/share/nginx/html;
-        index  index.html index.htm;
+        root   /var/www/html;
+        index  index.php index.html index.htm;
     }

     error_page  404              /404.html;
     location = /404.html {
-        root   /usr/share/nginx/html;
+        root   /var/www/html;
     }

     # redirect server error pages to the static page /50x.html
     #
     error_page   500 502 503 504  /50x.html;
     location = /50x.html {
-        root   /usr/share/nginx/html;
+        root   /var/www/html;
     }

     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
@@ -37,13 +37,13 @@

     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
     #
-    #location ~ \.php$ {
-    #    root           html;
-    #    fastcgi_pass   127.0.0.1:9000;
-    #    fastcgi_index  index.php;
-    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
-    #    include        fastcgi_params;
-    #}
+    location ~ \.php$ {
+        root           /var/www/html;
+        fastcgi_pass   127.0.0.1:9000;
+        fastcgi_index  index.php;
+        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
+        include        fastcgi_params;
+    }

     # deny access to .htaccess files, if Apache's document root
     # concurs with nginx's one
[root@ik1-303-11969 nginx]# 

上のfastcgi_paramの箇所はちゃんと$document_rootになってることを確認

php-fpmを入れる

$ sudo yum install php-fpm
$ sudo chkconfig --level 345 php-fpm on

confの設定はデフォルトでapacheの箇所をnginxにするだけ。

$ sudo vi /etc/php-fpm.d/www.conf
$ cat /etc/php-fpm.d/www.conf
[www]
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
user  = nginx
group = nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session

mysqlを入れる

$ sudo yum install mysql-server
$ sudo /sbin/chkconfig --level 345 mysqld on
$ sudo /etc/init.d/mysqld start
$ sudo mysqladmin -u root password 'パスワード'
$ sudo mysql_secure_installation
      # root ユーザーのパスワードを変更するか?
      Change the root password? [Y/n] n

      # 匿名ユーザーを削除するか?
      Remove anonymous users? [Y/n] Y

      # リモートから root ユーザーのログインを禁止するか?
      Disallow root login remotely? [Y/n] Y

      # test データベースを削除するか?
      Remove test database and access to it? [Y/n] Y

      # 権限テーブルをリロードするか?
      Reload privilege tables now? [Y/n] Y

続けてDBを作成

$ mysql -u root -p
> create database xxxxxxx;

wordpressを入れる

$ tar zxvf wordpress-4.5.2-ja.tar.gz
$ sudo cp -r wordpress/* /var/www/html/
$ sudo chown nginx.nginx -R  /var/www/html/
$ sudo vi /var/www/html/wp-config.php

# DB_NAME, DB_USER, DB_PASSWORDを上で設定した値にする。
# AUTH_KEY~NONCE_SALTのdefineの設定は
# https://api.wordpress.org/secret-key/1.1/salt/
# にアクセスして取得したものを貼る

起動させる。

$ sudo /etc/init.d/php-fpm start
$ sudo /etc/init.d/nginx start

以上でとりあえずwordpressの画面にアクセスできるはず・・。


ちなみにiptablesを設定している場合は注意。
ローカルから9000ポートにアクセスできるようにしておきましょう。

$ sudo iptables -A INPUT -m state --state NEW -m tcp   -p tcp --dport 9000  -j ACCEPT -s 127.0.0.1

[追記]

なぜかテーマと、プラグインのメニューに「新規追加」の項目が出なかった。

db消して、wordpressを置きなおしたら直ったが、直接の原因は恐らく設定ファイルの文字コード
だと思われる。

$ nkf -g  /tmp/wp-config.php.old  ./wp-config.php
/tmp/wp-config.php.old:ASCII (LF)
./wp-config.php:UTF-8 (LF)

wp-config.php.oldが古いほう。

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
0