且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ubuntu lighttpd轻量级web服务器

更新时间:2022-10-02 16:29:12

   Lighttpd和PHP的搭配现在越来越常见了。但是老实说,如非必要,我还是推荐你使用Apache和PHP的搭配方式,毕竟LAMP构架的成熟度和稳定度都是经过时间考验的。
       那么什么时候Lighttpd和PHP的搭配更值得选择呢?可以从以下几点考虑:如果访问量比较大,硬件资源吃紧,那么Lighttpd适合你;如果和稳定相比,你倾向速度多一些,那么Lighttpd适合你。
OS:ubuntu server 8.04.1
software:lighttpd-1.4.20
software:PHP 5.2.6.tar.gz
apt-get install  libmysqlclient15-dev  libpng12-dev
1. 安装 php.5.2.6
tar zxvf php.5.2.6.tar.gz
cd php.5.2.6
 

./configure --prefix=/usr/local/php --enable-fastcgi --enable-force-cgi-redirect --with-config-file-path=/usr/local/php/etc --disable-ipv6 --enable-libxml --with-gd --enable-gd-native-ttf  --with-mysql=/usr/lib/mysql/lib --with-mysqli=/usr/bin/mysql_config --enable-xml --with-freetype-dir '--with-jpeg-dir --with-png-dir --with-ttf=shared --enable-static --with-zlib-dir --with-ttf

 

make

 

make  install

 

2.安装lighttpd

 

tar zxvf lighttpd-1.4.20.tar.gz

 

cd lighttpd-1.2.40
 
./configure --prefix=/usr/local/lighttpd --disable-ipv6 --with-mysql=/usr/bin/mysql_config  --with-zlib --with-bzip2 --with-fam --with-memcache
 
make
 
make install
 
mkdir -p /usr/local/lighttpd/etc
 
mkdir -p /usr/local/lighttpd/log
 
mkdir -p /usr/local/lighttpd/run
 
cp ./doc/lighttpd.conf /usr/local/lighttpd/etc/
 
3.配置lighttpd.conf
 
server.modules              = (
                               "mod_rewrite",
                               "mod_redirect",
                               "mod_alias",
                               "mod_fastcgi",
                               "mod_simple_vhost",
                               "mod_evhost",
                               "mod_cgi",
                                "mod_accesslog" )
 
server.document-root        = "/var/www/"      #web访问的根目录
 
server.errorlog             = "/usr/local/lighttpd/log/error.log" #错误日志
 
index-file.names            = ( "index.php", "index.html",
                                "index.htm", "default.htm" )    #支持格式

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",
  ""              =>      "application/octet-stream",
 )
 
 
url.access-deny             = ( "~", ".inc" )
$HTTP["url"] =~ "\.pdf$" {
  server.range-requests = "disable"
}
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

server.port                = 80                             #监听端口
server.bind                = "192.168.6.106"     #绑定IP

server.pid-file            = "/var/run/lighttpd.pid"
 
server.username            = "www-data"       #运行服务用户
server.groupname           = "www-data"      #运行服务用户组
 
fastcgi.server             = ( ".php" =>
                               ( "localhost" =>
                                 (
                                   "socket" => "/usr/local/lighttpd/run/php-fastcgi.socket",
                                   "bin-path" => "/usr/local/php/bin/php-cgi"   #php-cgi 
                                 )
                               )
                            )
 
server.max-fds = 20480   # 文件描述符数目
server.max-keep-alive-requests = 0  #保持会话连接
server.network-backend = "linux-sendfile"   
server.event-handler = "linux-sysepoll"   # Linux环境下epoll系统调用可提高吞吐量
 
保存退出`
 
然后写一个启动脚本`
 
#!/bin/sh
 
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/loca/lighttpd/sbin:/usr/loca/lighttpd/bin
DAEMON=/usr/local/lighttpd/sbin/lighttpd
NAME=lighttpd
DESC="web server"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
ENV="env -i LANG=C PATH=/usr/local/lighttpd/bin:/usr/local/lighttpd/bin:/bin"
SSD="/sbin/start-stop-daemon"
 
DAEMON_OPTS="-f /usr/local/lighttpd/conf/lighttpd.conf"
 
test -x $DAEMON || exit 0
 
set -e
 
mkdir -p /var/run/lighttpd > /dev/null 2> /dev/null
chown www-data:www-data /var/run/lighttpd
chmod 0750 /var/run/lighttpd
 
. /lib/lsb/init-functions
 
case "$1" in
  start)
        log_daemon_msg "Starting $DESC" $NAME
        if ! $ENV $SSD --start --quiet\
        --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS ; then
            log_end_msg 1
        else
            log_end_msg 0
        fi
    ;;
  stop)
        log_daemon_msg "Stopping $DESC" $NAME
        if $SSD --quiet --stop --oknodo --retry 30\
        --pidfile $PIDFILE --exec $DAEMON; then
            rm -f $PIDFILE
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;
  reload)
        log_daemon_msg "Reloading $DESC configuration" $NAME
        if $SSD --stop --signal 2 --oknodo --retry 30\
        --quiet --pidfile $PIDFILE --exec $DAEMON; then
            if $ENV $SSD --start --quiet  \
                --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS ; then
                log_end_msg 0
            else
                log_end_msg 1
            fi
        else
            log_end_msg 1
        fi
  ;;
  restart|force-reload)
        $0 stop
        [ -r  $PIDFILE ] && while pidof lighttpd |\
                 grep -q `cat $PIDFILE 2>/dev/null` 2>/dev/null ; do sleep 1; done
        $0 start
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
        exit 1
        ;;
esac
 
exit 0
放在/etc/init.d/lighttpd
 
然后在 /var/www 里写一个php 脚本
 
<php?
 
phpinfo();
 
?>
 
好了,启动 lighttpd 
 
/etc/init.d/lighttpd start
 
 * Starting web server lighttpd                                                       [ OK ]
现在就可以打开IE  [url]http://192.168.6.106[/url]
 
看到 php调试页面就可以了。
 
 
PS :安装的时候要注意建立 lithttpd 配置文件夹 php-fastcgi.socket 的目录 log目录是运行lighttpd用户的权限,不然会报错~
 
好了现在我在讲一下lighttpd有一个负载功能。相信大家都看到了
 
配置文件里的:
fastcgi.server             = ( ".php" =>
                               ( "localhost" =>
                                 (
                                   "socket" => "/usr/local/lighttpd/run/php-fastcgi.socket",
                                   "bin-path" => "/usr/local/php/bin/php-cgi"   #php-cgi 
                                 )
                               )
                            )
我们在 lighttpd 的源码包里面把spawn-php.sh 拷贝到 /usr/local/sbin/
下 复制成三份
cp spawn-php.sh spawn-php-2.sh
cp spawn-php.sh spawn-php-3.sh
然后分别配置三个文件
SPAWNFCGI="/usr/local/lighttpd/bin/spawn-fcgi"     #spawn-fcgi 路径
FCGIPROGRAM="/usr/local/php/bin/php-cgi"            #php-cgi的路径
FCGIPORT="1026"                                                         #运行端口 spawn-php-2.sh 改为 1027   spawn-php-3sh 改为 1028
FCGI_WEB_SERVER_ADDRS="127.0.0.1,192.168.6.106"   #绑定服务器IP 
 
好了。然后把每个都运行起来~~ 
./spawn-php.sh
spawn-fcgi.c.206: child spawned successfully: PID: 5233
./spawn-php-2.sh  .................
然后修改 /usr/local/lighttpd/etc/lighttpd.conf
把 fastcgi.server             = ( ".php" =>
                               ( "localhost" =>
                                 (
                                   "socket" => "/usr/local/lighttpd/run/php-fastcgi.socket",
                                   "bin-path" => "/usr/local/php/bin/php-cgi"   #php-cgi 
                                 )
                               )
                            )
替换成 #fastcgi.server = ( ".php" =>
  (( "host" => "127.0.0.1", "port" => 1026 ),
  ( "host" => "127.0.0.1", "port" => 1027 ),
  ( "host" => "127.0.0.1", "port" => 1028 )
)
 保存退出从启服务~~
/etc/init.d/lighttpd restart
 可以在 服务器上  top看到明显多了 很多运行进程~
当然我这么起不到什么作用,把 spawn-php.sh  可以拷贝到别的linux服务器上运行
才也可以的``但是要记得把 spawn-fcgi  也要拷贝过去哦~~大家可以试试看~


本文转自Deidara 51CTO博客,原文链接:http://blog.51cto.com/deidara/106774,如需转载请自行联系原作者