sudo apt-get install fcgiwrapStep 2 :配置
- 文件路径配置
vim /etc/nginx/fcgiwrap.conf
location /cgi/ {
gzip off;
root ${替换为你的代码文件目录};
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include /etc/nginx/fastcgi_params;
fastcgi_param script_FILENAME ${替换为你的代码文件目录}$fastcgi_script_name;
}
- nginx增加路径入口
vim /etc/nginx/sites-available/default
server {
listen 80;
server_name a.yeshen.org;
include fcgiwrap.conf; # 新增配置
}
Step 3 : 简单demo
cd ${替换为你的代码文件目录}
vim test.py
chmod a+x test.py
test.py
#!/usr/bin/python # -*- coding: UTF-8 -*- print "Content-type:text/html" print print '' print '' print '' print 'Step 4: 完整小demoHello World ' print '' print '' print 'Hello World!' print '' print ''
index.html
simple cgi
index.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
__author__ = 'hello@yeshen.org'
print "Content-type:text/html"
print "rn"
with open('index.html', 'r') as f:
print f.read()
get.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
__author__ = 'hello@yeshen.org'
import os
import json
print "Content-type:application/json; charset=UTF-8"
print "rn"
data = {}
for key in os.environ.keys():
data[key] = os.environ[key]
print json.dumps(data)
post.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
__author__ = 'hello@yeshen.org'
import os
import json
import cgi, cgitb
def parse_request_body(storage):
if storage.value is not None and type(storage.value) is str:
return json.loads(storage.value)
else:
return {}
def main():
print "Content-type:application/json; charset=UTF-8"
print "rn"
body = parse_request_body(cgi.FieldStorage())
if body.get("key") == 'value':
body['key'] = "hihi"
print json.dumps(body)
if __name__ == "__main__":
main()
预览地址
参考:- Nginx 上的 Python CgiPython CGI编程



