zoukankan      html  css  js  c++  java
  • Lighttpd 配置fastcgi

    http://my.oschina.net/davehe/blog/108107



             在配置之前,先了解下lighttpd和fastcgi.      

            Lighttpd相信大家都使用过,它是一个具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点。lighttpd是众多OpenSource轻量级的web server中较为优秀的一个。支持FastCGI, CGI, Auth, 输出压缩(output compress), URL重写, Alias等重要功能。
              Lighttpd提供了一种外部程序调用的接口,即FastCGI接口。这是一种独立于平台和服务器的接口,它介于Web应用程序和Web服务器之间。使用fastcgi方式运行php,它会使用很少的PHP进程响应很大的并发量。

    Fastcgi的优点在于:

             · 从稳定性上看, fastcgi是以独立的进程池运行来cgi,单独一个进程死掉,系统可以很轻易的丢弃,然后重新分配新的进程来运行逻辑.

             · 从安全性上看, fastcgi和宿主的server完全独立, fastcgi怎么down也不会把server搞垮,

             · 从性能上看, fastcgi把动态逻辑的处理从server中分离出来, 大负荷的IO处理还是留给宿主server, 这样宿主server可以一心一意操作IO,对于一个普通的动态网页来说, 逻辑处理可能只有一小部分, 大量的图片等静态IO处理完全不需要逻辑程序的参与.

         · 从扩展性上讲, fastcgi是一个中立的技术标准, 完全可以支持任何语言写的处理程序php,java,python

            

            什么是CGI:

    CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上。 CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。如php,perl,tcl等.

           什么是FastCGI:

    FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次(这是CGI最为人诟病的fork-and-execute 模式)。它还支持分布式的运算, 即 FastCGI 程序可以在网站服务器以外的主机上执行并且接受来自其它网站服务器来的请求。
    FastCGI是语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性、Fail- Over特性等等。

           FastCGI与CGI特点:

    1、如CGI,FastCGI也具有语言无关性. 2、如CGI, FastCGI在进程中的应用程序,独立于核心web服务器运行,提供了一个比API更安全的环境。(API是把应用程序的代码与核心的web服务器链接在一起,这意味着在一个错误的API的应用程序可能会损坏其他应用程序或核心服务器; 恶意的API的应用程序代码甚至可以窃取另一个应用程序或核心服务器的密钥。)
    3、FastCGI技术目前支持语言有 PHP、C/C++、Java、Perl、Tcl、Python、SmallTalk、Ruby等。相关模块在Apache,IIS, Lighttpd,Nginx等流行的服务器上也是可用的。
    4、如CGI,FastCGI的不依赖于任何Web服务器的内部架构,因此即使服务器技术的变化, FastCGI依然稳定不变。

           FastCGI的工作原理:

    1、Web Server启动时载入FastCGI进程管理器(IIS ISAPI或Apache lighttpd Module)
    2、FastCGI进程管理器自身初始化,启动多个CGI解释器进程(可见多个php-cgi)并等待来自Web Server的连接。
    3、当客户端请求到达Web Server时,FastCGI进程管理器选择并连接到一个CGI解释器。Web server将CGI环境变量和标准输入发送到FastCGI子进程php-cgi。
    4、FastCGI子进程完成处理后将标准输出和错误信息从同一连接返回Web Server。当FastCGI子进程关闭连接时,请求便告处理完成。FastCGI子进程接着等待并处理来自FastCGI进程管理器(运行在Web Server中)的下一个连接。 在CGI模式中,php-cgi在此便退出了。
    在上述情况中,你可以想象CGI通常有多慢。每一个Web请求PHP都必须重新解析php.ini、重新载入全部扩展并重初始化全部数据结构。使用FastCGI,所有这些都只在进程启动时发生一次。一个额外的好处是,持续数据库连接(Persistent database connection)可以工作。


           FastCGI的优点:

           1. PHP脚本运行速度更快(3到30倍)。PHP解释程序被载入内存而不用每次需要时从存储器读取,极大的提升了依靠脚本运行的站点的性能。
           2. 需要使用更少的系统资源。由于服务器不用每次需要时都载入PHP解释程序,可以将站点的传输速度提升很高而不必增加cpu负担。数据库的持久连接也将可以起到它设计初的效果。
           3. 不需要对现有的代码作任何改变。

            FastCGI的不足:
    因为是多进程,所以比CGI多线程消耗更多的服务器内存,PHP-CGI解释器每进程消耗7至25兆内存。


          下面安装lighttpd以及相应模块.

    1.安装相应包

    root@10.1.6.200:~# apt-get install lighttpd php5-cgi php5-curl php5-gd php5-mcrypt php5-memcache php5-mysql

     2.配置fastcgi选项

    root@10.1.6.200:conf-available# pwd
    /etc/lighttpd/conf-available

    root@10.1.6.200:conf-available# vim 10-fastcgi.conf

    server.modules   += ( "mod_fastcgi" )
     
    ## Start an FastCGI server for php (needs the php5-cgi package)
    fastcgi.server    = ( ".php" =>
        ((
            "bin-path" => "/usr/bin/php-cgi",
            "socket" => "/tmp/php.socket",
            "max-procs" => 3,
            "idle-timeout" => 20,
            "bin-environment" => (
                "PHP_FCGI_CHILDREN" => "15",
                "PHP_FCGI_MAX_REQUESTS" => "10000"
            ),
            "bin-copy-environment" => (
                "PATH", "SHELL", "USER"
            ),
            "broken-scriptfilename" => "enable"
        )),
     
        "advertise.fcgi" =>
            ((
              "bin-path" => "/opt/online/cgi-bin/advertise.fcgi",
              "socket" => "/tmp/hello.py.socket",
              "min-procs" => 15,
              "max-procs" => 30,
              "bin-environment" => (
                  "REAL_SCRIPT_NAME" => ""
                  ),
              "check-local" => "disable"
             ))
    )


    bin-path:本地FastCGI二进制程序的路径,当本地没有FastCGI正在运行时,会启动这个FastCGI程序。

    socket:unix-domain-socket所在路径。

    PHP_FCGI_CHILDREN:子进程数

    check_local:这是一个可选项,默认是enable。如果是enable,那么server会首先在本地(server.document-root)目录中检查被请求的文件是否存在,如果不存在,则给用户返回404(Not Found),而不会把这个请求传递给FastCGI。如果是disable,那么server不会检查本地文件,而是直接将请求转发给FastCGI。(disable的话,server从某种意义上说就变为了一个转发器)

    max-procs:主进程数,设置多少个FastCGI进程被启动

    bin-environment:在FastCGI进程启动时设置一个环境变量

    bin-copy-environment:清除环境,并拷贝指定的变量到全新的环境中。


    3.加载fastcgi模块

    root@10.1.6.200:conf-available# lighty-enable-mod
    Available modules: auth cgi fastcgi proxy rrdtool simple-vhost ssi ssl status userdir
    Already enabled modules: fastcgi
    Enable module: fastcgi
    Enabling fastcgi: already enabled
    Run /etc/init.d/lighttpd force-reload to enable changes


    root@10.1.6.200:lighttpd# pwd
    /etc/lighttpd
    root@10.1.6.200:lighttpd# vim lighttpd.conf
    server.modules              = (
                "mod_access",
                "mod_alias",
                "mod_accesslog",
                "mod_compress",
                "mod_fastcgi"
    )
    server.document-root       = "/opt/online/cgi-bin"


    root@10.1.6.200:conf-available# /etc/init.d/lighttpd restart
    Stopping web server: lighttpd.
    Starting web server: lighttpd.


    root@10.1.6.200:conf-available#ps -ef
    www-data 26344     1  0 12:05 ?        00:00:01 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
    www-data 26345 26344  0 12:05 ?        00:00:00 /usr/bin/php-cgi
    www-data 26352 26345  0 12:05 ?        00:00:00 /usr/bin/php-cgi
    www-data 26353 26345  0 12:05 ?        00:00:00 /usr/bin/php-cgi
    www-data 26393 26384  0 12:05 ?        00:00:00 /usr/bin/php-cgi
    www-data 26394 26384  0 12:05 ?        00:00:00 /usr/bin/php-cgi
    www-data 26395 26384  0 12:05 ?        00:00:00 /usr/bin/php-cgi
    www-data 26396 26384  0 12:05 ?        00:00:00 /usr/bin/php-cgi
    www-data 26397 26384  0 12:05 ?        00:00:00 /usr/bin/php-cgi
    www-data 26398 26384  0 12:05 ?        00:00:00 /usr/bin/php-cgi
    www-data 26399 26384  0 12:05 ?        00:00:00 /usr/bin/php-cgi
    www-data 26400 26384  0 12:05 ?        00:00:00 /usr/bin/php-cgi
    ...
    www-data 26401 26344  0 12:05 ?        00:00:00 /opt/online/cgi-bin/advertise.fcgi
    www-data 26402 26344  0 12:05 ?        00:00:00 /opt/online/cgi-bin/advertise.fcgi
    www-data 26403 26344  0 12:05 ?        00:00:00 /opt/online/cgi-bin/advertise.fcgi
    www-data 26404 26344  0 12:05 ?        00:00:00 /opt/online/cgi-bin/advertise.fcgi
    www-data 26405 26344  0 12:05 ?        00:00:00 /opt/online/cgi-bin/advertise.fcgi
    www-data 26406 26344  0 12:05 ?        00:00:00 /opt/online/cgi-bin/advertise.fcgi
    www-data 26407 26344  0 12:05 ?        00:00:00 /opt/online/cgi-bin/advertise.fcgi
    www-data 26408 26344  0 12:05 ?        00:00:00 /opt/online/cgi-bin/advertise.fcgi
    www-data 26409 26344  0 12:05 ?        00:00:00 /opt/online/cgi-bin/advertise.fcgi
    ...


    ++++++++++++ 附lighttpd配置参数:

    server.document-root   = "/var/www/"    #网站根目录 映射在机器上的物理路径

    index-file.names  = ( "index.php", "index.html",
                                "index.htm", "default.htm",
                                "index.lighttpd.html" ) #如果网站目录中出现以下文件名,不用指定文件名便可直接访问

    server.username = "www-data"              #Lighttpd 进程的归属用户

    server.groupname  = "www-data"           #Lighttpd 进程的归属群组

    server.port   = 80                               #绑定到端口默认为 80

    server.bind   = "localhost"                   #绑定到地址默认为所有

    accesslog.filename  = "/var/log/lighttpd/access.log"    #访问日志路径

    server.errorlog  = "/var/log/lighttpd/error.log"  # 错误日志路径

    url.access-deny  = ( "~", ".inc" )   #禁止访问以下文件

    dir-listing.activate       = "enable"    #与目录列表相关的设置

    dir-listing.encoding        = "utf-8"   #与目录列表相关的设置

    server.dir-listing          = "enable"   #与目录列表相关的设置

    路径绑定
    将一个路径,映射到网站目录中
    ## 启用 mod_alias 模块
    servers.modules +=( "mod_alias")    
    #将 /home/dave 映射到 http://host/example
    alias.url += ( "/example" => "/home/dave" )


    虚拟主机
    Lighttpd 可以建立多个虚拟主机,绑定在不同的网络接口
    #启用 mod_evhost 模块
    servers.modules +=( "mod_evhost")
    #虚拟主机绑定的网络接口
    $HTTP["host"] == "10.1.1.200"
    {
    #虚拟主机可以使用独立的选项
    dir-listing.activate       = "enable"
    dir-listing.encoding       = "utf8"
    dir-listing.show-readme  = "enable"
    #虚拟主机根目录
    server.document-root = "/home/user/html"
    #虚拟主机路径绑定
    alias.url = ( "/download/" => "/home/user/downloads/" )
    alias.url += ( "/pictures/" => "/home/user/pictures/" )
    }

    例如:

    $HTTP["host"] == "www.test.com"  {
    server.name = "www.test.com"
    server.document-root = "/var/www/www.test.com"
    server.errorlog = "/var/log/lighttpd/www.test.com_error.log"
    accesslog.filename = "/var/log/lighttpd/www.test.com_access.log"
    }

    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
    阅读(64) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~
    评论热议
  • 相关阅读:
    Java集合框架:Collections工具类
    百度编辑器多图上传返回图片绝对路径问题
    iOS开发中“此证书的签发者无效”的解决方式
    codeblocks如何watch指针
    codeblocks如何watch数组
    printf不支持%lf
    doxygen可以生成C/C++代码的文档(根据注释)
    codeblocks中右键源文件没有Rename选项?
    codeblocks中给GCC编译器加参数
    codeblocks设置当前行高亮
  • 原文地址:https://www.cnblogs.com/ztguang/p/12649079.html
Copyright © 2011-2022 走看看