Nginx是一个开源的web服务器,它能够很好地处理高负载的web任务。Nginx在处理静态文件,比如图片、html文档,要比Apache服务器快的多。和Apache不一样,Nginx本身并没有提供对CGI脚本的内部支持,所以需要其他的应用加以辅助。
作为Nginx的辅助应用,uWSGI是一个不错的选择。uWSGI是一个快速发展的项目,它拥有众多的插件供你原则,当然,CGI也是其中之一。插件化的结构使得它能扩展支持多种平台和语言,值得一提的是,该项目名字中'WSGI'部分取自该项目的第一个插件——同名python web协议——WSGI。
Nginx的安装
sudo apt-get install nginx
配置/etc/nginx/sites-enabled/default文件
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
#nginx访问根路径
root /var/www;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /cgi-bin {
#引入uwsgi_params文件
include uwsgi_params;
#对当前location的请求进行cgi脚本处理
uwsgi_modifier1 9;
#TCP连接方式
uwsgi_pass 127.0.0.1:9000;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
<span style="font-family:SimSun;">}</span>
重启Nginx
sudo service nginx restart
访问localhost,出现如下信息,证明Nginx已配置成功。
It works!
This is the default web page for this server.
The web server software is running but no content has been added, yet.
uwsgi的安装(CGI)
curl http://uwsgi.it/install | bash -s cgi /tmp/uwsgi
在tmp目录下配置uwsgi运行环境,更多配置在这里。
[uwsgi]
plugins = cgi
socket = 127.0.0.1:9000
chdir = /var/www/cgi-bin/
cgi=/cgi-bin=/var/www/cgi-bin/
cgi-helper=.py=python
启动uwsgi服务
sudo -u www-data ./uwsgi ./uwsgi_config.ini
在目录/var/www/cgi-bin/下编写CGI测试脚本hello.py
#!/usr/bin/env python
print "Content-type: text/html\n\n"
print "<h1>Hello World</h1>"
更改脚本权限
sudo chmod +x hello.py
访问地址http://127.0.0.1/cgi-bin/hello.py,页面显示大大的Hello World,配置成功。