Home > Backend Development > PHP Problem > How to solve php fpm error problem

How to solve php fpm error problem

藏色散人
Release: 2023-03-14 18:46:02
Original
2393 people have browsed it

php fpm出错的解决办法:1、在nginx的目录中创建个php的检测脚本index.php;2、找到nginx加载php配置的部分;3、修改路径即可。

How to solve php fpm error problem

本文操作环境:linux5.9.8系统、PHP5.5版、Dell G3电脑。

如何解决php fpm出错问题?

nginx调用php-fpm出错解决方法和nginx配置详解

这篇文章介绍了nginx调用php-fpm出错的解决方法,最后给出了nginx配置方法,需要的朋友可以参考下

 

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

使用下面的命令

代码如下:

/usr/local/php/sbin/php-fpm
Copy after login
Copy after login

就可以启动了。

在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"
Copy after login

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

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

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

代码如下:

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
Copy after login

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

代码如下:

 fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
http://localhost/index.php
Copy after login

可以出现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
Copy after login

与php-5.2.9在同一级目录

gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | patch -d php-5.2.9 -p1
Copy after login

补丁打好以后,编译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
Copy after login

注:--enable-fastcgi \ 需要在--enable-fpm \的前面,否则,fpm不能编译上。

二.编译好php后,修改配置文件

vi /EBS/php/etc/php-fpm.conf
Copy after login

需要注意下面几处配置

<value name="listen_address">127.0.0.1:9000</value>
Copy after login

这个表示php的fastcgi进程监听的ip地址以及端口

<value name="user">nobody</value>
<value name="group">nobody</value>
Copy after login

表示php的fastcgi进程以什么用户以及用户组来运行,默认该行是注释掉的,需要打开

<value name="display_errors">0</value>
Copy after login

是否显示php错误信息

<value name="max_children">5</value>
Copy after login

最大的子进程数目

运行php-fpm:

php-fpm用一个程序来控制fastcgi进程,这个文件在$PREFIX/sbin/php-fpm

/usr/local/php/sbin/php-fpm
Copy after login
Copy after login

该程序有如下参数:

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
Copy after login

这样,就保持了在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 &#39;$remote_addr - $remote_user [$time_local] "$request" &#39;
   &#39;$status $body_bytes_sent "$http_referer" &#39;
   &#39;"$http_user_agent" $http_x_forwarded_for&#39;;
   access_log logs/access.log access;
   }
}
Copy after login

新建配置文件

代码如下:

/usr/local/nginx/conf/fcgi.conf
Copy after login

注: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;
Copy after login

四 配置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
Copy after login

2、计算密码的md5值

echo -n "password"|md5sum
5f4dcc3b5aa765d61d8327deb882cf99
Copy after login

3、配置XCache

;注:zend_extension,用来加载zend的扩展,是绝对路径, extension是相对路径,相对于extension_dir的相对路径,非zend扩展

如果你更改路径以后,一定要apachectl stop后再start,而不要restart。

vi /usr/local/php/etc/php.ini
Copy after login

添加:

代码如下:

[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 = ""
Copy after login

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"
Copy after login

五、使用nginx对应多台facgi服务器

Idea: A front-end nginx is used for load balancing and processing static pages. Use nginx's upstream module to distribute php requests to the backend php-fpm server.

There are multiple php-fpm servers in the backend, and only the php-fpm service is used to process php.

This reduces the nginx service on php-fpm, which is equivalent to missing a layer.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to solve php fpm error problem. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template