Nginx Call PHP-FPM Error resolution and Nginx configuration _nginx

Source: Internet
Author: User
Tags apc diff fpm md5 php error php source code zts zend

After loading nginx and php-5.5, configure the Nginx to invoke PHP and start php-fpm.

Use the following command

Copy Code code as follows:

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

You can start it up.

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

Results in opening http://localhost/index.php

The discovery of tragedy cannot be opened. View the log file and see the reason for the error

Copy Code code as follows:

2013/07/01 22:34:26 [ERROR] 3214#0: *64 FastCGI sent in stderr: ' Primary script unknown ' while reading response header fro M 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 bottom port. See the PHP-FPM 9000 port has been opened, indicating that PHP-FPM is no problem, the problem is in the nginx. There may be a problem with my configuration file.

Find the block where Nginx loaded the PHP configuration. Also refer to the next online nginx configuration file.

There is a call script path on line 69th

Copy Code code as follows:

Fastcgi_param Script_filename/scripts$fastcgi_script_name;

I changed the path and changed it to the bottom.
Copy Code code 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 relies on other fastcgi initiators, such as Lighttpd's spawn-fcgi.
PHP-FPM is easy to use, configuration is in Php-fpm.ini files
and start, restart can be done from the PHP/SBIN/PHP-FPM
more convenient is to modify the php.ini can be used directly after the PHP-FPM Reload to load without killing the process can complete php.ini modifications load the
results show that using PHP-FPM can make PHP have a performance boost
PHP-FPM controlled processes. CPU recovery is relatively slow. Memory allocation is very uniform
The spawn-cgi-controlled process CPU drops very quickly. And the memory allocation is not uniform.
There are many processes that do not seem to be assigned, while others are highly occupied. The
may be caused by uneven allocation of process tasks. This also leads to a decrease in the overall response rate
and php-fpm a reasonable allocation. The reference to the overall response and the average task
use PHP-FPM need to be patched on the PHP source code and then recompile 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
and php-5.2.9 at the same level of directory
GZIP-CD php-5.2.8-fpm-0.5.10.diff.gz | patch-d PHP-5.2.9-P1
After the patch is ready, add the following parameters when compiling PHP:
–enable-fpm activation fastcgi mode FPM support
–with-fpm-conf PHP-FPM configuration file (default is prefix/etc/php-fpm.conf)
–with-fpm-log php-fpm log file (default is Prefix/logs/php-fpm.log)
– With-fpm-pid php-fpm 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 \ Need to be in front of--enable-fpm \, otherwise, FPM cannot compile on.

Two. After compiling good PHP, modify the configuration file

Vi/ebs/php/etc/php-fpm.conf
You need to be aware of the following configurations
<value name= "Listen_address" >127.0.0.1:9000</value>
This represents the IP address and port that PHP fastcgi process listens to
<value name= "User" >nobody</value>
<value name= "group" >nobody</value>
Indicates what user and user groups the PHP fastcgi process runs on, and the default line is commented out and needs to be opened
<value name= "Display_errors" >0</value>
Whether to display PHP error messages
<value name= "Max_children" >5</value>
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 PHP fastcgi process
Stop forced to terminate PHP fastcgi process
Quit smooth termination of PHP fastcgi process
Restart restart PHP's fastcgi process
Reload Reload PHP's php.ini
Logrotate log file back on
In other words, after modifying the php.ini, we can use the
/USR/LOCAL/PHP/SBIN/PHP-FPM Reload
In this way, the PHP fastcgi process continues to run in the state, and reload the php.ini.

Copy Code code 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 the can is 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 configuration file

Copy Code code as follows:

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

Note: Nginx has a configuration file,/usr/local/nginx/conf/fastcgi_params, this profile is missing the bold font part, will cause access to PHP file times 404 error.

Copy Code code as follows:

Fastcgi_param Gateway_interface;
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 is built with--enable-force-cgi-redirect
#fastcgi_param redirect_status 200;

Four Configuration XCache

1. Install XCache module
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, 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 an absolute path, extension is a relative path, relative to the Extension_dir relative path, non-zend extension
If you change the path, be sure to Apachectl stop and start again instead of restart.
Vi/usr/local/php/etc/php.ini

Add to:

Copy Code code 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 that Phpinfo showed
Zend This should add XCache content.

6, the other two kinds of acceleration modules:
In our test, the effect is better than XCache, the 3 acceleration can not exist at the same time two, there is conflict.
6.1 APC

Copy Code code 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"

V. Use Nginx to correspond multiple facgi servers

Train of thought: Front end a nginx, used as load balancing and processing static page. Use the Nginx upstream module to distribute the PHP request to the PHP-FPM server on the back segment.
Back-end multiple PHP-FPM servers, only PHP-FPM services to handle PHP.
This reduces the Nginx service on the PHP-FPM, which is equivalent to a missing layer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.