本篇内容主要讲解“Nginx调用PHP-fpm出错怎么解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Nginx调用PHP-fpm出错怎么解决”吧!
装完了Nginx和PHP-5.5,配置好了Nginx调用PHP后,就开始启动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的配置文件。
fastcgi_param script_filename /scripts$fastcgi_script_name;
我把路径改下,改成下面的就可以了。
fastcgi_param script_filename $document_root$fastcgi_script_name;
http://localhost/index.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
需要注意下面几处配置
<value name="listen_address">127.0.0.1:9000</value>
这个表示PHP的fastcgi进程监听的ip地址以及端口
<value name="user">nobody</value>
<value name="group">nobody</value>
表示PHP的fastcgi进程以什么用户以及用户组来运行,默认该行是注释掉的,需要打开
<value name="display_errors">0</value>
是否显示PHP错误信息
<value name="max_children">5</value>
最大的子进程数目
运行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出错怎么解决”有了更深的了解,不妨来实际操作一番吧!这里是编程之家网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。