昨天花了一整天的時間研究搭建了nginx+python+fastcgi環境,並測試沒問題,由於是第一次,並且參考了網上很多東西,網上也有很多,但還是把自己的過程記錄下。
主要感謝這位兄弟的文章給了我很大的幫忙http://blog.csdn.net/linvo/article/details/5870498,不過這位兄弟的測試代碼我沒跑成功。
一、環境配置主要分以下幾步:
1、Linux環境和python環境(此步驟省略)
2、Nginx環境、flup、spawn-fcgi工具的部署如下
- wget http://nginx.org/download/nginx-1.2.1.tar.gz
- tar -xzvf nginx-1.2.1.tar.gz
- cd nginx-1.2.1
- ./configure --prefix=/usr/local/nginx-1.2.1 --with-http_stub_status_module --with-http_ssl_module --with-cc-opt='-O2'
- make
- make install
- wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz
- tar -xzvf flup-1.0.2.tar.gz
- cd flup-1.0.2
- python setup.py install
- http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
- tar -xzvf spawn-fcgi-1.6.3.tar.gz
- cd spawn-fcgi-1.6.3
- ./configure
- make
- make install
- 默認位置在/usr/local/bin/spawn-fcgi
二、配置nginx.conf支持fastcgi
具體配置不詳說,下面是配置的一個虛擬主機。/naiveloafer.cgi就是配置的fastcgi,請求會轉發到5678端口的程序,配置好后重啟nginx服務。
- server {
- listen 83;
- server_name naiveloafer.xxx.com;
- access_log logs/naiveloafer.xxx.com main;
- location / {
- root /usr/local/nlweb/htdocs;
- index index.html index.htm;
- }
- location /naiveloafer.cgi {
- fastcgi_pass 127.0.0.1:5678;
- include fastcgi.conf;
- }
- }
編寫fcgi腳本,並保存為fcgi.py:
- #!/usr/bin/env python
- #coding=utf-8
- #author:naiveloafer
- #date:2012-06-07
- from flup.server.fcgi import WSGIServer
- def naiveloafer_app(environ, start_response):
- start_response('200 OK', [('Content-Type', 'text/plain')])
- content = "Hello World!naiveloafer"
- return [content]
- if __name__ == '__main__':
- WSGIServer(naiveloafer_app).run()
開啟監聽,具體參數見那位兄弟的文章
- spawn-fcgi -f /usr/local/nlweb/cgi-bin/fcgi.py -a 127.0.0.1 -p 5678 -u nobody -F 5
至此,通過web或者HTTP請求就能從fastcgi返回信息了。但這只是一個具體的配置
具體如何處理請求的參數,獲取請求的數據看