ubuntu lighttpd轻量级web服务器

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
   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,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
23天前
|
Ubuntu JavaScript 关系型数据库
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
在阿里云Ubuntu 20.04服务器上部署Ghost博客的步骤包括创建新用户、安装Nginx、MySQL和Node.js 18.x。首先,通过`adduser`命令创建非root用户,然后安装Nginx和MySQL。接着,设置Node.js环境,下载Nodesource GPG密钥并安装Node.js 18.x。之后,使用`npm`安装Ghost-CLI,创建Ghost安装目录并进行安装。配置过程中需提供博客URL、数据库连接信息等。最后,测试访问前台首页和后台管理页面。确保DNS设置正确,并根据提示完成Ghost博客的配置。
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
|
1月前
|
Ubuntu 网络协议 Java
【Android平板编程】远程Ubuntu服务器code-server编程写代码
【Android平板编程】远程Ubuntu服务器code-server编程写代码
|
1月前
|
存储 资源调度 应用服务中间件
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
24 0
|
30天前
|
网络协议 Shell 网络安全
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
161 0
|
23天前
|
前端开发 应用服务中间件 nginx
使用Docker快速搭建Web服务器Nginx
本文指导如何使用Docker快速搭建Nginx服务器。首先,通过`docker pull`命令获取Nginx镜像,然后以容器形式运行Nginx并映射端口。通过挂载目录实现本地文件与容器共享,便于自定义网页。使用`docker ps`检查运行状态,访问IP:8088确认部署成功。最后,介绍了停止、删除Nginx容器的命令,强调Docker简化了服务器部署和管理。
38 0
|
16天前
|
Ubuntu Linux 虚拟化
【Linux】ubuntu安装samba服务器
【Linux】ubuntu安装samba服务器
|
1月前
|
Windows
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
51 2
|
1月前
|
机器学习/深度学习 Python
Python基础:构建一个简单的Web服务器
Python基础:构建一个简单的Web服务器
48 1
|
1月前
|
应用服务中间件 Apache nginx
web后端-web服务器对比
web后端-web服务器对比
|
1月前
|
存储 Ubuntu 网络安全