Home  >  Article  >  Operation and Maintenance  >  How to solve the error when nginx calls php-fpm

How to solve the error when nginx calls php-fpm

WBOY
WBOYforward
2023-05-14 10:25:051026browse

After installing nginx and php-5.5, and configuring nginx to call php, start php-fpm.

Use the following command

Copy the code The code is as follows:

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

That’s it started.

Create a PHP detection script index.php in the nginx directory

The result is that when opening http://localhost/index.php

The tragic discovery is that it cannot be opened. . Check the log file and see the reason for the error

Copy code The code is as follows:

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"

Check the port. Seeing that the 9000 port of php-fpm has been opened, it means that there is no problem with php-fpm. The problem lies with nginx. Maybe there is something wrong with my configuration file.

Find the part where nginx loads the php configuration. Also refer to the nginx configuration file online.

There is a calling script path on line 69

Copy the code The code is as follows:

fastcgi_param script_filename /scripts$fastcgi_script_name;

I put Just change the path to the following.

Copy code The code is as follows:

fastcgi_param script_filename $document_root$fastcgi_script_name;

http://localhost/index.php

The version information of php can appear.
You can also refer to the configuration method below

php-fpm no longer needs to rely on other fastcgi launchers, such as lighttpd's spawn-fcgi.
php-fpm is very convenient to use, the configuration is in the php-fpm.ini file
Startup and restart can be done from php/sbin/php-fpm
More conveniently is modification After php.ini, you can directly use php-fpm reload to load it. You can complete the modification and loading of php.ini without killing the process.
The results show that using php-fpm can significantly improve the performance of php
php-fpm The CPU recycling speed of the controlled process is relatively slow. The memory allocation is very even.
The CPU of the process controlled by spawn-cgi drops very quickly. The memory allocation is relatively uneven.
There are many processes that seem to be unallocated. , while others occupy very high amounts.
It may be caused by the uneven distribution of process tasks. This also leads to a decrease in the overall response speed
And php-fpm's reasonable allocation leads to an improvement in the overall response and the average task
Using php-fpm requires patching the php source code, and then recompiling php

1. Download 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
In the same directory as php-5.2.9
gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | patch -d php-5.2.9 - After the p1
patch is applied, the following parameters are added when compiling php:
–enable-fpm activates fastcgi mode fpm support
–with-fpm-conf php-fpm configuration file (default Is prefix/etc/php-fpm.conf)
–with-fpm-log php-fpm’s log file (the default is prefix/logs/php-fpm.log)
–with-fpm-pid php- fpm's pid file (default is prefix/logs/php-fpm.pid)
./configure --prefix=/ebs/php \
--with-config-file-path=/ebs/php/ etc \
--enable-fastcgi \
--enable-fpm \
--others
Note: --enable-fastcgi \ needs to be in front of --enable-fpm \, otherwise, fpm cannot be compiled.

2. After compiling php, modify the configuration file
vi /ebs/php/etc/php-fpm.conf
You need to pay attention to the following configurations
127.0.0.1:9000
This represents the IP address and port that PHP's fastcgi process listens to
nobody
< ;value name="group">nobody
Indicates which user and user group the PHP fastcgi process runs as. By default, this line is commented out and needs to be turned on
0
Whether to display php error messages
5
Maximum number of child processes
Run php-fpm :
php-fpm uses a program to control the fastcgi process. This file is in $prefix/sbin/php-fpm
/usr/local/php/sbin/php-fpm
The program has the following parameters:
start Start the fastcgi process of php
stop Forcefully terminate the fastcgi process of php
quit Smoothly terminate the fastcgi process of php
restart Restart the fastcgi process of php
reload Reload the php.ini of php ##logrotate Re-enable the log file
In other words, after modifying php.ini, we can use
/usr/local/php/sbin/php-fpm reload
In this way, it remains in While php's fastcgi process continues to run, php.ini is reloaded.

Copy code The code is as follows:

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的内容
How to solve the error when nginx calls php-fpm

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服务,相当于少了一层。

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

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete