Lighttpd 作為新一代的web server,以小巧(不到1M的大小)、快速而著稱(chēng),因?yàn)榉?wù)器上安裝了rails、java,并以lighttpd為前端代理服務(wù)器,不想再部署apache了,所以直接使用lighttpd來(lái)部署,順便看一下性能如何。
本文主要介紹在CentOS下,配置一套用lighttp作為web server的php環(huán)境
· 安裝Lighttpd
從http://www.lighttpd.net/download/下載源碼
安裝前先檢查pcre是否安裝,需要pcre和pcre-devel兩個(gè)包。 用yum search pcre\*檢查,如果都是installed就是都安裝了。否則安裝缺少的包。
tar xzvf lighttpd-1.4.23.tar.gz
cd lighttpd-1.4.23
./configure –prefix=/usr/local/lighttpd
configure完畢以后,會(huì)給出一個(gè)激活的模塊和沒(méi)有激活模塊的清單,可以檢查一下,是否自己需要的模塊都已經(jīng)激活,在enable的模塊中一定要有“mod_rewrite”這一項(xiàng),否則重新檢查pcre是否安裝。然后編譯安裝:
編譯后配置:
mkdir /etc/lighttpd
cp doc/lighttpd.conf /etc/lighttpd/lighttpd.conf
如果你的Linux是RedHat/CentOS,那么:
如果你的Linux是SuSE,那么:
其他Linux發(fā)行版本可以自行參考該文件內(nèi)容進(jìn)行修改。然后修改/etc/init.d/lighttpd,把
改為
此腳本用來(lái)控制lighttpd的啟動(dòng)關(guān)閉和重起:
/etc/init.d/lighttpd stop
/etc/init.d/lighttpd restart
如果你希望服務(wù)器啟動(dòng)的時(shí)候就啟動(dòng)lighttpd,那么:
這樣lighttpd就安裝好了,接下來(lái)需要配置lighttpd。
配置Lighttpd
修改/etc/lighttpd/lighttpd.conf
1)server.modules
取消需要用到模塊的注釋?zhuān)琺od_rewrite,mod_access,mod_fastcgi,mod_simple_vhost,mod_cgi,mod_compress,mod_accesslog是一般需要用到的。
2)server.document-root, server.error-log,accesslog.filename需要指定相應(yīng)的目錄
3)用什么權(quán)限來(lái)運(yùn)行l(wèi)ighttpd
server.username = “nobody”
server.groupname = “nobody”
從安全角度來(lái)說(shuō),不建議用root權(quán)限運(yùn)行web server,可以自行指定普通用戶權(quán)限。
4)靜態(tài)文件壓縮
compress.cache-dir = “/tmp/lighttpd/cache/compress”
compress.filetype = (“text/plain”, “text/html”,”text/javascript”,”text/css”)
可以指定某些靜態(tài)資源類(lèi)型使用壓縮方式傳輸,節(jié)省帶寬,對(duì)于大量AJAX應(yīng)用來(lái)說(shuō),可以極大提高頁(yè)面加載速度。
5)配置ruby on rails
最簡(jiǎn)單的配置如下:
server.document-root = "/yourrails/public"
server.error-handler-404 = "/dispatch.fcgi"
fastcgi.server = (".fcgi" =>
("localhost" =>
("min-procs" => 10,
"max-procs" => 10,
"socket" => "/tmp/lighttpd/socket/rails.socket",
"bin-path" => "/yourrails/public/dispatch.fcgi",
"bin-environment" => ("RAILS_ENV" => "production")
)
)
)
}
即由lighttpd啟動(dòng)10個(gè)FCGI進(jìn)程,lighttpd和FCGI之間使用本機(jī)Unix Socket通信。
如果想指定www.abc.com以及所有二級(jí)域名,則需要把第一行改為
$HTTP[”host”] =~ “(^|\.)abc\.com” {
…
}
如果要設(shè)置代理,比如lighttpd和tomcat整合,tomcat放在lighttpd后面,則需要通過(guò)代理訪問(wèn)tomcat
$HTTP["host"] =~ “www.domain.cn” {
proxy.server = ( “” => ( “localhost” => ( “host”=> “127.0.0.1″, “port”=> 8080 ) ) )
}
則www.domain.cn為主機(jī)的網(wǎng)址都交給tomcat處理,tomcat的端口號(hào)為8080. 在tomcat的虛擬主機(jī)中,需要捕獲www.domain.cn這個(gè)主機(jī)名,設(shè)置這個(gè)虛擬主機(jī)。這里的host都是跟tomcat里面的虛擬主機(jī)對(duì)應(yīng)的。
· 安裝支持fastcgi的PHP
安裝PHP所需的相關(guān)類(lèi)庫(kù)
curl
tar xvjf curl-7.19.5.tar.bz2
cd curl-7.19.5
./configure –prefix=/usr/local/curl
make
make install
gettext
tar xvzf gettext-0.17.tar.gz
cd gettext-0.17
./configure –prefix=/usr/local/gettext
make
make install
zlib
tar xvzf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure –prefix=/usr/local/zlib
make && make install
libpng
tar xvzf libpng-1.2.9.tar.gz
cd libpng-1.2.9
./configure –prefix=/usr/local/libpng
make && make install
jpeg
tar xvzf jpegsrc.v6b.tar.gz
cd jpeg-6b/
./configure –prefix=/usr/local/jpeg6
make
mkdir /usr/local/jpeg6/bin
mkdir -p /usr/local/jpeg6/bin
mkdir -p /usr/local/jpeg6/man/man1
mkdir -p /usr/local/jpeg6/lib
mkdir -p /usr/local/jpeg6/include
make install-lib
make install
freetype
tar xvzf freetype-2.3.9.tar.gz
cd freetype-2.3.9
./configure –prefix=/usr/local/freetype2
make
make install
gd
tar xvzf gd-2.0.35.tar.gz
cd gd-2.0.35
./configure –prefix=/usr/local/gd2 –with-zlib=/usr/local/zlib/ –with-png=/usr/local/libpng/ –with-jpeg=/usr/local/jpeg6/ –with-freetype=/usr/local/freetype2/
make
如果第一次make出錯(cuò),試著再make一次,我就是這樣,第二次就對(duì)了。
make install
PHP
cd php-5.2.10
./configure –prefix=/usr/local/php –with-mysql=/usr/local/mysql –with-pdo-mysql=/usr/local/mysql –with-jpeg-dir=/usr/local/jpeg6/ –with-png-dir=/usr/local/libpng/ –with-gd=/usr/local/gd2/ –with-freetype-dir=/usr/local/freetype2/ –with-zlib-dir=/usr/local/zlib –with-curl=/usr/local/curl –with-gettext=/usr/local/gettext –enable-fastcgi –enable-zend-multibyte –with-config-file-path=/etc –enable-discard-path –enable-force-cgi-redirect
make
make install
cp php.ini-dist /etc/php.ini
可以使用php -m查看你安裝的模塊
eAccelerator
eAccelerator是一個(gè)開(kāi)源的PHP加速器
tar xjvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
export PHP_PREFIX="/usr/local/php"
$PHP_PREFIX/bin/phpize
./configure –enable-eaccelerator=shared –with-php-config=$PHP_PREFIX/bin/php-config
make
make install
執(zhí)行好后,會(huì)提示安裝到的路徑,下面會(huì)用到,如我的被安裝到這里
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613
編輯php.ini中的內(nèi)容
vim /etc/php.ini
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
如果一切順利,你可以通過(guò)下面命令來(lái)驗(yàn)證是否安裝成功
PHP 5.2.10 (cli) (built: Jun 20 2009 23:32:09)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
修改/etc/lighttpd/lighttpd.conf文件,添加下面的配置
vim /etc/lighttpd/lighttpd.conf
( "localhost" =>
(
"socket" => "/tmp/php-fastcgi.socket",
"bin-path" => "/usr/local/php/bin/php-cgi"
)
)
)
重啟lighttpd
寫(xiě)一個(gè)php測(cè)試文件在lighttpd的網(wǎng)站目錄里,測(cè)試php是否安裝成功
http://blog.prosight.me/