2009年4月7日星期二
评: php-fpm 与 spawn-cgi 对比测试
php-fpm 与 spawn-cgi 对比测试
1. 软件版本
Nginx 0.7.17
php 5.2.6
php-fpm 0.5.9
编译参数
./configure –prefix=/usr/local/webserver/php \
–with-config-file-path=/usr/local/webserver/conf \
–enable-fastcgi \
–enable-fpm \
–with-fpm-conf=/usr/local/webserver/conf/php-fpm.conf \
–enable-force-cgi-redirect \
–with-gd \
–with-jpeg-dir \
–with-png-dir \
–enable-gd-native-ttf \
–with-freetype-dir \
–with-mysql=/usr/local/webserver/mysql \
–with-libxml-dir \
–with-mcrypt \
–with-mhash \
–with-curl \
–with-curlwrappers \
–with-zlib-dir \
–with-zlib \
–with-pear \
–with-openssl \
–without-pdo-sqlite \
–without-sqlite \
–disable-debug \
–disable-rpath \
–without-iconv \
–enable-bcmath \
–enable-pcntl \
–enable-mbstring \
–enable-exif \
–enable-zip \
–enable-ftp
3. 补丁安装方法
下载fpm补丁
将fpm加入php
patch -p1 < php-5.2.6-fpm-0.5.9.diff
在./configure后加入–enable-fastcgi –enable-fpm 以启用fpm
4.测试方法
取服务端的一个页面
var_dump($_SERVER);
phpinfo();
?>
另一台服务器发起请求
./http_load -parallel 25 -seconds 10 urls
5.CPU以及内存使用情况
对使用fpm启动的php-cgi以及spawn-fcgi启动的php-cgi进行比对
使用http_load对两者进行访问
然后获取服务器内的内存使用以及响应速度
fpm的使用后CPU下降的比较缓慢.而spawn-cgi的进程cpu下降很快.
很多情况下是使用完后进程CPU便回到0.0
php-fpm
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME
root 27724 0 1.2 103468 3400 ? Ss 02:45 00:00
nobody 27725 0.6 2.1 103988 5672 ? S 02:45 00:00
nobody 27726 0.5 2.1 103988 5672 ? S 02:45 00:00
nobody 27727 0.6 2 103988 5352 ? S 02:45 00:00
nobody 27728 0.7 2 103988 5348 ? S 02:45 00:00
nobody 27729 0.5 2 103988 5348 ? S 02:45 00:00
nobody 27730 0.6 2 103988 5348 ? S 02:45 00:00
nobody 27731 0.5 2 103988 5348 ? S 02:45 00:00
nobody 27732 0.7 2 103988 5344 ? S 02:45 00:00
nobody 27733 0.5 2 103988 5344 ? S 02:45 00:00
nobody 27734 0.2 2 103988 5340 ? S 02:45 00:00
nobody 27735 0.7 2 103988 5348 ? S 02:45 00:00
nobody 27736 0.3 2 103988 5344 ? S 02:45 00:00
nobody 27737 0.7 2 103988 5348 ? S 02:45 00:00
nobody 27738 0.5 2 103988 5348 ? S 02:45 00:00
nobody 27739 0.7 2 103988 5348 ? S 02:45 00:00
nobody 27740 0.8 2 103988 5352 ? S 02:45 00:00
nobody 27741 0.4 2 103988 5348 ? S 02:45 00:00
nobody 27742 0.5 2 103988 5344 ? S 02:45 00:00
nobody 27743 0.6 2 103988 5344 ? S 02:45 00:00
nobody 27744 0.5 2 103988 5348 ? S 02:45 00:00
nobody 27745 0.7 2 103988 5352 ? S 02:45 00:00
nobody 27746 0.4 2 103988 5348 ? S 02:45 00:00
nobody 27747 0.5 2 103988 5344 ? S 02:45 00:00
nobody 27748 0.6 2 103988 5348 ? S 02:45 00:00
nobody 27749 0.7 2 103988 5352 ? S 02:45 00:00
spwan-cgi
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME
www 27760 0 2.4 103452 6320 ? Ss 02:47 00:00
www 27761 0.2 2.1 103976 5548 ? S 02:47 00:00
www 27762 0.2 1.9 103980 5212 ? S 02:47 00:00
www 27763 0.2 1.9 103980 5212 ? S 02:47 00:00
www 27764 0.2 1.9 103980 5212 ? S 02:47 00:00
www 27765 1.4 1.9 103980 5224 ? S 02:47 00:00
www 27766 0.2 1.9 103980 5212 ? S 02:47 00:00
www 27767 0.4 1.9 103980 5220 ? S 02:47 00:00
www 27768 0.5 1.9 103980 5212 ? S 02:47 00:00
www 27769 0.5 1.9 103980 5216 ? S 02:47 00:00
www 27770 2.4 1.9 103980 5236 ? S 02:47 00:01
www 27771 3.1 2 103980 5248 ? S 02:47 00:01
www 27772 2.4 1.9 103980 5236 ? S 02:47 00:01
www 27773 1.3 1.9 103980 5224 ? S 02:47 00:00
www 27774 0.4 1.9 103980 5212 ? S 02:47 00:00
www 27775 0.4 1.9 103980 5212 ? S 02:47 00:00
www 27776 0.3 1.9 103980 5216 ? S 02:47 00:00
www 27777 0.1 1.9 103980 5212 ? S 02:47 00:00
www 27778 0.2 1.9 103980 5212 ? S 02:47 00:00
www 27779 0.2 1.9 103980 5212 ? S 02:47 00:00
www 27780 0.1 1.9 103980 5212 ? S 02:47 00:00
www 27781 0.1 1.9 103980 5212 ? S 02:47 00:00
www 27782 0.1 1.9 103980 5212 ? S 02:47 00:00
www 27783 0 1.9 103980 5212 ? S 02:47 00:00
www 27784 0 1.9 103980 5212 ? S 02:47 00:00
www 27785 0 1.9 103980 5212 ? S 02:47 00:00
6.反应时间
fetches/sec
php-fpm
264.468
269.657
266.5
267.7
267.334
spawn-cgi
246.153
258.742
256.1
256.9
251.6
7.测试结果
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合理的分配.导致总体响应的提到以及任务的平均
-------------------------------------------
i3server
i3server
www.86-00.com
评: php-fpm文档中文翻译
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://syre.blogbus.com/logs/20092011.html
原文链接:http://php-fpm.anight.org/
wiki:http://www.php-fpm.com/
什么是 FastCGI
FastCGI 是一个可伸缩、高速的在web server和脚本语言间通迅的接口。关于FastCGI技术的更多信息可以在官方网站和Wikipedia看到。
FastCGI 被许多脚本语言所支持,包括 php,如果用 --enable-fastcgi 选项编译的话。
多数流行的web server都支持 FastCGI。包括Apache(mod_fastcgi和mod_fcgid),Zeus,nginx和lighttpd。
FastCGI 的主要优点是把动态语言和 web server 分离开来。这种技术允许 web server 和动态语言运行在不同的主机上。这可以改进可扩展性和安全性而没有大的效率损失。
php-fpm 可以和任何支持外部 FastCGI 技术的 web server 一起使用。
php-fpm是做啥用的
很不幸,官方网站 php.net 上的 php 在将 FastCGI SAPI 用于生产环境方面有许多已知的问题。
下面是关于启用 FastCGI SAPI 时的问题和 php-fpm 是如何解决他们的对比列表。
| 描述 | php自带的 | spawn-fcgi + spawn-php.sh + daemontools | php-fpm |
|---|---|---|---|
| php守护进程化: pid file, log file, setsid(), setuid(), setgid(), chroot() | (-) | (+) | (+) |
| 进程管理。可以用 "graceful" 来停止并启动 php worker 进程而不会丢失请求。能够平滑地升级配置和二进制程序而不丢失任何请求。 | php4 (-), php5 (只有 graceful) | (-) | (+) |
| 严格限制来源请求的 web server 的 ip 地址 | php4 (-) php5 (+) (从 5.2.2 开始) | (-) | (+) |
| 根据负载动态调整进程数 | (-) | (-) | Todo |
| 用不同的 uid/gid/chroot/environment 和不同的 php.ini 选项启动 worder 进程。你不需要 safe mode 了! | (-) | (-) | (+) |
| 记录 worker 进程 stdout 和 stderr 日志 | (-) | (-) | (+) |
| 如果使用优化器,在共享内存意外破坏的情况下紧急重启所有的进程 | (-) | (-) | (+) |
| 如果 set_time_limit() 失败,确保进程会结束 | (-) | (-) | (+) |
| 特色功能 Error header、优化的上传支持、fastcgi_finish_request() | |||
特色功能
所有这些特性都是“不打断”的方式实现的。也就是说,如果你不使用它们,它们的存在不会影响php的功能性——他们都是“透明”的。
Error header
范围:php.ini 选项
分类:便利性
默认情况下,如果被访问的php脚本包含语法错误,用户会收到一个空的“200 ok”页。这是不方便的。Error header 这个 php.ini 选项允许在这种情况下产生一个 HTTP 错误码,比如“HTTP/1.0 550 Server Made Big Boo”,从而中断web server请求并显示一个正确的错误页。
如果要实现这样的功能,需要在 php.ini 中添加一条 fastcgi.error_header = "HTTP/1.0 550 Server Made Big Boo"
在 php-5.2.4 中添加了类似,但不相同的功能:如果被访问的php脚本包含语法错误,并且 display_errors = off,会立刻返回“HTTP/1.0 500 Internal Server Error”。
如果你需要设定一个 503 错误,或者想要使这个行为独立于 display_errors 的设置,那么可以使用fastcgi.error_header。如果你在 php-5.2.5 或以上版本上启用 php-fpm,那么 fastcgi.error_header的优先级更高。
优化的上传支持
实质:web server 支持
类型:优化
这个特性正如名字那样,可以加速对大 POST 请求的处理速度,包括文件上传。优化是通过将请求体已写入一个临时文件,然后 fastcgi 协议传递文件名而不是请求体到来实现的。目前就我所知,只有 nginx0.5.9 以上才支持这个功能。显然,这种模式只在 php 和 web server 在一台机器上的时候才能用。
nginx 样例配置:
location ~ \.php$ {
fastcgi_pass_request_body off;
client_body_in_file_only clean;
fastcgi_param REQUEST_BODY_FILE $request_body_file;
...
fastcgi_pass ...;
}
在php中不需要配置任何东西。如果php收到了参数REQUEST_BODY_FILE,就读取其中的请求体,如果没有,就自行从fastcgi协议中读取请求体。
结合这个特性,可以考虑对临时文件使用内存文件系统,例如tmpfs(linux):
client_body_temp_path /dev/shm/client_body_temp;
fastcgi_finish_request()
范围:php 函数
类型:优化
这个特性可以提高一些 php 请求的处理速度。如果有些处理可以在页面生成完后进行,就可以使用这种优化。比如,在 memcached 中保存 session 就可以在页面交给 web server 后进行。fastcgi_finisth_request() ,这一特性可以结束响应输出,web server 可以立即开始交给等不及的客户端,而此刻,php 可以在请求的上下文环境中处理许多事情。比如保存session,转换上传的视频,处理统计等等。
fastcgi_finisth_request() 会触发 shutdown 函数运行。
request_slowlog_timeout
范围: php-fpm.conf 选项
分类: 方便
这个选项能让你跟踪执行缓慢的脚本并把他们连同调用栈一起记录再日志文件中。例如如下设置:
记录的 slow.log 可能是这个样子:
Sep 21 16:22:19.399162 pid 29715 (pool default)
script_filename = /local/www/stable/www/catalogue.php
[0x00007fff23618120] mysql_query() /srv/stable/common/Database/class.MySQLRequest.php:20
[0x00007fff23618560] getResult() /srv/stable/common/Database/class.Facade.php:106
[0x00007fff23618aa0] query() /srv/stable/common/mysite.com/ORM/class.UsersMapper.php:99
[0x00007fff23618d60] resolveByID() /srv/stable/common/mysite.com/ORM/class.User.php:629
[0x00007fff236193b0] getData() /srv/stable/common/class.DataEntity.php:90
[0x00007fff236195d0] load() /srv/stable/common/mysite.com/ORM/class.User.php:587
[0x00007fff23619a00] getIsHidden() /srv/stable/common/mysite.com/class.User.php:42
[0x00007fff2361a470] getName() /local/www/stable/www/catalogue.php:41
同时,在 error.log 中保存了如下记录:
Sep 21 16:22:19.399031 [WARNING] fpm_request_check_timed_out(), line 135: child 29715, script '/local/www/stable/www/catalogue.php' (pool default) executing too slow (5.018002 sec), logging
正如你再例子中看到的,脚本运行了 5 秒以上,并很可能是由于 mysql 响应慢造成的(top backtrace)。
FAQ
Q:php-fpm 可以和 ZendOptimize 一起用吗?
A:完全可以。
Q:php-fpm 可以和 ZendPlatform、xcache、eAccelerator、APC 等的优化器一起用吗?
A:是的。php-fpm 的架构和任何一种用于高速 opcode 缓存的共享内存都适用。唯一的限制是:所有的 worker 进程只能适用一个缓存,即使它们用不同的 uid/gid 运行
Q:为什么我要给 php 打补丁呢?spawn-fcgi 不需要这样!
A:php-fpm 的创建是为了增强方便管理。没有打过补丁的 php 不能做到:
平滑重启 php 而不丢失请求,包括升级 php 二进制文件 以及/或者 扩展。
用不同的 uid / gid / chroot 环境运行 worker 进程
所有的设置只有一个配置文件
根据负载动态请求 (TODO)
对 php 请求实时统计性能 (TODO)
Q:为什么要用 root 运行 php-fpm 呢?这安全吗?
A:用 root 启动 php-fpm 只有在你打算用不同 uid/gid 的 php 来处理请求时才有意义。比如,在共享主机上的不同站点。因为只有在 master 进程用 root 运行的时候,才可以建立不同 uid/gid 的子进程。这是相当安全的。master 进程自己从来不会去处理请求。
在任何情况下,php-fpm 都不会用 root 身份来处理请求。
Q:php-fpm 可以加速 php 脚本处理速度吗?
A:不,它不会影响处理速度。不过,如果你使用一些特殊特性,对于一些特定的请求还是可以有性能提升的。
Q:如果我把我的网站从 mod_php 迁移到 php-fpm ,我会得到性能提升吗?
A: 通常,当有服务器上有大量空闲内存可用时,能从迁移到 php-fpm 中得到的性能提升可能不大。但是如果内存并不充裕,性能提升还是很可观的,在某些情况下可以达到 300-500%。这可能是由于 nginx + php-fpm 一般会比 Apache + mod_php 使用更少的内存。而且 VFS 缓存会由于更多的空余内存而更有效地工作。
Q:php-fpm 将来会被官方的 php 包含吗?
A: 我希望如此。目前,php-fpm 代码的协议是 GPL 。所以现在 php-fpm 的代码与 php 协议(类似 bsd)并不匹配。这是临时性措施。这样的选择是为了简化开发过程。一旦代码的功能完备,比如自适应生成子进程和其他一些东西,协议会改为一个相匹配的。 之后,php-fpm 会正式发布给 php 开发团队,并被建议包含。
邮件列表
如果你有问题的话,请不要犹豫在邮件组里写邮件。
English: highload-php-en Russian: highload-php-ru
文档
php-fpm 已经在 Linux、MacOSX、Solaris 和 FreeBSD 上测试通过。
确信 libxml2(在某些系统上叫做libxml2-devel)已经安装。
$ bzip2 -cd php-5.2.5.tar.bz2 | tar xf -
$ gzip -cd php-5.2.5-fpm-0.5.7.diff.gz | patch -d php-5.2.5 -p1
$ cd php-5.2.5 && ./configure --enable-fastcgi --enable-fpm
$ make all install
编辑 $prefix/etc/php-fpm.conf
运行 $prefix/bin/php-cgi --fpm
仔细检查 $prefix/logs/php-fpm.log
运行 phpinfo() 检查你的网站是否还正常运行
master 进程的 pid 被存放在 $prefix/logs/php-fpm.pid
master进程可以理解以下信号:
| SIGINT, SIGTERM | 立刻终止 |
| SIGQUIT | 平滑终止 |
| SIGUSR1 | 重新打开日志文件 |
| SIGUSR2 | 平滑重载所有worker进程并重新载入配置和二进制模 |
关于
嗨,我的名字叫 Andrei Nigmatulin, 我是 php-fpm 的作者。
从 2004 年开始,我就在等有什么人让 PHP FastCGI 能满足产品环境,但我等不下去了。
php-fpm 是在数个项目种使用 PHP 的 FastCGI SAPI 中的知识、经验和想法的产物。
php-fpm 可以在 GPL 协议下用在公共用途。和 php-fpm 绑定的修改版的 libevent 是在 BSD 协议下发布的。
我需要得到您的反馈——新的想法和建议——来改进和优化 php FastCGI SAPI。 如果您有什么想法、意见、补充和建议,我会很高兴,很原意听取,也许还会实现他们。给给我发邮件吧。(地址在本页的末尾)。
如果你想支持 php-fpm 的开发,可以作一些捐赠: Paypal Yandex.Money
15/05/2007 - 第一次提交到 php-fpm.
andrei dot nigmatulin at gmail dot com
译注:
php-fpm还带有一个更方便的脚本,在$prefix/sbin/php-fpm。可以用php-fpm start|graceful|restart|stop来维护。稍编辑一下就可以让它使用配置文件。
后记:
最开始,php-fpm 只有俄文文档,弄的我很郁闷,于是我先用 google 翻译先弄成英文,然后再人工翻成中文。这当中会难免会在我自己的英文水平引起的错误之外,再多些错误出来。后来终于有了一个英文的 wiki,并邀请我提供中文翻译。同时,距上一次翻译(2008年5月)以后,原来的文档也已经有了更新。于是我就根据英文 wiki ,重新翻译了一遍。
-------------------------------------------
i3server
i3server
www.86-00.com
评: Nginx 0.6.31 + PHP 5.2.6(FastCGI)搭建胜过Apache十倍的Web服务器(第3版)
文章作者:张宴 本文版本:v3.0 最后修改:2008.06.09
前言:本文是我撰写的关于搭建“Nginx + PHP(FastCGI)”Web服务器的第3篇文章,也是Nginx 0.6系列版本成为稳定版后的第一篇文章,安装、配置方式与以往略有不同,并增加了一些在生产环境运行中不断修改、调整,总结出的配置上的优化。
Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。
Nginx 的中文维基:http://wiki.codemongers.com/NginxChs
在高并发连接的情况下,Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。根据我的测试结果,Nginx 0.6.31 + PHP 5.2.6 (FastCGI) 可以承受3万以上的并发连接数,相当于同等环境下Apache的10倍。
根据我的经验,4GB内存的服务器+Apache(prefork模式)一般只能处理3000个并发连接,因为它们将占用3GB以上的内存,还 得为系统预留1GB的内存。我曾经就有两台Apache服务器,因为在配置文件中设置的MaxClients为4000,当Apache并发连接数达到 3800时,导致服务器内存和Swap空间用满而崩溃。
而这台 Nginx 0.6.31 + PHP 5.2.6 (FastCGI) 服务器在3万并发连接下,开启的10个Nginx进程消耗150M内存(15M*10=150M),开启的64个php-cgi进程消耗1280M内存 (20M*64=1280M),加上系统自身消耗的内存,总共消耗不到2GB内存。如果服务器内存较小,完全可以只开启25个php-cgi进程,这样 php-cgi消耗的总内存数才500M。
在3万并发连接下,访问Nginx 0.6.31 + PHP 5.2.6 (FastCGI) 服务器的PHP程序,仍然速度飞快。下图为Nginx的状态监控页面,显示的活动连接数为28457(关于Nginx的监控页配置,会在本文接下来所给出 的Nginx配置文件中写明):
我生产环境下的两台Nginx + PHP5(FastCGI)服务器,跑多个一般复杂的纯PHP动态程序,单台Nginx + PHP5(FastCGI)服务器跑PHP动态程序的处理能力已经超过“700次请求/秒”,相当于每天可以承受6000万 (700*60*60*24=60480000)的访问量(更多信息见此),而服务器的系统负载也不算高:
~~~~~~~~~~~~~~~~~~~~~~~~~
安装步骤:
(系统要求:Linux 2.6+ 内核,本文中的Linux操作系统为CentOS 5.1,另在RedHat AS4上也安装成功)
一、获取相关开源程序:
1、利用CentOS Linux系统自带的yum命令安装、升级所需的程序库(RedHat等其他Linux发行版可从安装光盘中找到这些程序库的RPM包,进行安装):
sudo -s
LANG=C
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel
2、下载程序源码包:
本文中提到的所有开源软件为截止到2008年6月5日的最新稳定版。
mkdir -p /data0/software cd /data0/software wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz wget http://www.php.net/get/php-5.2.6.tar.gz/from/this/mirror wget http://php-fpm.anight.org/downloads/head/php-5.2.6-fpm-0.5.8.diff.gz wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.24-rc.tar.gz/from/http://mysql.ntu.edu.tw/ wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.12.tar.gz wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0" wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.7.tar.gz?modtime=1194463373&big_mirror=0" wget http://pecl.php.net/get/memcache-2.2.3.tgz wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.tar.gz?modtime=1175740843&big_mirror=0" wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz
二、安装PHP 5.2.6(FastCGI模式)
1、编译安装PHP 5.2.6所需的支持库:
tar zxvf libiconv-1.12.tar.gz cd libiconv-1.12/ ./configure --prefix=/usr/local make make install cd ../tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../tar zxvf mhash-0.9.9.tar.gz
cd mhash-0.9.9/
./configure
make
make install
cd ../cp /usr/local/lib/libmcrypt.* /usr/lib
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2tar zxvf mcrypt-2.6.7.tar.gz
cd mcrypt-2.6.7/
./configure
make
make install
cd ../
2、编译安装MySQL 5.1.24-rc
/usr/sbin/groupadd mysql /usr/sbin/useradd -g mysql mysql tar zxvf mysql-5.1.24-rc.tar.gz cd mysql-5.1.24-rc/ ./configure --prefix=/usr/local/webserver/mysql/ --without-debug --with-unix-socket-path=/usr/local/webserver/mysql/mysql.sock --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --with-extra-charsets=gbk,gb2312,utf8 --with-pthread --enable-thread-safe-client make && make install chmod +w /usr/local/webserver/mysql chown -R mysql:mysql /usr/local/webserver/mysql cp support-files/my-medium.cnf /usr/local/webserver/mysql/my.cnf cd ../
附:以下为附加步骤,如果你想在这台服务器上运行MySQL数据库,则执行以下两步。如果你只是希望让PHP支持MySQL扩展库,能够连接其他服务器上的MySQL数据库,那么,以下两步无需执行。
①、以mysql用户帐号的身份建立数据表:
/usr/local/webserver/mysql/bin/mysql_install_db --defaults-file=/usr/local/webserver/mysql/my.cnf --basedir=/usr/local/webserver/mysql --datadir=/usr/local/webserver/mysql/data --user=mysql --pid-file=/usr/local/webserver/mysql/mysql.pid --skip-locking --port=3306 --socket=/tmp/mysql.sock
②、启动MySQL(最后的&表示在后台运行)
/bin/sh /usr/local/webserver/mysql/bin/mysqld_safe --defaults-file=/usr/local/webserver/mysql/my.cnf &
3、编译安装PHP(FastCGI模式)
tar zxvf php-5.2.6.tar.gz gzip -cd php-5.2.6-fpm-0.5.8.diff.gz | patch -d php-5.2.6 -p1 cd php-5.2.6/ ./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-debug --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl sed -i 's#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#' Makefile make make install cp php.ini-dist /usr/local/webserver/php/etc/php.ini cd ../
4、编译安装PHP5扩展模块
tar zxvf memcache-2.2.3.tgz cd memcache-2.2.3/ /usr/local/webserver/php/bin/phpize ./configure --with-php-config=/usr/local/webserver/php/bin/php-config make make install cd ../tar zxvf xcache-1.2.2.tar.gz
cd xcache-1.2.2/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --enable-xcache
make
make install
cd ../
5、修改php.ini文件
手工修改:查找/usr/local/webserver/php/etc/php.ini中的extension_dir = "./"
修改为extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
再查找display_errors = On
修改为display_errors = Off
自动修改:若嫌手工修改麻烦,可执行以下shell命令,自动完成对php.ini文件的修改:
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\n#' /usr/local/webserver/php/etc/php.ini sed -i 's#display_errors = On#display_errors = Off#' /usr/local/webserver/php/etc/php.ini
6、配置XCache加速PHP:
vi /usr/local/webserver/php/etc/php.ini按shift+g键跳到配置文件的最末尾,加上以下配置信息:
[xcache-common] zend_extension = /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so[xcache.admin]
xcache.admin.user = "xcache"
; xcache.admin.pass = md5($yourpasswd)
xcache.admin.pass = "8e6867a5d05144cf4761d6481fc674a8"[xcache]
xcache.cacher = On
xcache.shm_scheme = "mmap"
xcache.size = 32M
; cpu number (cat /proc/cpuinfo |grep -c processor)
xcache.count = 2
xcache.slots = 8k
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size = 2M
; cpu number (cat /proc/cpuinfo |grep -c processor)
xcache.var_count = 2
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.readonly_protection = Off
xcache.mmap_path = "/dev/zero"
7、创建www用户和组,以及供blog.s135.com和www.s135.com两个虚拟主机使用的目录:
/usr/sbin/groupadd www -g 48 /usr/sbin/useradd -u 48 -g www www mkdir -p /data0/htdocs/blog chmod +w /data0/htdocs/blog chown -R www:www /data0/htdocs/blog mkdir -p /data0/htdocs/www chmod +w /data0/htdocs/www chown -R www:www /data0/htdocs/www
8、创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):
在/usr/local/webserver/php/etc/目录中创建php-fpm.conf文件:
rm -f /usr/local/webserver/php/etc/php-fpm.conf vi /usr/local/webserver/php/etc/php-fpm.conf
输入以下内容:
All relative paths in this config are relative to php's install prefix
Pid file
/usr/local/webserver/php/logs/php-fpm.pid
Error log file
/usr/local/webserver/php/logs/php-fpm.log
Log level
notice
When this amount of php processes exited with SIGSEGV or SIGBUS ...
10
... in a less than this interval of time, a graceful restart will be initiated.
Useful to work around accidental curruptions in accelerator's shared memory.
1m
Time limit on waiting child's reaction on signals from master
5s
Set to 'no' to debug fpm
yes
Name of pool. Used in logs and stats.
default
Address to accept fastcgi requests on.
Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
127.0.0.1:9000
Set listen(2) backlog
-1
Set permissions for unix socket, if one used.
In Linux read/write permissions must be set in order to allow connections from web server.
Many BSD-derrived systems allow connections regardless of permissions.
0666
Additional php.ini defines, specific to this pool of workers.
/usr/sbin/sendmail -t -i
0
Unix user of processes
www
Unix group of processes
www
Process manager settings
Sets style of controling worker process count.
Valid values are 'static' and 'apache-like'
static
Sets the limit on the number of simultaneous requests that will be served.
Equivalent to Apache MaxClients directive.
Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
Used with any pm_style.
128
Settings group for 'apache-like' pm style
Sets the number of server processes created on startup.
Used only when 'apache-like' pm_style is selected
20
Sets the desired minimum number of idle server processes.
Used only when 'apache-like' pm_style is selected
5
Sets the desired maximum number of idle server processes.
Used only when 'apache-like' pm_style is selected
250
Time limit on waiting execution of single request
Should be used when 'max_execution_time' ini option does not terminate execution for some reason
31s
Set open file desc rlimit
51200
Set max core size rlimit
0
Chroot to this directory at the start
Chdir to this directory at the start
Redirect workers' stdout and stderr into main error log.
If not set, they will be redirected to /dev/null, according to FastCGI specs
yes
How much requests each process should execute before respawn.
Useful to work around memory leaks in 3rd party libraries.
For endless request processing please specify 0
Equivalent to PHP_FCGI_MAX_REQUESTS
51200
Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
Makes sense only with AF_INET listening socket.
127.0.0.1
Pass environment variables like LD_LIBRARY_PATH
All $VARIABLEs are taken from current environment
$HOSTNAME
/usr/local/bin:/usr/bin:/bin
/tmp
/tmp
/tmp
$OSTYPE
$MACHTYPE
2
9、启动php-cgi进程,监听127.0.0.1的9000端口,进程数为128(如果服务器内存小于3GB,可以只开启25个进程),用户为www:
/usr/local/webserver/php/sbin/php-fpm start
注:/usr/local/webserver/php/sbin/php-fpm还有其他参数,包 括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件 使用reload。
~~~~~~~~~~~~~~~~~~~~~~~
三、安装Nginx 0.6.31
1、安装Nginx所需的pcre库:
tar zxvf pcre-7.7.tar.gz
cd pcre-7.7/
./configure
make && make install
cd ../
2、安装Nginx
tar zxvf nginx-0.6.31.tar.gz cd nginx-0.6.31/ ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module make && make install cd ../
3、创建Nginx日志目录
mkdir -p /data1/logs chmod +w /data1/logs chown -R www:www /data1/logs
4、创建Nginx配置文件
①、在/usr/local/webserver/nginx/conf/目录中创建nginx.conf文件:
rm -f /usr/local/webserver/nginx/conf/nginx.conf vi /usr/local/webserver/nginx/conf/nginx.conf
输入以下内容:
user www www;worker_processes 8;
error_log /data1/logs/nginx_error.log crit;
pid /usr/local/webserver/nginx/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 utf-8;
server_names_hash_bucket_size 128;
sendfile on;
tcp_nopush on;keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /dev/shm;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;server
{
listen 80;
server_name blog.s135.com;
index index.html index.htm index.php;
root /data0/htdocs/blog;if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
location ~ .*\.php?$
{
include fcgi.conf;
#fastcgi_pass unix:/tmp/php-cgi.sock;
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 /data1/logs/access.log access;
}server
{
listen 80;
server_name www.s135.com;
index index.html index.htm index.php;
root /data0/htdocs/www;if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}location ~ .*\.php?$
{
include fcgi.conf;
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}log_format wwwlogs '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data1/logs/wwwlogs.log wwwlogs;
}server
{
listen 80;
server_name status.blog.s135.com;location / {
stub_status on;
access_log off;
}
}
}
②、在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件:
vi /usr/local/webserver/nginx/conf/fcgi.conf
输入以下内容:
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;
5、启动Nginx
/usr/local/webserver/nginx/sbin/nginx
--------------------------------------------------------------------------------
四、配置开机自动启动Nginx + PHP
vi /etc/rc.local
在末尾增加以下内容:
/usr/local/webserver/php/sbin/php-fpm start
/usr/local/webserver/nginx/sbin/nginx
--------------------------------------------------------------------------------
五、优化Linux内核参数
vi /etc/sysctl.conf
在末尾增加以下内容:
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000
使配置立即生效:
/sbin/sysctl -p
--------------------------------------------------------------------------------
六、在不停止Nginx服务的情况下平滑变更Nginx配置
(1)、修改/usr/local/webserver/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/webserver/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully
(2)、这时,输入以下命令查看Nginx主进程号:
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
屏幕显示的即为Nginx主进程号,例如:
6302
这时,执行以下命令即可使修改过的Nginx配置文件生效:
kill -HUP 6302
或者无需这么麻烦,找到Nginx的Pid文件:
kill -HUP `cat /usr/local/webserver/nginx/nginx.pid`
--------------------------------------------------------------------------------
本文若有小的修改,会第一时间在以下网址发布:
http://blog.s135.com/read.php/351.htm
-------------------------------------------
i3server
i3server
www.86-00.com