免费注册 查看新帖 |

Chinaunix

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

[Web] lighttpd 静态文件web服务器配置部署指南 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-19 01:38 |只看该作者 |倒序浏览
部署概述:
1、        测试环境:
操作系统为REHL4,部署目标目录为/home/lighttpd;
2、        使用版本:
使用lighttpd1.4.19版;
3、        优化说明:
对etag头配置进行修改,,根据文件更新频繁程度,对不同的文件目录采用不同的expire策略;对于静态html/js/css文件均启用http压缩支持。主进程仅包含运行必须的模块,其他模块均排除,减少进程资源。
修改etag头是为了在集群环境中能够对相同的文件不用进行重复的请求,Lighttpd中可以通过设置etag.use-inode="disable",只设置mtime和size来解决这个问题,不过研发那边既然要求去掉Etag,那么就把Etag disable掉吧。(原来以为是可以将etag的输出disable掉的,但是如果同时启用了compress模块,会有问题,compress仍然会输出etag头,看了网上的一些文档,似乎可以通过hack手段修改源码来解决这个问题,但是没有时间去测试这个方法的稳定性,只得作罢。但是需要注意集群环境中文件的时间和大小需要一致)
4、        测试说明:
对大日志(超过2G的日志)、系统性能、Etag和expire头均进行过基本的测试;
关于压缩功能的测试,需要说明一点,lighttpd的compress模块只对超过一定大小的文件才启用压缩,这个具体的文件大小也没有找到相关的说明。
5、        部署拓扑:
使用四层交换机对客户的静态文件请求进行负载均衡调度,服务器之间用rsync来进行文件同步。

一、编译:
1、下载安装包:
         安装mod cache需要打补丁,使用的lighttpd版本为lighttpd-1.4.19,对应的patch版本为lighttpd-1.4.19.modcache.v.1.4.4.patch (mod cache是一个类似于squid,可以通过lighttpd来实现squid的功能,考虑到将来的扩展和部署,可以在安装是将mod cache一并安装) 。
        安装 mod cache需要gamin和pcre,也一并下载安装。按照下面的安装方式,所有的功能模块都是安装在同一个目录下面,这样便于整个备份和删除。
        安装 mod cache 需要打补丁,需要一并下载。
       
        创建临时代码目录,存放下载源代码。



2、安装过程:

安装gamin源码包
cd /tmp/lighttpd/
tar xvzf gamin-0.0.17.tar.gz
cd gamin-0.0.17
./configure --prefix=/home/lighttpd/source/
make
make install


安装pcre源码包
cd /tmp/lighttpd/
tar xvjf pcre-6.6.tar.bz2
cd pcre-6.6
./configure --prefix=/home/lighttpd/source/
make
make install

   
安装mod_cache补丁并安装lighttpd
cd /tmp/lighttpd/
tar xvzf lighttpd-1.4.19.tar.gz
cp lighttpd-1.4.19.modcache.v.1.4.4.patch lighttpd-1.4.19/src/
cd lighttpd-1.4.19/src/
patch < lighttpd-1.4.19.modcache.v.1.4.4.patch
cd ..
env PATH="/home/lighttpd/source/binPATH" CPPFLAGS="-I/home/lighttpd/source/include/" LDFLAGS="-L/home/lighttpd/source/lib" ./configure --prefix=/home/lighttpd --enable-static --without-ldap --without-mysql --without-openssl --without-kerberos5 --with-pcre --with-zlib --with-bzip2 --with-fam --disable-ipv6
make
make install


安装cronolog(用来对日志进行轮转,之所以使用cronolog是因为可以对自然日进行日志轮转)
cd /tmp/lighttpd/
tar xvzf cronolog-1.6.2.tar.gz
cd cronolog-1.6.2
修改src/cronolog.c
vi src/cronolog.c
#查找log_fd = open((pfilename, O_WRONLY|O_CREAT|O_APPEND, FILE_MODE)
#修改为log_fd = open64(pfilename, O_WRONLY|O_CREAT|O_APPEND, FILE_MODE)
#一共需要修改两处,此处的修改是为了进行大于2G以上的大日志文件的支持,修改后测试成功
#然后进行编译
./configure --prefix=/home/lighttpd/cronolog/
make
make install


