Lighttpd 搭建 Web 服务器

摘要:
默认值为/var/tmpserver。upload dirs=#设置错误日志文件服务器的路径。errorlog=“/var/log/lighttpd/error.log”#设置lighttpd进程服务器的路径。pid file=“/var/run/lighttpd.pid”#指定可以运行服务器的用户名和组名,并要求lighttp以root权限启动。到目前为止,基本的lighttpd功能已经成功安装。

背景:
      公司项目用到了lighttpd,由于自己没有接触过,所以做下记录。

简介:
      Lighttpd 是一个德国人领导的开源Web服务器软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的web server环境。具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点。支持FastCGI, CGI, Auth, 输出压缩(output compress), URL重写, Alias等重要功能,而Apache之所以流行,很大程度也是因为功能丰富,在lighttpd上很多功能都有相应的实现了。

安装:
环境:Ubuntu 11
下载安装:

apt-get install lighttpd

配置文件在:/etc/lighttpd/lighttpd.conf

Lighttpd 搭建 Web 服务器第1张Lighttpd 搭建 Web 服务器第2张View Code
#指定要装载的模块
server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
#       "mod_rewrite",
)
#fastcgi的配置
#fastcgi.server = ( ".php" => ((
#                    "host" => "127.0.0.1",
#                    "port" => "9000",
#                 )))

fastcgi.server = ( ".php" => ((
                    "socket" => "/tmp/php.socket",
                 )))
#设置网页服务器的根目录。配置文件必需指定该参数
server.document-root        = "/var/www"
#设置上传目录。缺省为 /var/tmp 
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
# 设置错误日志文件的路径
server.errorlog             = "/var/log/lighttpd/error.log"
# 设置Lighttpd进程的路径
server.pid-file             = "/var/run/lighttpd.pid"
#指定可以运行服务器的用户名和组名,要求lighttp以root权限启动。
server.username             = "www-data"
server.groupname            = "www-data"
#默认读取的文件,index.lighttpd.html 是安装默认的文件,按照顺序来查找执行。
index-file.names            = ( "index.php", "index.html",
                                "index.htm", "default.htm",
                               " index.lighttpd.html","a.html")

url.access-deny             = ( "~", ".inc" )
# 禁止访问通过扩展的文件的某些类型的源
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
server.port = 80
server.bind = "192.168.200.201"

## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl"
#为目录列表设置编码
dir-listing.encoding        = "utf-8"
#启用或则禁用目录列表
server.dir-listing          = "disable"

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/x-javascript", "text/css", "text/html", "text/plain" )

#虚拟主机1
$HTTP["host"] == "bbs.db2.com" {
server.name = "bbs.db2.com"
server.document-root = "/var/www/db2/"
accesslog.filename = "/var/www/db2/access.log"
accesslog.format = "%{X-Forwarded-For}i %v %u %t \"%r\" %s %b \"%{User-Agent}i\" \"%{Referer}i\""
}
#虚拟主机2
$HTTP["host"] == "bbs.abc.com" {
server.name = "bbs.abc.com"
server.document-root = "/var/www/abc/"
accesslog.filename = "/var/www/abc/access.log"
}
#虚拟主机3
$HTTP["host"] == "bbs.xyz.com" {
server.name = "bbs.xyz.com"
server.document-root = "/var/www/xyz/"
accesslog.filename = "/var/www/xyz/access.log"
}


include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

 

安装完毕之后,做浏览器里面输入:服务器ip地址,会出现“Index of /”的列表(server.dir-listing = "enable"),点 index.lighttpd.html 文件,直接加载了该html文件的内容。这个文件刚好是在参数 server.document-root 指定的目录中。要是server.dir-listing = "disable"的话,直接输入ip则会出现404报错,需要输入ip+index.lighttpd.html才行。
自己可以创建一个html文件,如a.html放到server.document-root指定的目录中,再修改 index-file.names 参数,让a.html包含进去,再重启lighttpd,会让a.html展示出来。
到此,lighttpd基本功能已经成功安装。

使用:

1:多虚拟主机的配置:
1个IP地址配置多个域名,即1个IP多各域名。
首先,需要修改host名称,如:

vi /etc/hosts
192.168.220.201 bbs.abc.com --添加
192.168.220.201 bbs.xyz.com --添加

改完之后,需要重启networking 才能生效。
接着,在/etc/lighttpd/lighttpd.conf 配置文件中添加虚拟主机:

$HTTP["host"] == "bbs.db2.com" {
server.name = "bbs.db2.com"
server.document-root = "/var/www/db2/"
accesslog.filename = "/var/www/db2/access.log"
accesslog.format = "%{X-Forwarded-For}i %v %u %t \"%r\" %s %b \"%{User-Agent}i\" \"%{Referer}i\""
}

$HTTP["host"] == "bbs.abc.com" {
server.name = "bbs.abc.com"
server.document-root = "/var/www/abc/"
accesslog.filename = "/var/www/abc/access.log"
}

$HTTP["host"] == "bbs.xyz.com" {
server.name = "bbs.xyz.com"
server.document-root = "/var/www/xyz/"
accesslog.filename = "/var/www/xyz/access.log"
}

再在/var/www/的目录下创建 db2,abc,xyz目录,把之前写的a.html放入到这些目录中。
最后,在浏览器里面输入bbs.db2.com,bbs.xyz.com,bbs.abc.com 看看什么效果。

当然上面的虚拟主机可以单独写一个文件,再在include 到 lighttpd.conf 中。

2,lighttpd 支持 PHP,即 lighttpd + php-fpm

 安装:

apt-get install php5-fpm php5

 确保 /etc/php5/fpm/php.ini 中的cgi.fix_pathinfo=1

cd /etc/lighttpd/conf-available/
cp 15-fastcgi-php.conf 15-fastcgi-php-spawnfcgi.conf
vi 15-fastcgi-php.conf
fastcgi.server += ( ".php" =>
        ((
                "bin-path" => "/usr/bin/php-cgi",
                "socket" => "/tmp/php.socket",
                "max-procs" => 1,
                "bin-environment" => (
                        "PHP_FCGI_CHILDREN" => "4",
                        "PHP_FCGI_MAX_REQUESTS" => "10000"
                ),
                "bin-copy-environment" => (
                        "PATH", "SHELL", "USER"
                ),
                "broken-scriptfilename" => "enable"
        ))
)

开启lighttpd 的 fastcgi 配置,执行:

lighttpd-enable-mod fastcgi
lighttpd-enable-mod fastcgi-php

出现2个软链接:
ls -l /etc/lighttpd/conf-enabled
lrwxrwxrwx 1 root root 33 Nov 14 04:52 10-fastcgi.conf -> ../conf-available/10-fastcgi.conf
lrwxrwxrwx 1 root root 37 Nov 14 04:52 15-fastcgi-php.conf -> ../conf-available/15-fastcgi-php.conf

修改/etc/lighttpd/lighttpd.conf 配置:添加

fastcgi.server = ( ".php" => ((
                    "host" => "127.0.0.1",
                    "port" => "9000",
                 )))

重启lighttpd

/etc/init.d/lighttpd force-reload

最后写一个php页面:index.php ,该文件放到/var/www/xyz/目录下面。
在index-file.names配置文件中的index-file.names选项里添加该php文件。注:index-file.names 选项里当有多个文件时,会按照文件的顺序来去查找页面。

<?php
phpinfo();
?>

现在可以在浏览器里面输入查看页面了。
上面的是通过TCP进行通信链接的,端口是9000
netstat -ap | grep php
tcp        0      0 localhost:9000          *:*                     LISTEN      1288/php-fpm.conf
或则:
netstat -ntl | grep 9000

那么通过套接字如何进行呢?

1,修改/etc/php5/fpm/pool.d/www.conf文件,把
listen = 127.0.0.1:9000
改成
listen = /tmp/php.socket
2,修改/etc/lighttpd/lighttpd.conf文件,把
fastcgi.server = ( ".php" => ((
                    "host" => "127.0.0.1",
                    "port" => "9000",

                 )))
改成
fastcgi.server = ( ".php" => ((
                    "socket" => "/tmp/php.socket",
                 )))

最后重启php和lighttpd:
/etc/init.d/php5-fpm restart
/etc/init.d/lighttpd restart

赶紧在浏览器里面输入地址看看是否OK。

 

这里只是简单的介绍了lighttpd的一些功能,还有很多功能没有写出来,如:跳转等。后期当自己遇到的时候再学再完善笔记。

参考:
http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ConfigurationOptions
http://www.howtoforge.com/installing-lighttpd-with-php5-php-fpm-and-mysql-support-on-ubuntu-12.10
http://i.linuxtoy.org/docs/guide/ch23s03.html


免责声明:文章转载自《Lighttpd 搭建 Web 服务器》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇速度与精度的结合排查程序死循环,死锁的方法 ——pstack下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

加密通讯协议SSL编程周立发

Linux网络编程:加密通讯协议SSL编程<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 服务器端源代码如下: #include <stdio.h>#include <stdlib.h>#include &...

h5 时间控件问题,怎么设置type =datetime-local 的值

在js中设置自定义时间到date控件的方法: 1、在html5中定义时间控件 <input type="date" value=""/> 2、编写脚本实现自定义时间的赋值 //创建一个当前日期对象 var now = new Date(); //格式化日,如果小于9,前面补0var day = ("0" + now.getDa...

ssh 提示Connection closed by * 的解决方案

使用ssh方式连接linux系统时,发现一直上报这个错误: Connection closed by 192.168.3.71 port 22 刚开始还以为是端口被防火墙禁止了呢,通过关闭和查看,并没有发现 什么错误,这就要详细的分析了。到底是哪儿出的问题呢? 根据思路来,先看log:从log可以看出,出错的原因很明显,就是加密文件权限有问题了,接下来就去...

Echarts自动刷新数据

1.Echarts自动刷新数据 1.Echarts柱状图的正常配置 注:声明了 myChart、test这两个都有用 官方示例中myChart是声明在 function(ec)里面的 <script src="http://cdn.staticfile.org/jquery/2.1.1-rc2/jquery.min.js"></scri...

vue配置请求转发解决跨域问题

通过nodejs的请求转发到后台,前端地址:http://localhost:8080 后端地址:http://localhost:8081 vue.config.js内容如下: let proxyObj={} proxyObj['/']={ //websocket ws:false, target:'http://localho...

[转]ZABBIX API简介及使用

API简介 Zabbix API开始扮演着越来越重要的角色,尤其是在集成第三方软件和自动化日常任务时。很难想象管理数千台服务器而没有自动化是多么的困难。Zabbix API为批量操作、第三方软件集成以及其他作用提供可编程接口。 Zabbix API是在1.8版本中开始引进并且已经被广泛应用。所有的Zabbix移动客户端都是基于API,甚至原生的WEB前端部...