Lighttpd 作為新一代的web server,以小巧(不到1M的大小)、快速而著稱,因為服務器上安裝了rails、java,并以lighttpd為前端代理服務器,不想再部署apache了,所以直接使用lighttpd來部署,順便看一下性能如何。
本文主要介紹在CentOS下,配置一套用lighttp作為web server的php環境
· 安裝Lighttpd
從http://www.lighttpd.net/download/下載源碼
安裝前先檢查pcre是否安裝,需要pcre和pcre-devel兩個包。 用yum search pcre\*檢查,如果都是installed就是都安裝了。否則安裝缺少的包。
yum
install pcre-devel
tar xzvf lighttpd-1.4.23.tar.gz
cd lighttpd-1.4.23
./configure –prefix=/usr/local/lighttpd
configure完畢以后,會給出一個激活的模塊和沒有激活模塊的清單,可以檢查一下,是否自己需要的模塊都已經激活,在enable的模塊中一定要有“mod_rewrite”這一項,否則重新檢查pcre是否安裝。然后編譯安裝:
make && make install
編譯后配置:
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="/usr/sbin/lighttpd"
改為
lighttpd="/usr/local/lighttpd/sbin/lighttpd"
此腳本用來控制lighttpd的啟動關閉和重起:
/etc/init.d/lighttpd start
/etc/init.d/lighttpd stop
/etc/init.d/lighttpd restart
如果你希望服務器啟動的時候就啟動lighttpd,那么:
chkconfig lighttpd on
這樣lighttpd就安裝好了,接下來需要配置lighttpd。
配置Lighttpd
修改/etc/lighttpd/lighttpd.conf
1)server.modules
取消需要用到模塊的注釋,mod_rewrite,mod_access,mod_fastcgi,mod_simple_vhost,mod_cgi,mod_compress,mod_accesslog是一般需要用到的。
2)server.document-root, server.error-log,accesslog.filename需要指定相應的目錄
3)用什么權限來運行lighttpd
server.username = “nobody”
server.groupname = “nobody”
從安全角度來說,不建議用root權限運行web server,可以自行指定普通用戶權限。
4)靜態文件壓縮
compress.cache-dir = “/tmp/lighttpd/cache/compress”
compress.filetype = (“text/plain”, “text/html”,”text/javascript”,”text/css”)
可以指定某些靜態資源類型使用壓縮方式傳輸,節省帶寬,對于大量AJAX應用來說,可以極大提高頁面加載速度。
5)配置ruby on rails
最簡單的配置如下:
$HTTP["host"] == "www.xxx.com" {
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啟動10個FCGI進程,lighttpd和FCGI之間使用本機Unix Socket通信。
如果想指定www.abc.com以及所有二級域名,則需要把第一行改為
$HTTP[”host”] =~ “(^|\.)abc\.com” {
…
}
如果要設置代理,比如lighttpd和tomcat整合,tomcat放在lighttpd后面,則需要通過代理訪問tomcat
$HTTP["host"] =~ “www.domain.cn” {
proxy.server = ( “” => ( “localhost” => ( “host”=> “127.0.0.1″, “port”=> 8080 ) ) )
}
則www.domain.cn為主機的網址都交給tomcat處理,tomcat的端口號為8080. 在tomcat的虛擬主機中,需要捕獲www.domain.cn這個主機名,設置這個虛擬主機。這里的host都是跟tomcat里面的虛擬主機對應的。
· 安裝支持fastcgi的PHP
安裝PHP所需的相關類庫
curl
wget http://curl.cs.pu.edu.tw/download/curl
-7.19.5.
tar.bz2
tar xvjf curl-7.19.5.tar.bz2
cd curl-7.19.5
./configure –prefix=/usr/local/curl
make
make install
gettext
wget ftp://ftp.ntu.edu.tw/pub/gnu/gnu/gettext/gettext-0.17.tar.gz
tar xvzf gettext-0.17.tar.gz
cd gettext-0.17
./configure –prefix=/usr/local/gettext
make
make install
zlib
wget http://kent.dl.sourceforge.net/sourceforge/libpng/zlib-1.2.3.tar.gz
tar xvzf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure –prefix=/usr/local/zlib
make && make install
libpng
wget http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/l/li/libpng/libpng-1.2.9.tar.gz
tar xvzf libpng-1.2.9.tar.gz
cd libpng-1.2.9
./configure –prefix=/usr/local/libpng
make && make install
jpeg
wget http://www.ijg.org/files/jpegsrc.v6b.
tar.gz
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
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.3.9.tar.gz
tar xvzf freetype-2.3.9.tar.gz
cd freetype-2.3.9
./configure –prefix=/usr/local/freetype2
make
make install
gd
wget http://www.libgd.org/releases/gd-2.0.35.tar.gz
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出錯,試著再make一次,我就是這樣,第二次就對了。
make install
PHP
tar xvzf php
-5.2.10.
tar.gz
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是一個開源的PHP加速器
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
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
執行好后,會提示安裝到的路徑,下面會用到,如我的被安裝到這里
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613
編輯php.ini中的內容
vim /etc/php.ini
cgi.fix_pathinfo =
1
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"
如果一切順利,你可以通過下面命令來驗證是否安裝成功
$ php -v
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
fastcgi.server = ( ".php" =>
( "localhost" =>
(
"socket" => "/tmp/php-fastcgi.socket",
"bin-path" => "/usr/local/php/bin/php-cgi"
)
)
)
重啟lighttpd
/etc/init.d/lighttpd restart
寫一個php測試文件在lighttpd的網站目錄里,測試php是否安裝成功
http://blog.prosight.me/
posted on 2011-06-22 23:24
Alpha 閱讀(1786)
評論(0) 編輯 收藏 所屬分類:
Linux Nginx