HOWTO:安装和配置 lighttpd webserver

August 16th, 2008

lighttpd是一个可运行在UNIX/Linux和Windows操作系统上的web服务器软件。和Apache类似。lighttped有时也被称为lighty。

lighttpd特别针对速度进行优化,致力于设计一个安全、快速、与标准兼容和灵活的web服务器。它的小内存占用(和其他web服务器相比)、低cpu负载和快速相应使它更适用于面临负载危机的服务器。请移步官方网站获取详细信息。

本文将指导你如何安装lighttpd服务器,并进行简单配置.

安装lighttpd

如果你使用基于RPM的发行版 (RedHat, Fedora 等) 请从此处下载

下载Fedora Core RPM Binary:
$ cd /tmp
$ wget http://lighttpd.net/download/lighttpd-1.4.13-1.i386.rpm
$ su -
# rpm -ivh lighttpd-1.4.13-1.i386.rpm 

请跳至 配置lighttpd 章节.

Debian Linux 用户使用apt-get命令来安装:
  # apt-get install lighttpd 

请跳至 配置lighttpd 章节.

从源码安装

以上的RPM包不包含mod_rewrite等其他东西,所以你可以从源代码进行编译(注意:源代码安装仅推荐有经验的用户使用):

 $ cd /tmp

取得最新源码:

 $ wget http://lighttpd.net/download/lighttpd-1.4.13.tar.gz 

解压:

$ tar -zxvf lighttpd-1.4.13.tar.gz
$ cd lighttpd-1.4.13
运行 ./configure
$ ./configure --host=i686-redhat-linux-gnu \
         --build=i686-redhat-linux-gnu \
         --target=i386-redhat-linux \
         --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 \
         --with-pcre \
         --with-zlib \
         --with-bzip2 \
         --disable-ipv6 \
         --with-PACKAGE=mod_redirect \
         --with-rewrite \
         --with-redirect \
         --with-ssi

以上编译选项使lighttpd支持SSL, mod_rewrite, ssi, mod_redirect etc. 运行一下命令可以得到具体的参数帮助:

 $ ./configure --help | less

安装lighttpd:

$ make
# make install

创建配置目录:

   # mkdir /etc/lighttpd/ 

创建lighttp用户和用户组

 # groupadd lighttpd
# useradd -g lighttpd -d /var/www/html -s /sbin/nologin lighttpd
OR
# adduser -g lighttpd -d /var/www/html -s /sbin/nologin lighttpd

创建log目录:

   # mkdir /var/log/lighttpd
# chown lighttpd:lighttpd /var/log/lighttpd

下载配置文件和启动文件的sample:

# cd /etc/lighttpd
# wget http://www.cyberciti.biz/tips/wp-content/uploads/2006/07/lighttpd.conf.txt
# mv lighttpd.conf.txt lighttpd.conf
# chown lighttpd:root /etc/lighttpd/lighttpd.conf
# cd /etc/init.d/
# wget http://www.cyberciti.biz/tips/wp-content/uploads/2006/07/lighttpd.txt
# mv lighttpd.txt lighttpd
# chmod +x lighttpd 

配置lighttpd

下面两个文件对lighttpd很重要:

  • 默认的lighttpd配置文件: /etc/lighttpd/lighttpd.conf
  • lighttpd服务启动脚本: /etc/init.d/lighttpd

所有的二进制方式安装都会安装这两个文件。
现在lighttpd已安装完成。下面开始配置。

删除apache

如果你不准备再使用Apache v1.3/2.x ,最好删除它 (确保你备份了数据文件和配置文件):

# rpm -e httpd
# yum remove httpd
# apt-get remove apache2
理解lighttpd主要配置参数

下面是lighttpd的主要配置参数:

  • server.document-root = “/var/www/html”: 指定服务器的默认根目录。
  • server.port = 80: 指定默认的http端口。
  • server.username = “lighttpd”
  • server.groupname = “lighttpd”: 指定开启和停止lighttpd服务的默认用户和用户组。这是一个安全功能(可以不必是root来操作)。
  • server.bind = “server-ip-address”: 指定服务器IP或域名,如 theos.in.
  • server.tag =”lighttpd”: 用来设置lighttpd的名称和版本(默认)。 这也是一个安全功能。 你可以设置成如下值:
    server.tag ="myWebServer v1.0"

    注意,这个信息会在http头中显示。 (可以通过 netcraft看到)

  • server.errorlog = “/var/log/lighttpd/error.log”: 指定错误日志文件。
  • accesslog.filename = “/var/log/lighttpd”: 指定访问日志文件(可通过日志文件分析得到访问统计信息)。
  • index-file.names = ( “index.php”, “index.html” ): 指定index文件.

server.modules = (
“mod_access”,
“mod_accesslog”,
“mod_fastcgi”,
“mod_rewrite”,
“mod_auth”
)
: 以上模块会被加载:

  • mod_access: 用来限制访问某些后缀的文件
  • mod_accesslog: 记录CLF log, 类似与apache
  • mod_fastcgi : FastCGI调用perl/PHP等
  • mod_rewrite : 生成SEO友好的url
  • mod_auth: 验证(用密码保护目录)

mimetype.assign = (
“.pdf” => “application/pdf”,
“.sig” => “application/pgp-signature”
)
: 设置mimetype映射.

打开文件/etc/lighttpd/lighttpd.conf,设置以上参数:


# vi /etc/lighttpd/lighttpd.conf

保存文件,启动lighttpd:


# /etc/init.d/lighttpd start

验证lighttpd是否在运行:


$ netstat -ntulp

Output:

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      29855/sshd
tcp        0      0 203.111.103.81:80           0.0.0.0:*                   LISTEN      5866/lighttpd
...
.....
..

打开浏览器,输入url测试: http://your-domain.com/ or http://server-ip/

webflier i.t.

Howto: Linux Lighttpd SSL (secure server layer) https 安装和配置

August 16th, 2008

SSL是一种加密协议,为电子邮件、web网页等Internet服务提供安全的通信。

SSL证书是一种数字证书,用来标识网站并且使用SSL技术加密服务器和客户端通信时的数据。

SSL对电子商务网站特别有用,使用网上银行付款、用户登录和注册等行为都离不开SSL。

为了要购买一个数字证书,你必须先生成一个Certificate Signing Request (CSR),并提交给Certification Authority (CA)。CSR中包含有你使用证书的程序的相关信息,还有你的public key。你的Web服务器软件可以生成CSR(openssl命令),同时生成public/private key,这将用来加密和解密你的安全链接。

必备条件

  • Lighttpd webserver with SSL support
  • 独立IP地址
  • CA颁发给你的SSL 证书
  • 域名 - theos.in (为方便举例,这里使用 theos.in )

服务器上一些重要文件

  • /etc/lighttpd/theos.in/ - 存储SSL证书和相关文件的目录
  • /etc/lighttpd/theos.in/theos.in.key - 你的private key,永远不要把这个文件给别人,lighttpd用这个文件来加密信息。
  • /etc/lighttpd/theos.in/theos.in.csr - 当你去CA处请求颁发SSL证书时,你需要将这个文件的内容提供给他们,这里面有你的public key。
  • /etc/lighttpd/theos.in/theos.in.pem - 这个文件里面包含了private keys (RSA and DSA), public keys (RSA and DSA) 和 (x509) 证书通常,你的密钥和证书都存放在这个文件中。

CA的一些重要文件

  •     CA_issuing.crt - 你的 intermediate certificate 或 chained root certificate 文件。
  •      theos.in.crt - 你的域名证书。

以下是具体步骤

Step # 1: 生成certificate signing request (CSR)
首先,用openssl命令生成域名theos.in的certificate-signing request:

# mkdir -p /etc/lighttpd/ssl/theos.in
# cd /etc/lighttpd/ssl/theos.in

创建 RSA key:

# openssl genrsa -des3 -out theos.in.key 1024

创建CSR:

# openssl req -new -key theos.in.key -out theos.in.csr

程序会提示你输入域名等信息。如果你申请通配证书(wildcard certificate),请用*号,比如*.theos.in。这样,你的子域名也可以用这个证书。

然后,把theos.in.csr提交到CA,通常,这是CA要让你交钱,还要验证你是这个域名的所有者。一旦CA批准了你的申请,你就可以下载证书并将其用在lighttpd服务器上了。

Step # 2: 准备证书
你的CA已经颁发给你证书,你要做的就是将这个证书安装在lighttpd服务器上。

复制下载下来的证书 (theos.in.crt) 到 /etc/lighttpd/ssl/theos.in

# cp theos.in.crt /etc/lighttpd/ssl/theos.in

生成 pem 文件、设置权限:

# cat theos.in.key theos.in.crt > theos.in.pem
# chmod 0600 theos.in.pem
# chown lighttpd:lighttpd /etc/lighttpd/ssl/theos.in -R

Step #3: 确保lighttpd支持ssl
用以下命令检查lighttpd是否支持ssl:

[vivek@cyberciti.biz]$ /usr/sbin/lighttpd -v
lighttpd-1.4.11 (ssl) - a light and fast webserver
Build-Date: Jul 12 2006 13:47:40

否则,参考安装和配置lighttpd。

Step #4: 配置lighttpd,是你的域名支持ssl
打开lighttpd配置文件 

# vi /etc/lighttpd/lighttpd.conf

加下面这段信息:

$SERVER["socket"] == "theos.in:443" {
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/theos.in/theos.in.pem"
ssl.ca-file = "/etc/lighttpd/theos.in/CA_issuing.crt"
server.name = "theos.in"
server.document-root = "/home/lighttpd/theos.in/https"
server.errorlog = "/var/log/lighttpd/theos.in/serror.log"
accesslog.filename = "/var/log/lighttpd/theos.in/saccess.log"
}

其中,

  • ssl.engine = "enable" : 打开lighttpd ssl开关 
  • ssl.pemfile = "/etc/lighttpd/theos.in/theos.in.pem" - 你的PEM文件
  • ssl.ca-file = "/etc/lighttpd/theos.in/CA_issuing.crt" - 你的 intermediate certificate 文件

保存并关闭这个文件。重启lighttpd服务器(会提示你输入SSL private key的密码):

# /etc/init.d/lighttpd restart

显示:

Password:

打开浏览器输入url: https://yourdomain.com (https://theos.in),测试ssl是否工作正常。

翻译自:http://www.cyberciti.biz/tips/how-to-install-ssl-lighttpd-https-configuration.html

注:一般CA的证书都需要钱,如果需要免费的,可使用self-signed证书,或者去http://www.cacert.org

webflier i.t.

Ubuntu 7.10通过USB连接手机GPRS上网

February 5th, 2008
  1. 前提条件:有GPRS Modem功能的手机,并开通GPRS。
  2. 激活手机的GPRS Modem功能(如果需要的话,参考手机说明书),用数据线连接电脑。
  3. 在terminal输入命令dmesg,查看最后几行,找出手机所在的设备
  4. [ 7462.292000] usb 1-1: new full speed USB device using uhci_hcd and address 16
    [ 7462.440000] usb 1-1: configuration #1 chosen from 1 choice
    [ 7462.452000] ipaq 1-1:1.0: PocketPC PDA converter detected
    [ 7462.464000] usb 1-1: PocketPC PDA converter now attached to ttyUSB0
    [ 9148.688000] usb 1-2: USB disconnect, address 14

    比如从以上信息可以看出,手机的设备名是ttyUSB0,记下备用。

  5. 以root身份创建文件:/etc/ppp/peers/gprs.chat
  6. TIMEOUT 35
    ECHO ONHANGUP ON''      \rAT
    OK      ATZ
    OK      'AT+CGDCONT=1,"IP","CMNET","",0,0'
    OK      ATD*99#
    CONNECT ""

    关于最后两行,需要的话请参考说明书,这两行的参数和在windows上用你手机上网时设置的参数是一样的。
    第一行为额外命令,第二行为拨的号码。

  7. 以root身份创建文件:/etc/ppp/peers/gprs
  8. /dev/ttyUSB0
    connect '/usr/sbin/chat -v -f /etc/ppp/peers/gprs.chat'
    noauth
    defaultroute
    usepeerdns
    lcp-echo-interval 65535
    debug

    以上的ttyUSB0即第3步中记录的设备名,根据自己的实际情况替换。

那么怎么拨号上网呢?只需在terminal中敲入:pon gprs,随后可以敲plog查看连接信息。如需中断连接,则只需敲入:poff gprs即可。
以上实验在Ubuntu7.10 + dopod 565 + USB线上通过。

webflier i.t.

The Controls collection cannot be modified because the control contains code blocks

February 3rd, 2008

原来我在网页中的client javascript ,使用了 <%=%>,如下

function setField() {
  var strUrl = "InvoiceDisplayFieldSetting.aspx?oid=<%=OID%>";
  window.showModalDialog(strUrl,"setField",
  "dialogWidth:400px;dialogHeight:630px;
  center:1;scroll:0;help:0;status:0;resizable=0");
  }


在 html 中的不会有问题, 但使用了<head runat=”server”> 后, client side javacript 就不能使用
解法是override OnInit()
如下

override protected void OnInit(EventArgs e)
  {
  InitializeComponent();
  base.OnInit(e);
  Page.DataBind();
  }

然后在 client side javacript ,改用 <%# OID%>
大功告成

webflier i.t.

Godaddy cron job 的执行时间最大是36秒

January 12th, 2008

想想也是,这种相对来说比较便宜的共享空间,也不会让你占用太多的系统资源。
刚写了一个死循环的脚本,跑了一下,得出的时间是36妙。

#!/usr/local/bin/ruby
# index.cgi
require "time"

i=0
tm = Time.now
newfile = File.open( "time1.html", "w+",0644)
newfile << tm.min().to_s + ":" + tm.sec().to_s + "\n"
newfile.close

while true
  i=i+1
  if i % 1000000 == 0 then
    tm = Time.now
    newfile = File.open("time2.html", "w+",0644)
    newfile << tm.min().to_s + ":" + tm.sec().to_s + "\n"
    newfile.close
  end

end

time1.html和time2.html的时间相差了36妙。
有个老外也碰到了这个问题。
http://bogdan.org.u……html
不光跑ruby有这个问题,跑python,ELF等也有这个问题。

webflier i.t.