CentOS 7にnginxとWordPressをサブディレクトリの形でインストールしてみました

WordPress
スポンサーリンク

CentOS 7にnginxとWordPressをインストールしました。

WordPressをサブディレクトリの形でインストールして使えるようにするには少し設定に工夫が必要です。

以下、まとめてみました。

 

スポンサーリンク

nginxのインストール方法

デフォルトのCentOS 7ではnginxのリポジトリを参照しておりません。

そのため、yumでnginxをインストールするには、/etc/yum.repos.d/nginx.repoというファイルを事前に作成します。

 

 

その後、

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

 

CentOS 7からserviceコマンドは使えなくなりました。代わりにsystemctlコマンドを使います。

# systemctl disable xxx でxxxをサービスの登録から解除
# systemctl enable xxx でxxxをサービスに登録
#systemctl list-unit-files | grep xxxでサービスの登録確認
# systemctl start xxx でxxxを起動
# systemctl stop xxx でxxxを停止
# systemctl status xxx でxxxのステータスを確認

となります。

 

Apacheとnginxではポート80番が競合するので、Apacheが動いている場合はサービスの登録から解除して、停止しておきます。

 

 

その後、nginxをサービスに登録してから起動します。

 

nginxが起動したことを確認します。

image

ブラウザで、http://localhost/ と入力して、

「Welcome to nginx!」

と表示されれば、nginxのインストールは正常に完了しています。

 

WordPress関連のプログラムのインストール

yumで以下のプログラムをインストールします。


データベースに関しては、CentOS 7から、MySQLではなくて、MariaDBというMySQL派生のオープンソースDBを使います。

最近は、MySQLではなくて、MariaDBを使っているプロジェクトが多いようです。

 

PHPの設定

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

 

PHPのタイムゾーンを設定します。

/etc/php.iniを開いて、date.timezone = “Asia/Tokyo”と入力します。

タイムゾーンの設定を行っていないと、後述するnginxのエラーログに以下のように表示されることがあります。

 

FastCGI sent in stderr: “PHP message: PHP Warning:  phpinfo(): It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone ‘UTC’ for now, but please set date.timezone to select your timezone. in /var/www/html/index.php on line 1” while reading response header from upstream, client: 127.0.0.1, server: localhost, request: “GET /index.php HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “localhost”

 

次に、php-fpmの設定を行います。

/etc/php-fpm.d/www.confを開いて、user、group、pm.max_requestsを設定します。

 

php-fpmをサービスに登録してから起動します。

 

php-fpm は、localhost のポート9000で待ち受けする形で起動しました。

 

MariaDBの設定

MariaDBをサービスに登録して、起動します。

 

wordpressのデータベースを作成して、ユーザーwordpressパスワードxxxyyyを設定し、権限を設定します。

 

 

WordPress本体のインストール

WordPress 本体は yum からはインストールできないようなので、本家のサイトから wget して展開します。

WordPress4.0 日本語版が最近出たのでそちらをインストールします。

 

WordPressのデータベースの設定を行います。

 

WordPressをサブディレクトリ形式でnginxで使えるようにする

 

以下の要件を実現します。

  • http://localhost/ で/var/www/html/index.htmlが表示される。
  • http://localhost/index.php で/var/www/html/index.phpが表示される。
  • http://localhost/wordpress で/var/www/wordpressに設定したWordPressがサブディレクトリで表示される。

 

Apacheなら、以下の設定だけでOKですが、nginxでは少し苦労しました。

 

nginxでは、/etc/nginx/conf.d/nginx.confを以下のように記述することで、上記の要件を実現することができます。

locationをネストしていることがポイントです。

 

上記のnginxのconfファイルの設定により、要件のすべてを満たすことができます。

 

Primary script unknownって何?locationにrootを記述してはダメ

nginxの設定でエラーが出る際には、いくつか修正ポイントがあります。

以下の通りにまとめてみました。

参考になればと思います。

 

1. nginxのaccess.logとerror.logは必ず生成するにしましょう。

何かあれば、必ずログの確認を行います。

 

2. Connection refusedというエラーが出ることがあります。

2014/09/16 14:49:41 [error] 8537#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost, request: “GET / HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “localhost”

これは、localhostの9000番ポートへの接続ができていません。

php-fpmが起動できているかどうか確認します。

 

3. Primary script unknownというエラーが出ることがあります。

2014/09/16 16:19:57 [error] 12637#0: *1 FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream, client: 127.0.0.1, server: localhost, request: “GET /wordpress/index.php HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “localhost”

Primary script unknownというメッセージは必ず、SCRIPT_FILENAMEの設定ミスと関連しています。

SCRIPT_FILENAMEを見直しましょう。

 

以下の記述の場合、$document_rootが正しくないことがあります。

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

 

 

4. locationにrootを記述してはダメ

locationブロックにrootを記述しても動きますが、推奨されません。

この記述は避けましょう。

 

5.  “/etc/nginx/html/index.html” is not foundというエラーが出ることがあります。

2014/09/16 16:12:25 [error] 12402#0: *1 “/etc/nginx/html/index.html” is not found (2: No such file or directory), client: 127.0.0.1, server: localhost, request: “GET / HTTP/1.1”, host: “localhost”

 

6. 設定したことがないディレクトリ名がエラーログに記載されることがあります。

listen 80 default_server;

というようにdefault_serverを記述するとこのエラーは回避できます。

default_serverの記述すると、他のバーチャルホストの設定よりも優先されます。

 

7. directory index of xxxx is forbidden というエラーが出ることがあります。

2014/09/16 16:31:10 [error] 12780#0: *1 directory index of “/var/www/wordpress/” is forbidden, client: 127.0.0.1, server: localhost, request: “GET /wordpress/ HTTP/1.1”, host: “localhost”

上記のエラーが出たときは、以下の記述を追加すると回避できます。

index index.php index.html index.htm;

 

8. rewrite or internal redirection cycle while internally redirecting toというエラーが出ることがあります。

2014/09/16 19:10:15 [error] 18443#0: *1 rewrite or internal redirection cycle while internally redirecting to “/wordpress/index.php”, client: 127.0.0.1, server: localhost, request: “GET /wordpress/ HTTP/1.1”, host: “localhost”

これはrewriteの設定が間違えています。

設定を確認しましょう。

 

9. nginx.serviceの起動時にJob for nginx.service failed. See ‘systemctl status nginx.service’ and ‘journalctl -xn’ for details.というエラーが出ることがあります。

# systemctl restart nginx.service

Job for nginx.service failed. See ‘systemctl status nginx.service’ and ‘journalctl -xn’ for details.

 

confファイルの中で;(セミコロン)が抜けている個所がないかどうか確認してみてください。

 

 nginx についてのおすすめの本

 

↓nginx についてのおすすめの本はコチラ

nginx

 

 

コメント

  1. […] urashita.com Centos 7にnginxとWordPressをサブディレクトリの形でインストールしてみました karakaram-blog お名前.com VPS […]

タイトルとURLをコピーしました