LoginSignup
7
7

More than 5 years have passed since last update.

CentOS5.11でNginxとPHP-FPMでPHPを動かす

Last updated at Posted at 2015-10-24

手順は調べればたくさんWebにあるけど、エラーが起きてすんなり進まなかったのでメモ。

手順

Nginxのインストール

1.yumにあるNginxは古いので新しいものを取得する

$ wget http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm
$ sudo rpm -ivh nginx-release-centos-5-0.el5.ngx.noarch.rpm

2.インストールする

$ sudo yum -y install nginx

3.サーバを起動する

$ sudo /etc/init.d/nginx start

PHPのインストール

1.epelとremiレポジトリを追加

$ rpm -ivh  http://ftp-srv2.kddilabs.jp/Linux/distributions/fedora/epel/5/x86_64/epel-release-5-4.noarch.rpm
$ rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

2.PHPに関連するファイルとPHP-FPMをインストール

$ sudo yum -y install --enablerepo=remi --enablerepo=remi-php55 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-fpm

3.PHP-FPMが自動起動するように設定する

$ chkconfig php-fpm on

4.PHP-FPMの設定ファイルを編集

/etc/php-fpm.d/www.conf の以下の箇所を修正
(userとgroupは環境に合わせて修正)

user = nginx
group = nginx

5.PHP-FPMを起動

/etc/init.d/php-fpm start

6.nginxの設定
/etc/nginx/conf.d/default.conf

default.conf
#rootのパスを変更
root    /var/www

#index.をphpに変更
index   index.php

#コメントアウトを外す
location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
        include        fastcgi_params;
    }

7.nginx起動

$ /etc/init.d/nginx start

8.index.phpの配置
/var/www/index.phpを作成

index.php
<?php
    phpinfo();
?>

9.ブラウザからアクセス
http://IPアドレス

PHPバージョンのページが表示されれば成功

エラー

その1

事象

$ sudo /etc/init.d/nginx start でエラー

nginx を起動中: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
                                                           [失敗]

原因

Apacheを起動していたので、すでに80ポートを使っていたのが原因。

受信するポートを変える。

$ sudo vim /etc/nginx/conf.d/default.conf
の中のポートを使っていないポートに変更したら解決した。

その2

事象

http://IP にアクセスしたら「File not found.」と表示された。
/var/log/nginx/error.logを確認すると、以下のようなエラーが出ていた。

error.log
[error] 9273#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.16.208.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "IP"

解決策

/etc/nginx/conf.d/default.confの「location ~ .php${」内の以下の部分はデフォルトでは無いパスを指定するため、書換える必要があった。

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

/scripts が無いのでエラー。

fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;

nginx再起動

$ /etc/init.d/nginx restart

備考

参考にさせてもらったサイト
その1
その2

7
7
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
7
7