nginx+python+fastcgi环境配置(flup版本)

时间:2023-03-10 03:28:55
nginx+python+fastcgi环境配置(flup版本)

昨天花了一整天的时间研究搭建了nginx+python+fastcgi环境,并测试没问题,由于是第一次,并且参考了网上很多东西,网上也有很多,但还是把自己的过程记录下。

主要感谢这位兄弟的文章给了我很大的帮忙http://blog.****.net/linvo/article/details/5870498,不过这位兄弟的测试代码我没跑成功。

 一、环境配置主要分以下几步

1、Linux环境和python环境(此步骤省略)

2、Nginx环境、flup、spawn-fcgi工具的部署如下

  1. wget http://nginx.org/download/nginx-1.2.1.tar.gz
  2. tar -xzvf nginx-1.2.1.tar.gz
  3. cd nginx-1.2.1
  4. ./configure --prefix=/usr/local/nginx-1.2.1 --with-http_stub_status_module --with-http_ssl_module --with-cc-opt='-O2'
  5. make
  6. make install
  7. wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz
  8. tar -xzvf flup-1.0.2.tar.gz
  9. cd flup-1.0.2
  10. python setup.py install
  11. http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
  12. tar -xzvf spawn-fcgi-1.6.3.tar.gz
  13. cd spawn-fcgi-1.6.3
  14. ./configure
  15. make
  16. make install
  17. 默认位置在/usr/local/bin/spawn-fcgi

二、配置nginx.conf支持fastcgi

具体配置不详说,下面是配置的一个虚拟主机。/naiveloafer.cgi就是配置的fastcgi,请求会转发到5678端口的程序,配置好后重启nginx服务。

  1. server {
  2. listen      83;
  3. server_name naiveloafer.xxx.com;
  4. access_log  logs/naiveloafer.xxx.com main;
  5. location / {
  6. root /usr/local/nlweb/htdocs;
  7. index index.html index.htm;
  8. }
  9. location /naiveloafer.cgi {
  10. fastcgi_pass 127.0.0.1:5678;
  11. include fastcgi.conf;
  12. }
  13. }

编写fcgi脚本,并保存为fcgi.py:

  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. #author:naiveloafer
  4. #date:2012-06-07
  5. from flup.server.fcgi import WSGIServer
  6. def naiveloafer_app(environ, start_response):
  7. start_response('200 OK', [('Content-Type', 'text/plain')])
  8. content = "Hello World!naiveloafer"
  9. return [content]
  10. if __name__  == '__main__':
  11. WSGIServer(naiveloafer_app).run()

开启监听,具体参数见那位兄弟的文章

  1. spawn-fcgi -f /usr/local/nlweb/cgi-bin/fcgi.py -a 127.0.0.1 -p 5678 -u nobody -F 5

至此,通过web或者HTTP请求就能从fastcgi返回信息了。但这只是一个具体的配置

具体如何处理请求的参数,获取请求的数据看