使用CentOS5.11上的Nginx和PHP-FPM来运行PHP

我在网上找到了很多关于步骤的信息,但由于发生错误,无法顺利进行,所以做了一份备忘录。

操作步骤

安装Nginx

在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

启动服务器。

$ sudo /etc/init.d/nginx start

安装PHP

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

安装与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

启动PHP-FPM。

/etc/init.d/php-fpm start

6. Nginx的设置
/etc/nginx/conf.d/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

将index.php文件放置在/var/www/目录下。

<?php
    phpinfo();
?>

9. 使用浏览器访问
http://IP地址

如果能够显示PHP版本的页面就表示成功。

错误

有一个选项,那就是用中文来重新叙述这句话。

事件

以管理员权限运行 `/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()
                                                           [失敗]

Cause

由于启动了Apache,导致已经使用了80端口。

修改接收端口。

当我把 /etc/nginx/conf.d/default.conf 中的端口改为未被使用的端口时,问题得到了解决。

这2个

事件

当访问IP时,显示“找不到文件”。
查看/var/log/nginx/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. 网站二

bannerAds