redhat环境下lighttpd+flup+web.py架设笔记

首先,理解lighttpd,flup和web.py这三者之间的关系

1. lighthttpd: 功能同tomcat,做为一个请求代理(request proxy),它主要负责根据配置来把不同的请求分发到不同的server来进行处理,它也可以作为一个web server自己来处理一些静态文件请求。同时对于fastcgi等请求,它会把这些请求转发给flup这些server/gateway来进行处理。 tomcat运行比较稳定但是占用资源比较多,而lighthttpd占用资源少,但是处理php有时会不太稳定。

2.  flup: 一个用python写的web server,也就是cgi中所谓的Server/Gateway,它负责接受lighttpd转发的请求,并调用你写的程序 (application),并将application处理的结果返回到apache/lighttpd

3. web.py: 应该说有了上面的东西你就可以开始编写你的web程序了,但是问题是你就要自己处理浏览器的输入输出,还有cookie、session、模板等各种各样的问题了,web.py的作用就是帮你把这些工作都做好了,它就是所谓的web framework,另外一个出名的是django,不过感觉太复杂了,web.py差不多就够用了(目前web.py对session的支持不是太好)。

 

其次,安装

1. flup: 到http://trac.saddi.com/flup下载flup安装包,假设已经把安装包下载到了/tmp目录下。

  1. #cd /tmp  
  2. #tar -zxvf flup-1.0.1.tar.gz  
  3. #cd flup-1.0.1  
  4. #python setup.py install

2. web.py: 到http://webpy.org/下载web.py安装包,假设已经把安装包下载到了/tmp目录下。

  1. #cd /tmp
  2. #tar -zxvf web.py-0.23.tar.gz
  3. #cd web.py-0.23
  4. #python setup.py install

3. lighttpd: 到http://www.lighttpd.net/下载lighthttpd安装包,假设已经把安装包下载到了/tmp目录下。

    如果想在lighttpd中使用正则,则需要pcre的支持。所以先到http://sourceforge.net/project/showfiles.php?group_id=10194&package_id=9960&release_id=624398下载pcre-7.8.tar.gz 到/tmp目录下,进行编译安装

  1. #cd /tmp
  2. #tar -zxvf pcre-7.8.tar.gz 
  3. #cd pcre-7.8
  4. #./configure
  5. #make clean
  6. #make
  7. #make install

    然后安装lighttpd

  1. #cd /tmp
  2. #tar xzvf lighttpd-1.4.19.tar.gz 
  3. #cd lighttpd-1.4.19
  4. #./configure --prefix=/usr/local/lighttpd --with-openssl --with-openssl-libs=/usr/lib
  5. #make clean 
  6. #make 
  7. #make install 
  8. //复制配置文件到指定的文件夹下
  9. #mkdir /usr/local/lighttpd/etc
  10. #cp doc/lighttpd.conf /usr/local/lighttpd/etc
  11. //为lighttpd单独创建一个用户组和用户
  12. #groupadd lighttpd
  13. #useradd -g lighttpd lighttpd
  14. //创建lighttpd工作目录
  15. #mkdir /www
  16. #chown -R lighttpd.lighttpd /www
  17. //创建lighttpd日志目录
  18. #mkdir /lighttpdlogs
  19. #chown -R lighttpd.lighttpd /lighttpdlogs
  20. #chmod 750 /lighttpdlogs
  21. //安装启动脚本
  22. #cp doc/rc.lighttpd.redhat /etc/init.d/lighttpd
  23. #chkconfig lighttpd on
  24. //修改启动脚本,主要是修改lighttpd和lighttpd.conf文件所在的目录,以便能正常启动该服务
  25. #vim /etc/init.d/lighttpd
  26. //找到LIGHTTPD_CONF_PATH,并修改为LIGHTTPD_CONF_PATH="/usr/local/lighttpd-1.4.19/etc/lighttpd.conf"
  27. //找到lighttpd,并修改为lighttpd="/usr/local/lighttpd-1.4.19/sbin/lighttpd"

    在第四行中,--with-openssl --with-openssl-libs=/usr/lib表示编译ssl模块,从而可以支持https连接。至此lighttpd已经安装完毕了。接下来的工作就是配置文件的修改。

 

再次,配置文件的修改

    配置文件中有一些日志文件、工作目录、用户组用户名等一些配置项,所以需要在配置文件中找到如下几项,并设置为如下值:

  1. server.document-root        = "/www/"
  2. server.errorlog             = "/lighttpdlogs/lighttpd.error.log"
  3. accesslog.filename          = "/lighttpdlogs/access.log"
  4. static-file.exclude-extensions = ( ".py"".php"".pl"".fcgi" )
  5. server.username            = "lighttpd"
  6. server.groupname           = "lighttpd"

    server.modules定义了lighttpd运行时要使用的包,因为我们需要使用alias为界面代码保存的路径指定别名,所以需要将其前面的"#"去掉。

    为了使lighttpd支持web.py,我们需要mod_fastcgi模块。为了便于配置文件的管理,我们把配置文件进行分离,然后在lighttpd.conf文件中引入该mod_fastcgi.conf包。在lighttpd.conf中添加一条引入配置包的语句:

  1. include "/usr/local/lighttpd/etc/mod_fastcgi.conf"

    然后我们创建并写入mod_fastcgi.conf配置文件:

  1. #vim mod_fastcgi.conf
  2. #vim mod_fastcgi.conf
  3. #mv mod_fastcgi.conf /usr/local/lighttpd/etc

    其中mod_fastcgi.conf文件内容如下:

  1. server.modules += ("mod_fastcgi"
  2. server.modules  += ( "mod_rewrite" )
  3. fastcgi.server = ( "/index.py" =>
  4.                 (("socket" => "/tmp/fastcgi.socket",
  5.                   "bin-path" => "/www/index.py",
  6.                   "max-procs" => 1,
  7.                   "bin-environment" => (
  8.                         "REAL_SCRIPT_NAME" => ""
  9.                   ),
  10.                   "check-local" => "disable"
  11.                 ))
  12.                 )
  13. url.rewrite-once = (
  14.        "^/favicon.ico$" => "/satic/favicon.ico",
  15.        "^/py/(.*)$" => "/index.py/$1",
  16.        )

    上面的配置文件中的fastcgi.server设置表明lighttpd的fastcgi请求会由/www目录下的index.py脚本来进行处理。url.rewrite-once设置表明所有的/py/的请求会由index.py来进行处理。这两项共同作用下实现功能为:形如http://127.0.0.1/py/.*的所有fastcgi请求都转由index.py脚本来进行处理,并把结果返回给lighttpd.如果/www目录下还有静态页面index.html,我们则可以通过请求http://127.0.0.1/ 或 http://127.0.0.1/index.html 来进行访问。

    接下来我们在/www目录下创建index.py脚本文件:

  1. #cd /www
  2. #vim index.py

    index.py脚本文件内容如下:

  1. #!/usr/bin/env python
  2. import web
  3. import time
  4. from flup.middleware.session import MemorySessionStore, SessionMiddleware
  5. from list import *
  6.                                                                                                                                                                                     
  7. urls = (
  8.     '/py/count''count',
  9.     '/py/reset''reset',
  10.     '/py/list''list',
  11.     '/py/(.*)''index'
  12.     )
  13.                                                                                                                                                                                     
  14. #web.config['session'] = MemorySessionStore(timeout=5 )
  15. #session = web.config['session'].createSession()
  16. def getSession():
  17.     return web.ctx.environ['com.saddi.service.session'].session
  18.                                                                                                                                                                                     
  19. class index:
  20.     def GET(self, name):
  21.         print 'Hello %s!' % web.ctx.environ['REMOTE_ADDR']
  22.                                                                                                                                                                                     
  23. class count:
  24.     def GET(self):
  25.         session = getSession()
  26.         if 'count' in session.keys():
  27.             session['count'] += 1
  28.         else:
  29.             session['count'] = 0
  30.         print session
  31. class reset:
  32.     def GET(self):
  33.         getSession().invalidate()
  34.         web.seeother('/py/')
  35.         return ''
  36.                                                                                                                                                                                     
  37. def Session(app):
  38.     store = MemorySessionStore(1)
  39.     return SessionMiddleware(store, app)
  40.                                                                                                                                                                                     
  41. if __name__ == '__main__':
  42.     web.run(urls, globals(), web.reloader, Session)

    在index.py中import的list.py内容如下:

  1. #! /usr/bin/env python
  2. import os, web
  3.                                 
  4. class list:
  5.     def GET(self):
  6.         data = web.input(path={}, name={})
  7.         path = data.path.value
  8.         print os.popen('ls -l %s' % path).read()

    我们通过请求http://127.0.0.1/py/list?path=/sur,可以看到ie中列举了ls -l /usr的结果。

 

    如果我们需要lighttpd支持https, 则需要在lighttpd.conf配置如下:

  1. $SERVER["socket"] == ":443" {
  2.         ssl.engine      = "enable"
  3.         ssl.pemfile     = "/usr/local/lighttpd-1.4.19/etc/server.pem"
  4.         ssl.ca-file     = "/usr/local/lighttpd-1.4.19/etc/server.crt"
  5.         ssl.use-sslv2   = "disable"
  6. }

    上面配置说明对于443端口的https请求,则通过server.pem证书来验证。这样lighttpd可以同时支持80端口的http请求和443端口的https请求了。对于证书的生成,可以通过如下方式:

  1. #cd /tmp
  2. #openssl -config openssl.cnf -new -x509 -keyout server.key -out server.crt -days 365 -nodes
  3. //此时/tmp目录下会生成server.key和server.crt两个文件,其中key文件为私钥文件,crt文件为证书文件。合并这两个文件,生成pem证书。
  4. #copy /b server.key+server.crt server.pem
  5. //把生成的证书文件拷贝到配置文件中配置的目录下
  6. #mv server.* /usr/local/lighttpd/etc

     我们通过请求https://127.0.0.1/py/list?path=/sur,可以看到效果同http请求。

     如果需要双向证书(即客户端验证服务器端证书,同时服务器端也需要验证客户端证书)的话,则需要对安装包进行相应的patch,然后再设置相关配置项。可以参考:http://trac.lighttpd.net/trac/ticket/1288

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值