之所以这样编译是因为这样弄,系统会干净很多,把所有的库文件和源文件都放到指定的目录下,这样将来迁移或者有服务配置上的变更都方便很多。

[ 本帖最后由 mars531706 于 2008-11-5 10:34 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-09-19 01:43 |只看该作者
二、配置:
1、创建站点文件目录、日志目录以及配置文件目录。
       
         mkdir -pv /home/lighttpd/conf /home/lighttpd/htdocs /home/lighttpd/logs

2、创建配置文件:
        使用如下命令创建配置文件
     touch /home/lighttpd/conf/lighttpd.conf

        编辑配置文件如下
        vi /home/lighttpd/conf/lighttpd.conf


        配置文件内容
#### 加载以下模块,请尤其注意这里的模块之间的顺序,一定要严格按照以下顺序排列,如果需要加入其他模块,请测试
         server.modules              = (                                    
                                  "mod_expire",                     
                                  "mod_staticfile",                  
                                  "mod_compress",                    
                                  "mod_access",                     
                                  "mod_accesslog" ,                  
                                  )                                 
                                 
#### 定义站点根目录
  server.document-root        = "/home/lighttpd/htdocs/"        
  
#### 定义错误输出日志
  server.errorlog             = "/home/lighttpd/logs/error.log"
#### 定义运行时的进程文件,便于脚本控制
  server.pid-file            = "/home/lighttpd/logs/lighttpd.pid"



#### 定义默认站点文件
  index-file.names            = ( "index.php", "index.html",         
                                  "index.htm", "default.htm" )      
#### 定义mime类型
mimetype.assign             = (
  ".pdf"          =>      "application/pdf",
  ".sig"          =>      "application/pgp-signature",
  ".spl"          =>      "application/futuresplash",
  ".class"        =>      "application/octet-stream",
  ".ps"           =>      "application/postscript",
  ".torrent"      =>      "application/x-bittorrent",
  ".dvi"          =>      "application/x-dvi",
  ".gz"           =>      "application/x-gzip",
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
  ".swf"          =>      "application/x-shockwave-flash",
  ".tar.gz"       =>      "application/x-tgz",
  ".tgz"          =>      "application/x-tgz",
  ".tar"          =>      "application/x-tar",
  ".zip"          =>      "application/zip",
  ".mp3"          =>      "audio/mpeg",
  ".m3u"          =>      "audio/x-mpegurl",
  ".wma"          =>      "audio/x-ms-wma",
  ".wax"          =>      "audio/x-ms-wax",
  ".ogg"          =>      "application/ogg",
  ".wav"          =>      "audio/x-wav",
  ".gif"          =>      "image/gif",
  ".jar"          =>      "application/x-java-archive",
  ".jpg"          =>      "image/jpeg",
  ".jpeg"         =>      "image/jpeg",
  ".png"          =>      "image/png",
  ".xbm"          =>      "image/x-xbitmap",
  ".xpm"          =>      "image/x-xpixmap",
  ".xwd"          =>      "image/x-xwindowdump",
  ".css"          =>      "text/css",
  ".html"         =>      "text/html",
  ".htm"          =>      "text/html",
  ".js"           =>      "text/javascript",
  ".asc"          =>      "text/plain",
  ".c"            =>      "text/plain",
  ".cpp"          =>      "text/plain",
  ".log"          =>      "text/plain",
  ".conf"         =>      "text/plain",
  ".text"         =>      "text/plain",
  ".txt"          =>      "text/plain",
  ".dtd"          =>      "text/xml",
  ".xml"          =>      "text/xml",
  ".mpeg"         =>      "video/mpeg",
  ".mpg"          =>      "video/mpeg",
  ".mov"          =>      "video/quicktime",
  ".qt"           =>      "video/quicktime",
  ".avi"          =>      "video/x-msvideo",
  ".asf"          =>      "video/x-ms-asf",
  ".asx"          =>      "video/x-ms-asf",
  ".wmv"          =>      "video/x-ms-wmv",
  ".bz2"          =>      "application/x-bzip",
  ".tbz"          =>      "application/x-bzip-compressed-tar",
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
  # default mime type
  ""              =>      "application/octet-stream",
)
                 
#### 定义轮转日志                                      
                accesslog.filename  = "| /home/lighttpd/cronolog/sbin/cronolog /home/lighttpd/logs/access_%Y%m%d.log"
               
#  url.access-deny             = ( "~", ".inc" )                     

#### expire头配置,对html目录,js目录,以及css目录分别设置1、2、3个小时的超时时间。(请根据实际情况合理配置)

  $HTTP["url"] =~ "/testhtml/"{                                          
          expire.url = ("" => "access 1 hours")                     
          }                                                         
  $HTTP["url"] =~ "/testjs/"{                                            
          expire.url = ("" => "access 2 hours")                     
          }                                                         
  $HTTP["url"] =~ "/testcss/"{                                          
          expire.url = ("" => "access 3 hours")                     
          }                                                         

#### static 模块配置,禁止Etag
#  static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )        
#  static-file.etags = "disable"
  etag.use-inode = "disable"

#### compress 模块配置  
#       定义压缩文件的存放目录
  compress.cache-dir = "/home/lighttpd/compress"               
# 定义压缩的mime类型,对css、html和js文件进行压缩
  compress.filetype = ("text/html","text/css","text/javascript")                     
  
#端口设置为2080,避免和apache冲突
  server.port = 2080
  server.network-backend = "writev"                                 
  server.max-keep-alive-requests = 4                                 
  server.max-keep-alive-idle = 5                                    
  server.max-fds = 3072                                             
  server.event-handler = "linux-sysepoll"

3、启动服务器:
编辑lighttpctl脚本来进行服务的启动和停止的控制:       
vi /home/lighttpd/sbin/lightctl
chmod 700 /home/lighttpd/sbin/lightctl

       
编辑lighttpctl脚本如下:       
#!/bin/bash

ARGV="$@"

if [ $# != 1 ] ; then
        echo "Usage: lighttpctl stop|start"
        exit
fi

cmd=$ARGV
exefile="/home/lighttpd/sbin/lighttpd"
echo $cmd

case $cmd in
stop)
    if [ !  -f /home/lighttpd/logs/lighttpd.pid ] ; then
        echo "The service is not running."
        exit
    fi
    kill -9 `cat /home/lighttpd/logs/lighttpd.pid`
    rm -rf /home/lighttpd/logs/lighttpd.pid
    ERROR=$?
    ;;
start)
    `$exefile -f /home/lighttpd/conf/lighttpd.conf`
    ERROR=$?
    ;;
