Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the error of nginx calling php-fpm

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to solve the error of nginx calling php-fpm", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to solve the error of nginx calling php-fpm".

After nginx and php-5.5 are installed, and nginx is configured to call php, start php-fpm.

Use the following command

The copy code is as follows:

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

It's ready to start.

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

As a result, http://localhost/index.php is opened.

The tragic discovery can't be opened. Check the log file to see the reason for the error.

The copy code is as follows:

22:34:26 on 2013-07-01 [error] 321440: * 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 port 9000 of php-fpm has been opened, it shows that there is nothing wrong with php-fpm, but the problem lies in nginx. Maybe there's something wrong with my profile.

Find the piece where nginx loaded the php configuration. In addition, I refer to the configuration file of nginx on the Internet.

There is a path to invoke the script on line 69

The copy code is as follows:

Fastcgi_param script_filename / scripts$fastcgi_script_name

I'll just change the path to the one below.

The copy 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 following configuration method

Php-fpm no longer has to rely on other fastcgi initiators, such as lighttpd's spawn-fcgi.

Php-fpm is very convenient to use, and the configuration is all in the php-fpm.ini file.

Startup and restart can be carried out from php/sbin/php-fpm.

What is more convenient is that after modifying php.ini, you can directly use php-fpm reload to load without killing the process to complete the modified loading of php.ini.

The results show that the performance of php can be greatly improved by using php-fpm.

The process controlled by php-fpm. CPU recovery is relatively slow. The memory is allocated evenly.

On the other hand, the process controlled by spawn-cgi, cpu, drops rapidly. And the allocation of memory is relatively uneven.

Many processes seem to be unassigned, while others are highly occupied.

It may be due to the uneven distribution of process tasks. And this also leads to a decline in the overall response speed.

And the reasonable distribution of php-fpm. Leads to the mention of the overall response and the average of the task

Using php-fpm requires patching the php source code, and then recompiling php

one。 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

At the same level as php-5.2.9

Gzip-cd php-5.2.8-fpm-0.5.10.diff.gz | patch-d php-5.2.9-p1

After the patch is completed, the following parameters are added when compiling php:

-enable-fpm activates fpm support for fastcgi mode

-configuration file for with-fpm-conf php-fpm (default is prefix/etc/php-fpm.conf)

-the log file of with-fpm-log php-fpm (default is prefix/logs/php-fpm.log)

-pid file for with-fpm-pid php-fpm (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 precede-- enable-fpm\, otherwise fpm cannot be compiled on.

two。 After compiling the 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 the fastcgi process of php listens to.

Nobody

Nobody

Indicates the user and user group under which the fastcgi process of php runs. By default, the line is commented out and needs to be opened.

0

Whether or not to display php error messages

five

Maximum number of child processes

Run php-fpm:

Php-fpm uses a program to control the fastcgi process, which is found in $prefix/sbin/php-fpm

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

The program has the following parameters:

Start starts the fastcgi process of php

Stop forcibly terminates php's fastcgi process

Quit smoothly terminates the fastcgi process of php

Restart restarts the fastcgi process of php

Reload reloads php.ini for php

Logrotate re-enables log files

That is, after modifying the php.ini, we can use the

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

In this way, the php.ini is reloaded while the fastcgi process of php is running continuously.

The copy 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

}

}

New profile

The copy code is as follows:

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

Note: nginx comes with a configuration file, / usr/local/nginx/conf/fastcgi_params, which lacks a bold font, which will cause a 404 error when accessing the php file.

The copy code is as follows:

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

Four configuration xcache

1. Install the xcache module

Wget http://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. Calculate the MD5 value of the password

Echo-n "password" | md5sum

5f4dcc3b5aa765d61d8327deb882cf99

3. Configure xcache

Note: zend_extension, the extension used to load zend, is the absolute path, extension is the relative path, relative to extension_dir, non-zend extension

If you change the path, be sure to apachectl stop before start, not restart.

Vi / usr/local/php/etc/php.ini

Add:

The copy code is as follows:

[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. Restart the php module

After normal load

In the information shown by phpinfo

Zend, this should add the content of xcache.

6. The other two acceleration modules:

In our tests, the results are better than xcache, these three acceleration can not exist at the same time, there are conflicts.

6.1 apc

The copy code is as follows:

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"

5. Use nginx to correspond to multiple facgi servers

Idea: the front end of a nginx, used for load balancing and processing static pages. Use the upstream module of nginx to distribute the php request to the php-fpm server in the later segment.

There are multiple php-fpm servers at the back end, and only php-fpm services are used to handle php.

This reduces the number of nginx services on php-fpm, which is equivalent to one less layer.

At this point, I believe you have a deeper understanding of "how to solve the error of nginx calling php-fpm". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report