千家信息网

nginx调用php-fpm出错怎么解决

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本篇内容主要讲解"nginx调用php-fpm出错怎么解决",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"nginx调用php-fpm出错怎么解决"吧!装完
千家信息网最后更新 2026年10月31日nginx调用php-fpm出错怎么解决

本篇内容主要讲解"nginx调用php-fpm出错怎么解决",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"nginx调用php-fpm出错怎么解决"吧!

装完了nginx和php-5.5,配置好了nginx调用php后,就开始启动php-fpm。

使用下面的命令

复制代码 代码如下:

/usr/local/php/sbin/php-fpm

就可以启动了。

在nginx的目录中创建个php的检测脚本index.php

结果在打开http://localhost/index.php

悲剧的发现居然无法打开 。查看日志文件,看了下报错原因

复制代码 代码如下:

2013/07/01 22:34:26 [error] 3214#0: *64 fastcgi sent in stderr: "primary script unknown" while reading response header from upstream, client: 192.168.168.19, server: localhost, request: "get /index.php http/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.168.140"

查看下端口 。看到php-fpm的9000端口已经打开了,说明php-fpm是没什么问题的,问题出在了nginx上了。可能是我的配置文件有问题。

找到nginx加载php配置的那块。另外参考了下网上nginx的配置文件。

在第69行有一个调用脚本路径

复制代码 代码如下:

fastcgi_param script_filename /scripts$fastcgi_script_name;

我把路径改下,改成下面的就可以了。

复制代码 代码如下:

fastcgi_param script_filename $document_root$fastcgi_script_name;

http://localhost/index.php

可以出现php的版本信息了。
大家还可以参考下面的配置方法

php-fpm不用再依赖其它的fastcgi启动器,比如lighttpd的spawn-fcgi。
php-fpm的使用非常方便,配置都是在php-fpm.ini的文件内
而启动,重启都可以从php/sbin/php-fpm中进行
更方便的是修改php.ini后可以直接使用php-fpm reload进行加载 无需杀掉进程就可以完成php.ini的修改加载
结果显示使用php-fpm可以使php有不小的性能提升
php-fpm控制的进程.cpu回收的速度比较慢.内存分配的很均匀
而spawn-cgi控制的进程cpu下降的很快.而内存分配的比较不均匀.
有很多进程似乎未分配到,而另外一些却占用很高.
可能是由于进程任务分配的不均匀导致的.而这也导致了总体响应速度的下降
而php-fpm合理的分配.导致总体响应的提到以及任务的平均
使用php-fpm需要在php源码上打补丁,然后重新编译php

一.下载php-fpm
wget http://cn.php.net/get/php-5.2.8.tar.gz/from/www.php.net/mirror
wget http://php-fpm.anight.org/downloads/head/php-5.2.8-fpm-0.5.10.diff.gz
与php-5.2.9在同一级目录
gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | patch -d php-5.2.9 -p1
补丁打好以后,编译php的时候增加了下面几个参数:
-enable-fpm 激活fastcgi模式的fpm支持
-with-fpm-conf php-fpm的配置文件(默认是prefix/etc/php-fpm.conf)
-with-fpm-log php-fpm的日志文件(默认是prefix/logs/php-fpm.log)
-with-fpm-pid php-fpm的pid文件(默认是prefix/logs/php-fpm.pid)
./configure --prefix=/ebs/php \
--with-config-file-path=/ebs/php/etc \
--enable-fastcgi \
--enable-fpm \
--others
注:--enable-fastcgi \ 需要在--enable-fpm \的前面,否则,fpm不能编译上。

二.编译好php后,修改配置文件
vi /ebs/php/etc/php-fpm.conf
需要注意下面几处配置
127.0.0.1:9000
这个表示php的fastcgi进程监听的ip地址以及端口
nobody
nobody
表示php的fastcgi进程以什么用户以及用户组来运行,默认该行是注释掉的,需要打开
0
是否显示php错误信息
5
最大的子进程数目
运行php-fpm:
php-fpm用一个程序来控制fastcgi进程,这个文件在$prefix/sbin/php-fpm
/usr/local/php/sbin/php-fpm
该程序有如下参数:
start 启动php的fastcgi进程
stop 强制终止php的fastcgi进程
quit 平滑终止php的fastcgi进程
restart 重启php的fastcgi进程
reload 重新加载php的php.ini
logrotate 重新启用log文件
也就是说,在修改了php.ini之后,我们可以使用
/usr/local/php/sbin/php-fpm reload
这样,就保持了在php的fastcgi进程持续运行的状态下,又重新加载了php.ini。

复制代码 代码如下:

user www www;
worker_processes 10;
error_log logs/error.log notice;
pid logs/nginx.pid;
#specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
charset gb2312;
server_names_hash_bucket_size 128;
#sendfile on;
#tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/html application/xml;
{
listen 80;
server_name 192.168.1.2;
index index.html index.htm index.php;
root /ebs/www;
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
location ~ .*\.php?$
{
include fcgi.conf

fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log logs/access.log access;
}
}

新建配置文件

复制代码 代码如下:

/usr/local/nginx/conf/fcgi.conf

注:nginx自带了一个配置文件,/usr/local/nginx/conf/fastcgi_params,该配置文件缺少粗体字体的部分,会造成访问php文件时报404错误。

复制代码 代码如下:

fastcgi_param gateway_interface cgi/1.1;
fastcgi_param server_software nginx;
fastcgi_param query_string $query_string;
fastcgi_param request_method $request_method;
fastcgi_param content_type $content_type;
fastcgi_param content_length $content_length;
fastcgi_param script_filename $document_root$fastcgi_script_name;
fastcgi_param script_name $fastcgi_script_name;
fastcgi_param request_uri $request_uri;
fastcgi_param document_uri $document_uri;
fastcgi_param document_root $document_root;
fastcgi_param server_protocol $server_protocol;
fastcgi_param remote_addr $remote_addr;
fastcgi_param remote_port $remote_port;
fastcgi_param server_addr $server_addr;
fastcgi_param server_port $server_port;
fastcgi_param server_name $server_name;
# php only, required if php was built with --enable-force-cgi-redirect
#fastcgi_param redirect_status 200;

四 配置xcache
1、安装xcache模块
wgethttp://xcache.lighttpd.net/pub/releases/1.2.2/xcache-1.2.2.tar.gz
tar -xvzf xcache-1.2.2.tar.gz
cd xcache-1.2.2
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-xcache --enable-xcache-optimizer
make
make install
2、计算密码的md5值
echo -n "password"|md5sum
5f4dcc3b5aa765d61d8327deb882cf99
3、配置xcache
;注:zend_extension,用来加载zend的扩展,是绝对路径, extension是相对路径,相对于extension_dir的相对路径,非zend扩展
如果你更改路径以后,一定要apachectl stop后再start,而不要restart。
vi /usr/local/php/etc/php.ini

添加:

复制代码 代码如下:

[xcache-common]
zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so
[xcache.admin]
; change xcache.admin.user to your preferred login name
xcache.admin.user = "admin"
; change xcache.admin.pass to the md5 fingerprint of your password
; use md5 -s "your_secret_password" to find the fingerprint
xcache.admin.pass = "5f4dcc3b5aa765d61d8327deb882cf99"
[xcache]
; change xcache.size to tune the size of the opcode cache
xcache.size = 24m
xcache.shm_scheme = "mmap"
xcache.count = 2
xcache.slots = 8k
xcache.ttl = 0
xcache.gc_interval = 0
; change xcache.var_size to adjust the size of variable cache
xcache.var_size = 8m
xcache.var_count = 1
xcache.var_slots = 8k
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.test = off
xcache.readonly_protection = on
xcache.mmap_path = "/tmp/xcache"
xcache.coredump_directory = ""
xcache.cacher = on
xcache.stat = on
xcache.optimizer = off
[xcache.coverager]
xcache.coverager = on
xcache.coveragedump_directory = ""

5、重启php模块
正常load之后,
在phpinfo显出的信息内
zend这快应该会加上xcache的内容

6、另外两种加速模块:
在我们的测试中,效果都要好于xcache,这3中加速不能同时存在两种,有冲突。
6.1 apc

复制代码 代码如下:

wget http://pecl.php.net/get/apc-3.0.19.tgz
cd apc-3.0.19
/usr/local/php/bin/phpize
./configure --enable-apc --enable-apc-mmap --with-apxs=/ebs/apache/bin/apxs --with-php-config=/ebs/php/bin/php-config
make
make install
6.2 eaccelerator
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.zip
cd eaccelerator-0.9.5.3
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/ebs/php/bin/php-config
make
make install
vi php.ini
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

五、使用nginx对应多台facgi服务器
思路:前端一台nginx,用于做为负载均衡和处理静态页面。利用nginx的upstream模块来将php请求分发到后段的php-fpm服务器上。
后端多台php-fpm的服务器,只起php-fpm服务来处理php。
这样做减少了php-fpm上的nginx服务,相当于少了一层。

到此,相信大家对"nginx调用php-fpm出错怎么解决"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

代码 文件 进程 配置 路径 分配 服务 模块 编译 信息 内容 服务器 端口 问题 面的 控制 运行 任务 内存 参数 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 上海喔噻互联网科技有限公司 关于新基建和互联网科技 紫鲸互联网科技 智慧科技互联网 科技互联网公司logo 火传媒上海互联网科技 哈尔滨新华互联网科技 上海电发互联网科技有限公司 现代科技和互联网有关系吗 西藏七维空间互联网科技有限公司 字节跳动和互联网科技 玉溪互联网科技怎么选 如何创新互联网科技 特约风豹互联网科技为什么扣费 广东工业互联网大赛深圳学泰科技 深圳无忧互联网科技 扫呗互联网科技有限公司 恒生互联网科技趋势 互联网科技创新方面的资料 科技互联网企业在行动 奇酷互联网络科技 互联网科技宣传片 华服互联网科技官网 中国航天科技大学国际工业互联网 互联网科技与大数据专业 深圳市优智互联网科技有限公司 佛山市翊博互联网络科技有限公司 互联网科技平台产品运营 科技互联网企业在行动 科技美学互联网 艺术
0