*)
    echo "Usage: lightctl stop|start"
    ERROR=$?
esac

exit $ERROR


/home/lighthttpd/sbin/lightctl start/stop启动停止服务

[ 本帖最后由 mars531706 于 2008-11-5 10:31 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-09-19 01:45 |只看该作者
三、性能测试:
         使用的测试工具为apache的ab。命令如下:
         ./ab -c 100 -n 10000 http://192.168.10.20/press.html


         文本文件性能比较:

apache                                                                                                                                                        Concurrency Level:      100                                             
  Time taken for tests:   8.651640 seconds                                 
  Complete requests:      10000                                            
  Failed requests:        0                                                
  Write errors:           0                                                
  Total transferred:      69542794 bytes                                   
  HTML transferred:       67091569 bytes                                   
  Requests per second:    1155.85 [#/sec] (mean)                           
  Time per request:       86.516 [ms] (mean)                              
  Time per request:       0.865 [ms] (mean, across all concurrent requests)
  Transfer rate:          7849.61 [Kbytes/sec] received                        
  
  lighttpd
  Concurrency Level:      100                                             
  Time taken for tests:   5.815974 seconds                                 
  Complete requests:      10000                                            
  Failed requests:        0                                                
  Write errors:           0                                                
  Total transferred:      69658688 bytes                                   
  HTML transferred:       67078172 bytes                                   
  Requests per second:    1719.40 [#/sec] (mean)                           
  Time per request:       58.160 [ms] (mean)                              
  Time per request:       0.582 [ms] (mean, across all concurrent requests)
  Transfer rate:          11696.41 [Kbytes/sec] received                  
  Transfer rate:          1757.90 [Kbytes/sec] received                    
  Transfer rate:          31765.63 [Kbytes/sec] received                  

  
  图片性能比较:
  apache
  Concurrency Level:      100                                             
  Time taken for tests:   33.561399 seconds                                
  Complete requests:      10000                                            
  Failed requests:        0                                                
  Write errors:           0                                                
  Total transferred:      1091685708 bytes                                 
  HTML transferred:       1089177958 bytes                                 
  Requests per second:    297.96 [#/sec] (mean)                           
  Time per request:       335.614 [ms] (mean)                              
  Time per request:       3.356 [ms] (mean, across all concurrent requests)
  Transfer rate:          31765.63 [Kbytes/sec] received                  
  
  lighttpd
  Concurrency Level:      100                                             
  Time taken for tests:   28.988655 seconds                                
  Complete requests:      10000                                            
  Failed requests:        0                                                
  Write errors:           0                                                
  Total transferred:      1093736456 bytes                                 
  HTML transferred:       1091357213 bytes                                 
  Requests per second:    344.96 [#/sec] (mean)                           
  Time per request:       289.887 [ms] (mean)                              
  Time per request:       2.899 [ms] (mean, across all concurrent requests)
  Transfer rate:          36845.52 [Kbytes/sec] received  

从上面来看,apache的Time per request分别是0.865 [ms]和3.356 [ms],而lighttpd分别是0.582 [ms]和2.899 [ms]。似乎apache效率会低很多,但是从在虚拟机上配置的实际测试情况来看,对于图片文件,apache和lighttpd的差别并不是非常大(这个差别值只是一个个例,从更到的采样结果来看,似乎是差距要略小一些),当然效率上的优势还是略微倾向于lighttpd这边。对于比较小的文本文件,lighttpd的效率优势比较明显(这个效率的问题可能还是需要在好点的机器上测试才明显)。

[ 本帖最后由 mars531706 于 2008-9-19 01:48 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-09-20 00:20 |只看该作者
四层交换机啊

论坛徽章:
0
5 [报告]
发表于 2008-09-20 07:22 |只看该作者
up 一下

论坛徽章:
0
6 [报告]
发表于 2008-09-23 08:59 |只看该作者

回复 #4 aoegiss 的帖子

这个交换机估计都是七层的。
交换机不是我的势力范围,哈哈。
#端口设置为2080,避免和apache冲突
  server.port = 2080
  server.network-backend = "writev"                                 
  server.max-keep-alive-requests = 4                                 
  server.max-keep-alive-idle = 5                                    
  server.max-fds = 3072                                             
  server.event-handler = "linux-sysepoll"

这里的2080是用LVS来映射的VIP,下面的配置基本上是参照官网的Performance
需要根据实际的情况调节测试,大家有什么优化的意见请提出来做参考
我在实际的测试过程中发现lighttpd相对于apache来说性能上是有优势的
但是和有的文档中动不动就说2-3倍的性能优势来说,没有发现能达到如此高的性能

[ 本帖最后由 mars531706 于 2008-9-23 10:17 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2008-09-23 11:26 |只看该作者

论坛徽章:
0
8 [报告]
发表于 2008-09-23 22:30 |只看该作者

论坛徽章:
0
9 [报告]
发表于 2008-09-24 12:37 |只看该作者
呵呵,不错不错
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP