Lighttpd的安装与配置、调优

理论篇

lighttpd是一个德国人领导的开源软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的webserver环境。具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点。lighttpd是众多OpenSource轻量级的webserver中较为优秀的一个。支持FastCGI, CGI, Auth, 输出压缩(output compress), URL重写, Alias等重要功能,而Apache之所以流行,很大程度也是因为功能丰富,在lighttpd上很多功能都有相应的实现了,这点对于apache的用户是非常重要的,因为迁移到lighttpd就必须面对这些问题。

 

    为什么使用Lighttpd,从性能方面来说,首先考虑单进程与多进程问题,这也是我为什么不想使用Apache的原因,多进程服务器的惊群问题,简单来说多进程服务(例如Apache)在一个请求发送时候会唤醒所有sleep的进程,但是最终服务的只有一个,在进程数目很多,请求频繁的时候这会造成一个大困扰,系统会忙于切换进程,如果看Top会发现CPU使用在system的比例很高,至于Lighttpd则使用单进程来响应 quest,使用 libeventpoll()作为event handler,如果在linux2.4下,还可以选择rtsig作为event handler。如果不使用大文件(>4G)支持,lighttpd使用sendfile()来发送文件,完全的zero-copy,在这方面性能表现是占优的。同时Lighttpd使用FastCGI来做动态脚本处理,经过实测,在性能表现上也可以接受。

 

    另外就是原先使用Apache的用户迁移成本会比较少,Lighttpd语法与Apache接近,并且核心功能基本实现,我关注的就是url_rewrite,PHP(这是当然的),alias,custom_log这几个模块,语法接近,并且也同样支持跟cronolog的管道配合日志,所以迁移之后功能都可以实现。另外我比较了ApacheLighttpdSuexec方案,虽然目前Lighttpd并没有官方模块来支持这一特性,但是就实现的难度来看,跟 Apache1.3乃至Apache2+MPM差不多……(如果希望安全的使用Apache2+MPM+PHP,一样需要 FastCGI模式)

 

    实用起来lighttpd确实非常不错,apache主要的问题是密集并发下,不断的fork()和切换,以及较高(相对于 lighttpd而言)的内存占用,使系统的资源几尽枯竭。而lighttpd采用了Multiplex技术,代码经过优化,体积非常小,资源占用很低,而且反应速度相当快。

 

利用apacherewrite技术,将繁重的cgi/fastcgi任务交给lighttpd来完成,充分利用两者的优点,现在那台服务器的负载下降了一个数量级,而且反应速度也提高了一个甚至是2个数量级!

扫描二维码关注公众号,回复: 392161 查看本文章

 

实践篇

系统RedHat

一、           安装

1, Pcre

(1)     下载。

ftp://ftp.pbone.net/mirror/www.startcom.org/AS-4.0.0/os/i386/StartCom/RPMS/pcre-devel-4.5-3.2.SEL4.i386.rpm

(2)     安装。rpm -ivh pcre-devel-4.5-3.2.SEL4.i386.rpm --nodeps(强制安装)

(3)     输入rpm -qa |grep pcre

  返回结果:pcre-4.5-3  pcre-devel-4.5-3.2.SEL4 表示已经成功安装

2, lighttpd

tar

cd

./configure --prefix=/opt/lighttpd

make

make install

 

二、           配置

1cp doc/sysconfig.lighttpd /etc/sysconfig/lighttpd

 

2cp doc/rc.lighttpd.redhat /etc/init.d/lighttpd   

注:/etc/init.d/lighttpd           此脚本用来控制启动、关闭和重起。

修改/etc/init.d/lighttpd,

lighttpd="/usr/sbin/lighttpd" 改为 lighttpd="/usr/local/lighttpd/sbin/lighttpd"

 

3,创建文件夹

1)创建网站根目录文件夹

mkdir /var -p /www/htdocs

2)创建日志文件夹

mkdir /usr/local/lighttpd/logs

3)创建静态文件压缩

mkdir /usr/local/lighttpd/compress

 

4, 修改主配置文件

mkdir /etc/lighttpd

注:/etc/lighttpd/lighttpd.conf    此文件是lighttpd的主配置文件

cp doc/lighttpd.conf /etc/lighttpd/lighttpd.conf

1)修改网站根目录路径

    server.document-root    = "/srv/www/htdocs/" (40)

改为  server.document-root    = "/var/www/htdocs/"

2)设置错误日志文件路径

server.errorlog = "/usr/local/lighttpd/logs/lighttpd.error.log" (43)

设置访问日志文件路径

accesslog.filename = "/usr/local/lighttpd/logs/access.log" (116)

 

3compress.cache-dir = "/usr/local/lighttpd/compress/"

compress.filetype = ("text/plain", "text/html","text/javascript","text/css")

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

 

4)把#server.port                = 81 前的#去掉

 

5server.modules(24)

取消需要用到模块的注释,mod_rewritemod_accessmod_fastcgi

mod_simple_vhostmod_cgimod_compressmod_accesslog是一般需要用到的。

"mod_fastcgi"前边的#去掉(24);把"mod_cgi"前边的#去掉(29)

 

6)用什么权限来运行lighttpd

server.username = "lighttpd"

server.groupname = "lighttpd"

从安全角度来说,不建议用root权限运行web server,可以指定普通用户权限。

 

7#$HTTP["url"] =~ "/.pdf$" {

# server.range-requests = "disable"

#}

 

5,创建lighttpd用户

useradd lighttpd

chown –R lighttpd:lighttpd /usr/local/lighttpd/

chown –R lighttpd:lighttpd /var/www/htdocs/

 

 

三、           启动并测试

1, 启动

/etc/init.d/lighttpd start  

/usr/local/lighttpd/sbin/lighttpd -f /usr/local/lighttpd/etc/lighttpd.conf

相关指令一览:

/etc/init.d/lighttpd stop

/etc/init.d/lighttpd restart

/etc/init.d/lighttpd status

如果你希望服务器启动的时候就启动lighttpd,那么:chkconfig lighttpd on

2, 测试

/var/www/htdocs里建一个index.html

然后在浏览器里输入192.168.233.142:81

输出:hello,I am lighttpd!

测试成功!~~~

 

 

四、           总结

服务器软件至今一共有3种,分别为apache,lighttpd,nginx。他们之间的共同点是

1,  都需要创建网站根目录文件夹

2,  都需要各自用户权限,而不是root权限。

3,  主配置文件是修改重点。其实这一点几乎对所有的软件都是正确的。

他们的区别是:apache+mysql+php(mod),而lighttpd/nginx+mysql+php(fastcgi),在安装后续软件mysql,php时需注意。

 

===============================

 

如何在centos下编译安装lighttpd

1.环境: 
centos 5.4 
lighttpd 1.4.25

 

2.安装开发工具和开发库 
yum groupinstall "Development Tools" "Development Libraries"

 

3.为支持memcache  支持cache-fam,安装相关组件 
yum -y install gamin-devel  libevent-devel pcre-devel libmemcache-devel

注:安装rpmforge库才能用yum安装libmemcache-devel

 

4.lighttpd下载地址 
http://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.25.tar.gz

 

5.配置 
./configure –prefix=/usr/local/lighttpd –with-fam –with-memcache

 

6.编译安装 
make 
make install

 

7.建立相关目录 
mkdir /var/log/lighttpd

mkdir /etc/lighttpd

cp  doc/lighttpd.conf  /etc/lighttpd/lighttpd.conf

cp doc/rc.lighttpd.redhat  /etc/init.d/lighttpd

chkconfig lighttpd on

ln -s /usr/local/lighttpd/sbin/lighttpd   /usr/sbin/lighttpd 

 

8.修改lighttpd配置文件,根据自己的需求进行更改 
vi /etc/lighttpd/lighttpd.conf

 

9.启动lighttpd 
service lighttpd start

 

 

=============================

centos5.3下安装配置lighttpd优化配置

vi /etc/lighttpd/lighttpd.conf

#通用优化设置 
#server.max-worker=4 
server.stat-cache-engine = "simple" 
server.network-backend = "linux-sendfile" 
server.event-handler = "linux-sysepoll" 
server.max-keep-alive-requests = 0 
server.max-keep-alive-idle=0 
server.tag="Apache 2.4" 
server.max-fds=60000 
#etag.use-inode = "disable

 

#优化fastcgi,大流量并发设置 
fastcgi.server             = ( ".php" => 
                               ( "localhost" => 
                                 ( 
                                   "socket" => "/tmp/php-fastcgi.socket", 
                                   "bin-path" => "/usr/bin/php-cgi", 
                                   "max-procs" => 40, 
                                   "idle-timeout" => 20, 
                                   "bin-environment" => ( 
                                      "PHP_FCGI_CHILDREN" => "9", 
                                      "PHP_FCGI_MAX_REQUESTS" => "10000" 
                                   ), 
                                   "bin-copy-environment" => ( 
                                      "PATH", "SHELL", "USER" 
                                  )

                                 ) 
                               ) 
                            )

 

#配置虚拟主机 
$HTTP["host"] == "www.gaojinbo.com" { 
status.status-url = "/vstatus" 
server.name = "www.gaojinbo.com" 
server.document-root = "/www/www.gaojinbo.com.com" 
server.errorlog = "/www/log/www.gaojinbo.com.error.log" 
}

 

 

http://blog.csdn.net/zccst/article/details/4459925

http://www.gaojinbo.com/centos5-3%e4%b8%8b%e5%ae%89%e8%a3%85%e9%85%8d%e7%bd%aelighttpd%e4%bc%98%e5%8c%96%e9%85%8d%e7%bd%ae.html

http://www.gaojinbo.com/%e5%a6%82%e4%bd%95%e5%9c%a8centos%e4%b8%8b%e7%bc%96%e8%af%91%e5%ae%89%e8%a3%85lighttpd.html

猜你喜欢

转载自aoyouzi.iteye.com/blog/2288227
今日推荐