免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3971 | 回复: 2
打印 上一主题 下一主题

[Web] lighttpd配置介绍 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-12 16:18 |只看该作者 |倒序浏览
一,为什么要使用lighttpd?
    apache不可以吗?
    在支持纯静态的对象时,比如图片,文件等 ,
    lighttpd速度更快,更理想
    至于它和apache的比较,很多文档,大家可以google一下

二,从何处下载lighttpd?
   
http://www.lighttpd.net/download/
    这个是它的官方站

三,如何安装?
    1,编译安装
      ./configure --prefix=/usr/local/lighttpd
      make
      make install

configure完毕以后,会给出一个激活的模块和没有激活模块的清单,可以检查一下,是否自己需要的模块都已经激活,在enable的模块中一定要有“mod_rewrite”这一项,否则重新检查pcre是否安装。
    2,编译后配置
      cp doc/sysconfig.lighttpd /etc/sysconfig/lighttpd
      mkdir /etc/lighttpd
      cp doc/lighttpd.conf /etc/lighttpd/lighttpd.conf

      如果你的Linux是RedHat/CentOS,那么:
      cp doc/rc.lighttpd.redhat /etc/init.d/lighttpd
      如果你的Linux是SuSE,那么:
      cp doc/rc.lighttpd /etc/init.d/lighttpd
      其他Linux发行版本可以自行参考该文件内容进行修改。
      然后修改/etc/init.d/lighttpd,把
      LIGHTTPD_BIN=/usr/sbin/lighttpd
      改为
      LIGHTTPD_BIN=/usr/local/lighttpd/sbin/lighttpd

      此脚本用来控制lighttpd的启动关闭和重起:
      /etc/init.d/lighttpd start
      /etc/init.d/lighttpd stop
      /etc/init.d/lighttpd restart
    3,配置
      修改/etc/lighttpd/lighttpd.conf
      1)server.modules
      取消需要用到模块的注释,mod_rewrite,mod_access,mod_fastcgi,mod_simple_vhost,mod_cgi,      mod_compress,mod_accesslog是一般需要用到的。
      我们放开                               "mod_rewrite"
                                             "mod_compress",

      2)server.document-root, server.error-log,accesslog.filename需要指定相应的目录
         server.document-root        = "/www/phc/html/"
         mkdir /usr/local/lighttpd/logs
         chmod 777 /usr/local/lighttpd/logs/
          touch /usr/local/lighttpd/logs/error.log
          chmod 777 /usr/local/lighttpd/logs/error.log

         server.errorlog             = "/usr/local/lighttpd/logs/error.log"
accesslog.filename             = "|/usr/sbin/cronolog /usr/local/lighttpd/logs/%Y/%m/%d/accesslog.log"

      3)用什么权限来运行lighttpd
         server.username            = "nobody"
         server.groupname           = "nobody"
         从安全角度来说,不建议用root权限运行web server,可以自行指定普通用户权限。

       4)静态文件压缩
          mkdir /usr/local/lighttpd/compress
          chmod 777 /usr/local/lighttpd/compress/
compress.cache-dir         = "/usr/local/lighttpd/compress/"
compress.filetype          = ("text/plain", "text/html","text/javascript","text/css")

          可以指定某些静态资源类型使用压缩方式传输,节省带宽,
          对于大量AJAX应用来说,可以极大提高页面加载速度。

        5)server.port                = 81
        6)#$HTTP["url"] =~ ".pdf$" {
    131 # server.range-requests = "disable"
    132 #}


    4,优化
     1 最大连接数

            默认是1024
            修改 server.max-fds,大流量网站推荐2048.

            因为lighttpd基于线程,而apache(MPM-prefork)基于子进程,
            所以apache需要设置startservers,maxclients等,这里不需要
     2 stat() 缓存

               stat() 这样的系统调用,开销也是相当明显的.
              缓存能够节约时间和环境切换次数(context switches)

              一句话,lighttpd.conf加上
              server.stat-cache-engine = “fam”

              lighttpd还另外提供simple(缓存1秒内的stat()),disabled选项.
              相信没人会选disabled吧.
      3 常连接(HTTP Keep-Alive)

             一般来说,一个系统能够打开的文件个数是有限制的(文件描述符限制)
            常连接占用文件描述符,对非并发的访问没有什么意义.

            (文件描述符的数量和许多原因有关,比如日志文件数量,并发数目等)
           这是lighttpd在keep-alive方面的默认值.
server.max-keep-alive-requests = 128
server.max-keep-alive-idle = 30

换言之,lighttpd最多可以同时承受30秒长的常连接,每个连接最多请求128个文件.
但这个默认值确实不适合非并发这种多数情况.

lighttpd.conf 中减小
server.max-keep-alive-requests
server.max-keep-alive-idle
两个值,可以减缓这种现象.

甚至可以关闭lighttpd keep-alive.
server.max-keep-alive-requests = 0
4 事件处理

对于linux kernel 2.6来说,没有别的可说
lighttpd.conf中加上这一句足矣
server.event-handler = “linux-sysepoll”

另外,
linux 2.4 使用 linux-rtsig
freebsd 使用 freebsd-kqueue
unix 使用 poll
5 网络处理

lighttpd 大量使用了 sendfile() 这样一个高效的系统调用.
减少了从应用程序到网卡间的距离.
(同时也减少了lighttpd对cpu的占用,这部分占用转嫁到内核身上了)

根据平台,可以设置不同的参数.
server.network-backend = “linux-sendfile”
(linux)
freebsd: freebsd-sendfile
unix: writev

如果有兴趣的话,也可以看看lighttpd在async io(aio)上的实现,仅限 lighttpd 1.5
(linux-aio-sendfile, posix-aio, gthread-aio)

此外,网络方面,核心的参数也需要适当进行修改,
这里就不需要详细说明了.

    5,启动
    6,配置日志
    logrotate & cronolog
logrotate很粗暴,直接把进程砍了然后移动日志
cronolog就是比较不错的方式.
lighttpd用法:
accesslog.filename = " |/usr/sbin/cronolog /var/log/lighttpd/%Y/%m/%d/access_XXXX.log"


    7,安装pcre
      从何处下载?
      
http://www.pcre.org/
       wget
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.4.tar.bz2
     安装过程:
         ./configure
  make clean
  make
  make install

8,支持fam
   gamin默认已安装了此包
   yum install gamin-devel

   另外配置时需添加:
   ./configure --prefix=/usr/local/lighttpd --with-fam

9,测试lighttpd的启动:
/usr/local/lighttpd/sbin/lighttpd -f /usr/local/lighttpd/etc/lighttpd.conf

论坛徽章:
0
2 [报告]
发表于 2011-02-17 22:58 |只看该作者
这个不错,标记一下。

论坛徽章:
0
3 [报告]
发表于 2013-05-15 18:03 |只看该作者
求大牛指点!!!lighttpd 怎么发布war包?我在lighttpd下部署个war包,访问的时候报404-Not Found!~错误
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP