裝完了nginx和php-5.5,配置好了nginx調用php后,就開始啟動php-fpm。
使用下面的命令
復制代碼?代碼如下:
/usr/local/php/sbin/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的配置文件。
在第69行有一個調用腳本路徑
復制代碼?代碼如下:
fastcgi_param? script_filename? /scripts$fastcgi_script_name;
我把路徑改下,改成下面的就可以了。
復制代碼?代碼如下:
?fastcgi_param? script_filename? $document_root$fastcgi_script_name;
http://localhost/index.php
可以出現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
需要注意下面幾處配置
這個表示php的fastcgi進程監聽的ip地址以及端口
表示php的fastcgi進程以什么用戶以及用戶組來運行,默認該行是注釋掉的,需要打開
是否顯示php錯誤信息
最大的子進程數目
運行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服務,相當于少了一層。