Red Hat Enterprise Linux 安装 lighttpd 和 Fastcgi PHP
我收到了很多关于如何在 Red Hat Enterprise Linux 4.0 版本下配置和安装 Lighttpd Web 服务器的询问。Mark 问:
RHEL 64 位 v4.0 不支持 PHP 作为 FastCGI。RHN(up2date 命令)中不提供 Lighttpd。如何使用 FastCGI 配置和安装 lighttpd?
好吧,让我系统地回答这些问题和其他疑问。我已经在 RHEL v4.0 32/64 位版本下安装了 Lighttpd 几次。在所有情况下,您都需要编译 PHP 和 Lightttpd。不用担心,步骤相当简单。
RHEL下安装并配置Lighttpd
RedHat Linux 使用 RHN 提供所有软件(包括 PHP/Apache)的稳定版本,但出于某些奇怪的原因,它没有附带 lighttpd Web 服务器。不过,我测试了 RHEL v.5.0(测试版),它附带了许多好东西,例如缓存软件、fastcgi 等。
步骤#1:在 RHEL 64 位 v4.0 下安装并配置 Lighttpd
首先,您需要删除已安装的 PHP 版本。使用 rpm -qa | grep php 命令找出所有已安装的 PHP rpm 文件的列表:
# rpm -qa | grep php
删除所有 PHP 文件:# rpm -e php php-devel php-imap php-ldap php-pear
步骤#2:下载lighttpd源代码
Red Hat 本身没有提供 64/32 位版本的官方 RPM 文件。您可以按照以下步骤下载并编译Lighttpd :
# wget http://lighttpd.net/download/lighttpd-1.4.16.tar.gz
# tar -zxvf lighttpd-1.4.16.tar.gz
# cd lighttpd-1.4.16
步骤#3:编译并安装lighttpd:
以下命令将编译 lighttpd 并支持 OpenSSL。首先,配置 lighttpd:
# ./configure --program-prefix= --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/usr/com --mandir=/usr/share/man --infodir=/usr/share/info --with-openssl
现在编译lighttpd
# make
安装lighttpd:
# make install
步骤#4:将 PHP RPM 构建为 FastCGI
现在重新编译 PHP 并将 PHP RPM 构建为 FastCGI。从官方 Red Hat 网站下载PHP SRPM或使用以下命令下载 PHP 源 RPM(推荐):
# cd /opt
# up2date -d --src php
现在安装下载的源 RPM 文件:
# rpm -ivh php-4.3.9-3.1.src.rpm
首先,安装所有必要的开发库:
# up2date aspell-devel libjpeg-devel libpng-devel libc-client-devel mysql-devel postgresql-devel unixODBC-devel net-snmp-devel elfutils-devel libxslt-devel freetype-devel
打开 php rpm 配置文件:
# cd /usr/src/redhat/SPECS/
打开 php.spec 文件:
# vi php.spec
找到第 1 行,内容如下:
--enable-force-cgi-redirect
在该行之前添加:
--enable-fastcgi \
保存并关闭文件。
编译并构建RPM文件:
# rpmbuild -bb php.spec
现在安装所有新重建的 RPM 文件。转到 /usr/src/redhat/RPMS/x86_64 目录,所有新建的 RPM 都存储于此:
# cd /usr/src/redhat/RPMS/x86_64
# rpm -ivh php-4.3.9-3.18.x86_64.rpm php-gd-4.3.9-3.18.x86_64.rpm php-imap-4.3.9-3.18.x86_64.rpm php-mysql-4.3.9-3.18.x86_64.rpm php-mbstring-4.3.9-3.18.x86_64.rpm php-pear-4.3.9-3.18.x86_64.rpm
确保 php 安装了 fastcgi:
# php -v
输出:
PHP 4.3.9 (cgi-fcgi) (built: Oct 2 2006 15:31:07)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
如果您没有安装开发环境或者懒得编译,请下载AMD x86_64 RPM文件。请注意,这些文件按原样提供。
基本 Lighttpd 配置
a)添加 lighttpd 用户
# adduser -s /sbin/nologin lighttpd
b) 创建一个 lighttpd.conf 文件添加以下配置代码:
c) 创建一个 lighttpd sysconfig 文件:
添加以下行:
保存并关闭文件。
# mkdir /etc/lighttpd
# cd /etc/lighttpd
# vi lighttpd.conf
server.modules = (
"mod_rewrite",
"mod_redirect",
"mod_alias",
"mod_access",
"mod_auth",
"mod_status",
"mod_fastcgi",
"mod_cgi",
"mod_compress",
"mod_accesslog" )
index-file.names = ( "index.php", "index.html",
"index.htm", "default.htm" )
mimetype.assign = (
".rpm" => "application/x-rpm",
".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",
".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"
)
########## BASE CONFIG - EDIT BELOW #########################
server.tag = "lighttpd (RedHat)"
accesslog.filename = "/var/log/lighttpd/access_log"
server.errorlog = "/var/log/lighttpd/error_log"
server.document-root = "/var/www/html/"
url.access-deny = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
server.port = 80
server.bind = "202.54.xxx.xxx"
server.error-handler-404 = "/errorr404.php"
server.pid-file = "/var/run/lighttpd.pid"
server.username = "lighttpd"
server.groupname = "lighttpd"
compress.cache-dir = "/tmp/lighttpd/cache/compress/"
compress.filetype = ("text/plain", "text/html")
$HTTP["url"] =~ "\.pdf$" {
server.range-requests = "disable"
}
fastcgi.server = ( ".php" =>
( "localhost" =>
(
"socket" => "/tmp/php-fastcgi.socket",
"bin-path" => "/usr/bin/php",
"max-procs" => 2,
)
)
)
# vi /etc/sysconfig/lighttpd
LIGHTTPD_CONF_PATH=/etc/lighttpd/lighttpd.conf
d) 创建 lighttpd 启动文件 (init.d 脚本)
添加以下行:
# vi /etc/init.d/lighttpd
#!/bin/sh
#
# lighttpd Startup script for the lighttpd server
#
# chkconfig: - 85 15
# description: Lighttpd web server
#
# processname: lighttpd
# config: /etc/lighttpd/lighttpd.conf
# config: /etc/sysconfig/lighttpd
# pidfile: /var/run/lighttpd.pid
#
# Source function library
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/lighttpd ]; then
. /etc/sysconfig/lighttpd
fi
if [ -z "$LIGHTTPD_CONF_PATH" ]; then
LIGHTTPD_CONF_PATH="/etc/lighttpd/lighttpd.conf"
fi
prog="lighttpd"
lighttpd="/usr/sbin/lighttpd"
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon $lighttpd -f $LIGHTTPD_CONF_PATH
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc $lighttpd
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}
reload() {
echo -n $"Reloading $prog: "
killproc $lighttpd -HUP
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
reload)
reload;;
status)
status $lighttpd
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|reload|status}"
RETVAL=1
esac
exit $RETVAL
保存并关闭文件。
e) 创建必要的目录并设置正确的权限:
# mkdir -p /var/log/lighttpd
# mkdir -p /tmp/lighttpd/cache/compress/
# chown lighttpd:lighttpd /var/log/lighttpd
# chown lighttpd:lighttpd /tmp/lighttpd/cache/compress/
f) 启动 lighttpd,但如果正在运行,请先停止 Apache:
# chkconfig httpd off
# /etc/init.d/httpd stop
# chkconfig --add lighttpd
# chkconfig lighttpd on
# /etc/init.d/lighttpd start
验证 Lighttpd 是否正在运行:
# netstat -tulpn | grep :80
更新:查看如何在 RHEL 5.0 / CentOS 5.0 下使用lighttpd 和 FastCGI配置。