<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ls /proc &#187; fastcgi</title>
	<atom:link href="http://www.lsproc.com/blog/tag/fastcgi/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lsproc.com/blog</link>
	<description>lsproc.com</description>
	<lastBuildDate>Fri, 21 May 2010 14:29:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>nginx+factcgi 下使用 ob_flush</title>
		<link>http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/</link>
		<comments>http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 03:45:23 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Linux&Webserver]]></category>
		<category><![CDATA[buffer]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[ob_flush]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lsproc.com/blog/?p=376</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/ Nginx与php-cgi是两个独立的程序，通过TCP或Unix套接字通信，不像Apache那样是集成在一起的。所以，Nginx有fastcgi 缓冲区，数据超出缓冲区大小、或程序执行完，才会将内容输出到客户端。如果要使用ob_flush，不能开启gzip压缩输出。 nginx.conf: fastcgi_buffer_size 4k; fastcgi_buffers 8 4k; gzip off; php.ini: output_buffering = Off &#60;?php set_time_limit(0); ob_end_clean(); ob_implicit_flush(1); for($i = 0; $i &#60; 10; $i++) { echo $i . &#34;&#60;br /&#62;\n&#34;; echo str_repeat(&#039;&#039;, 1024*4); sleep(1); } 其中 echo str_repeat('', 1024*4); 使得fastcgi_buffer_size 4k; 的缓冲区满，从而输出内容到浏览器 参考: http://blog.s135.com/nginx_php_v6/2/1/ -- EOF -- Related Posts2010-05-21 -- Nginx/PHP [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/">http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/</a></p>
<p>Nginx与php-cgi是两个独立的程序，通过TCP或Unix套接字通信，不像Apache那样是集成在一起的。所以，Nginx有fastcgi 缓冲区，数据超出缓冲区大小、或程序执行完，才会将内容输出到客户端。<strong>如果要使用ob_flush，不能开启gzip压缩输出。</strong></p>
<p>nginx.conf:</p>
<pre class="brush: text">fastcgi_buffer_size 4k;
fastcgi_buffers 8 4k;
gzip off;</pre>
<p>php.ini:</p>
<pre class="brush: text">
output_buffering = Off
</pre>
<pre class="brush: php">
&lt;?php
set_time_limit(0);
ob_end_clean();
ob_implicit_flush(1);

for($i = 0; $i &lt; 10; $i++)
{
    echo $i . &quot;&lt;br /&gt;\n&quot;;
    echo str_repeat(&#039;&#039;, 1024*4);
    sleep(1);
}
</pre>
<p>其中 echo str_repeat('', 1024*4);<br />
使得fastcgi_buffer_size 4k; 的缓冲区满，从而输出内容到浏览器</p>
<p>参考: http://blog.s135.com/nginx_php_v6/2/1/
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2010-05-21 -- <a href="http://www.lsproc.com/blog/nginx_php_pathinfo_securit/" title="Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo">Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo</a> (2)</li><li>2010-04-12 -- <a href="http://www.lsproc.com/blog/nginx_userid_decode/" title="nginx userid 模块客户端 cookie 解码">nginx userid 模块客户端 cookie 解码</a> (0)</li><li>2008-06-15 -- <a href="http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/" title="lighttpd + PHP(fastcgi) 配置">lighttpd + PHP(fastcgi) 配置</a> (0)</li><li>2009-03-29 -- <a href="http://www.lsproc.com/blog/nginx_ssl_config/" title="Nginx SSL 配置">Nginx SSL 配置</a> (23)</li><li>2008-10-23 -- <a href="http://www.lsproc.com/blog/nginx_rewrite_param/" title="nginx rewrite 的一些参数  ">nginx rewrite 的一些参数  </a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>lighttpd + PHP(fastcgi) 配置</title>
		<link>http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/</link>
		<comments>http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/#comments</comments>
		<pubDate>Sun, 15 Jun 2008 11:06:58 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Linux&Webserver]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lostk.com/blog/?p=149</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/ php(fastcgi) 5.2.6 编译参数 ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-gd=/usr --enable-calendar --with-zlib --with-bz2 --with-curl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-openssl --enable-zend-multibyte --with-gettext --enable-exif --with-png-dir=/usr --with-jpeg-dir=/usr --with-libxml-dir=/usr --enable-gd-native-ttf --enable-dom --with-freetype-dir=/usr --with-iconv-dir=/usr --enable-fastcgi lighttpd 1.5.0 编译参数 ./configure --prefix=/usr/local/lighttpd --with-mysql=/usr/bin/mysql_config --with-openssl --with-pcre --with-bzip2 --enable-lfs --with-linux-aio 添加fastcgi 及lighttpd执行用户 groupadd www useradd -g www -s /sbin/nologin -d /dev/null www 复制lighttpd源码包内 doc/spawn-php.sh 并修改如下 #!/bin/bash [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/">http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/</a></p>
<p>php(fastcgi) 5.2.6 编译参数</p>
<pre class="brush: bash">
./configure  --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-gd=/usr --enable-calendar --with-zlib --with-bz2 --with-curl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-openssl --enable-zend-multibyte --with-gettext --enable-exif --with-png-dir=/usr --with-jpeg-dir=/usr --with-libxml-dir=/usr --enable-gd-native-ttf --enable-dom --with-freetype-dir=/usr --with-iconv-dir=/usr --enable-fastcgi
</pre>
<p>lighttpd 1.5.0 编译参数</p>
<pre class="brush: bash">
./configure --prefix=/usr/local/lighttpd --with-mysql=/usr/bin/mysql_config --with-openssl --with-pcre --with-bzip2 --enable-lfs --with-linux-aio
</pre>
<p>添加fastcgi 及lighttpd执行用户</p>
<pre class="brush: bash">
groupadd www
useradd -g www -s /sbin/nologin -d /dev/null www
</pre>
<p>复制lighttpd源码包内 doc/spawn-php.sh 并修改如下</p>
<pre class="brush: bash">
#!/bin/bash

## ABSOLUTE path to the spawn-fcgi binary
SPAWNFCGI=&quot;/usr/local/lighttpd/bin/spawn-fcgi&quot;

## ABSOLUTE path to the PHP binary
FCGIPROGRAM=&quot;/usr/local/php/bin/php-cgi&quot;

## TCP port to which to bind on localhost
FCGIPORT=&quot;1026&quot;

## SOCKET to which to bind on localhost
FCGISOCKET=&quot;/tmp/php-fastcgi1.sock&quot;

## number of PHP children to spawn
PHP_FCGI_CHILDREN=16

## maximum number of requests a single PHP process can serve before it is restarted
PHP_FCGI_MAX_REQUESTS=1000

## IP addresses from which PHP should access server connections
FCGI_WEB_SERVER_ADDRS=&quot;127.0.0.1&quot;

# allowed environment variables, separated by spaces
ALLOWED_ENV=&quot;HOME PATH USER&quot;

## if this script is run as root, switch to the following user
USERID=www
GROUPID=www

################## no config below this line

if test x$PHP_FCGI_CHILDREN = x; then
  PHP_FCGI_CHILDREN=5
fi

export PHP_FCGI_MAX_REQUESTS
export FCGI_WEB_SERVER_ADDRS

ALLOWED_ENV=&quot;$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS&quot;

# port
#if test x$UID = x0; then
#  EX=&quot;$SPAWNFCGI -p $FCGIPORT -f $FCGIPROGRAM -u $USERID -g $GROUPID -C $PHP_FCGI_CHILDREN&quot;
#else
#  EX=&quot;$SPAWNFCGI -p $FCGIPORT -f $FCGIPROGRAM -C $PHP_FCGI_CHILDREN&quot;
#fi

# socket
if test x$UID = x0; then
  EX=&quot;$SPAWNFCGI  -s $FCGISOCKET -f $FCGIPROGRAM -u $USERID -g $GROUPID -C $PHP_FCGI_CHILDREN&quot;
else
  EX=&quot;$SPAWNFCGI  -s $FCGISOCKET -f $FCGIPROGRAM -C $PHP_FCGI_CHILDREN&quot;
fi

# copy the allowed environment variables
E=

for i in $ALLOWED_ENV; do
  E=&quot;$E $i=${!i}&quot;
done

# clean the environment and set up a new one
env - $E $EX
</pre>
<p>保存为 spawn-php1.sh 放到 /etc/lighttpd 下, 多个fastcgi实例可复制此文件并修改相关端口号及socket</p>
<p>lighttpd 相关配置 lighttpd.conf 如下</p>
<pre class="brush: bash">
server.use-ipv6 = &quot;disable&quot; # 缺省为禁用
server.event-handler = &quot;linux-sysepoll&quot; # Linux环境下epoll系统调用可提高吞吐量
#server.max-worker = 10 # 如果你的系统资源没跑满，可考虑调高  lighttpd进程数
server.max-fds = 4096 # 默认的，应该够用了，可根据实际情况调整
server.max-connections = 4096 # 默认等于 server.max-fds
server.network-backend = &quot;linux-sendfile&quot;
server.max-keep-alive-requests = 0 # 在一个keep-alive会话终止连接前能接受处理的最大请求数。0为禁止

# 设置要加载的module
server.modules = (
    &quot;mod_rewrite&quot;,
    &quot;mod_redirect&quot;,
#    &quot;mod_alias&quot;,
    &quot;mod_access&quot;,
#    &quot;mod_cml&quot;,
#    &quot;mod_trigger_b4_dl&quot;,
    &quot;mod_auth&quot;,
    &quot;mod_expire&quot;,
#    &quot;mod_status&quot;,
#    &quot;mod_setenv&quot;,
    &quot;mod_proxy_core&quot;,
    &quot;mod_proxy_backend_http&quot;,
    &quot;mod_proxy_backend_fastcgi&quot;,
#    &quot;mod_proxy_backend_scgi&quot;,
#    &quot;mod_proxy_backend_ajp13&quot;,
#    &quot;mod_simple_vhost&quot;,
    &quot;mod_evhost&quot;,
#    &quot;mod_userdir&quot;,
#    &quot;mod_cgi&quot;,
    &quot;mod_compress&quot;,
#    &quot;mod_ssi&quot;,
#    &quot;mod_usertrack&quot;,
#    &quot;mod_secdownload&quot;,
#    &quot;mod_rrdtool&quot;,
    &quot;mod_accesslog&quot; )

# 网站根目录
server.document-root = &quot;/var/www/&quot;

# 错误日志位置
server.errorlog = &quot;/var/log/lighttpd/error.log&quot;

# 网站Index
index-file.names = ( &quot;index.php&quot;, &quot;index.html&quot;,
                                 &quot;index.htm&quot;, &quot;default.htm&quot; )

# 访问日志, 以及日志格式 (combined), 使用X-Forwarded-For可越过代理读取真实ip
accesslog.filename = &quot;/var/log/lighttpd/access.log&quot;
accesslog.format = &quot;%{X-Forwarded-For}i %v %u %t \&quot;%r\&quot; %s %b  \&quot;%{User-Agent}i\&quot; \&quot;%{Referer}i\&quot;&quot;

# 设置禁止访问的文件扩展名
url.access-deny = ( &quot;~&quot;, &quot;.inc&quot;, &quot;.tpl&quot; )

# 服务监听端口
server.port = 80

# 进程id记录位置
server.pid-file = &quot;/var/run/lighttpd.pid&quot;

# virtual directory listings 如果没有找到index文件就列出目录。建议disable。
dir-listing.activate = &quot;disable&quot;

# 服务运行使用的用户及用户组
server.username = &quot;www&quot;
server.groupname = &quot;www&quot;

# gzip压缩存放的目录以及需要压缩的文件类型
compress.cache-dir = &quot;/tmp/lighttpd/cache/compress/&quot;
compress.filetype = (&quot;text/plain&quot;, &quot;text/html&quot;)

# fastcgi module
# for PHP don&#039;t forget to set cgi.fix_pathinfo = 1 in the php.ini
$HTTP[&quot;url&quot;] =~ &quot;\.php$&quot; {
    proxy-core.balancer = &quot;round-robin&quot;
    proxy-core.allow-x-sendfile = &quot;enable&quot;
#    proxy-core.check-local = &quot;enable&quot;
    proxy-core.protocol = &quot;fastcgi&quot;
    proxy-core.backends = ( &quot;unix:/tmp/php-fastcgi1.sock&quot;,&quot;unix:/tmp/php-fastcgi2.sock&quot; )
    proxy-core.max-pool-size = 16
}

# 权限控制
auth.backend = &quot;htpasswd&quot;
auth.backend.htpasswd.userfile = &quot;/var/www/htpasswd.userfile&quot;

# 基于 evhost 的虚拟主机 针对域名
$HTTP[&quot;host&quot;] == &quot;a.lostk.com&quot; {
    server.document-root = &quot;/var/www/lostk/&quot;
    server.errorlog = &quot;/var/log/lighttpd/lostk-error.log&quot;
    accesslog.filename = &quot;/var/log/lighttpd/lostk-access.log&quot;

    # 设定文件过期时间
    expire.url = (
        &quot;/css/&quot; =&gt; &quot;access 2 hours&quot;,
        &quot;/js/&quot; =&gt; &quot;access 2 hours&quot;,
    )

    # url 跳转
    url.redirect = (
        &quot;^/$&quot; =&gt; &quot;/xxx/index.html&quot;,
    )

    # url 重写 (cakephp可用)
    url.rewrite = (
        &quot;^/(css|js)/(.*)$&quot; =&gt; &quot;/$1/$2&quot;,
        &quot;^/([^.]+)$&quot; =&gt; &quot;/index.php?url=$1&quot;,
    )

    # 权限控制
    auth.require   = ( &quot;&quot; =&gt;
         (
            &quot;method&quot; =&gt; &quot;basic&quot;,
            &quot;realm&quot; =&gt; &quot;admin only&quot;,
            &quot;require&quot; =&gt; &quot;user=admin1|user=admin2&quot;  # 允许的用户, 用户列表文件 在上面配置的auth.backend.htpasswd.userfile 里
        ),
    )
}

# 针对端口的虚拟主机
$SERVER[&quot;socket&quot;] == &quot;192.168.0.1:8000&quot; {
    server.document-root        = &quot;/var/www/xxx/&quot;
    server.errorlog = &quot;/var/log/lighttpd/test-error.log&quot;
    accesslog.filename = &quot;/var/log/lighttpd/test-access.log&quot;

   # ...
}
</pre>
<p># fastcgi 以及 lighttpd 启动脚本</p>
<pre class="brush: bash">
#!/bin/sh
startphp(){
    /etc/lighttpd/spawn-php1.sh
    /etc/lighttpd/spawn-php2.sh
}

starthttpd(){
    /usr/local/lighttpd/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
}

start(){
    startphp
    starthttpd
}

stopphp(){
    killall php-cgi
}

stophttpd(){
    killall lighttpd
}

stop(){
    killall php-cgi lighttpd
}

case &quot;$1&quot; in
    start)
        start
        ;;
    startphp)
        startphp
        ;;
    starthttpd)
        starthttpd
        ;;
    stop)
        stop
    ;;
    stopphp)
        stopphp
        ;;
    stophttpd)
        stophttpd
        ;;
    *)
        echo &quot;Usage: lighttpd {start|stop|startphp|starthttpd|stopphp|stophttpd}&quot;
        RETVAL=1
esac
</pre>
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2010-03-08 -- <a href="http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/" title="nginx+factcgi 下使用 ob_flush">nginx+factcgi 下使用 ob_flush</a> (0)</li><li>2010-05-21 -- <a href="http://www.lsproc.com/blog/nginx_php_pathinfo_securit/" title="Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo">Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo</a> (2)</li><li>2010-04-12 -- <a href="http://www.lsproc.com/blog/nginx_userid_decode/" title="nginx userid 模块客户端 cookie 解码">nginx userid 模块客户端 cookie 解码</a> (0)</li><li>2008-03-18 -- <a href="http://www.lsproc.com/blog/configure_lamp/" title="lamp 相关配置 [Debian]">lamp 相关配置 [Debian]</a> (2)</li><li>2008-01-17 -- <a href="http://www.lsproc.com/blog/use_dbprefix_in_zendframework/" title="ZendFramework 使用数据表前缀">ZendFramework 使用数据表前缀</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
