debian 下安装 nginx + uWSGI

编译安装 nginx
sudo aptitude install gcc g++ libpcre3-dev libssl-dev

wget http://nginx.org/download/nginx-0.8.54.tar.gz
tar xzvf nginx-0.8.54.tar.gz
cd nginx-0.8.54/
./configure \
–prefix=/home/gamexg/web \
–sbin-path=/home/gamexg/web/sbin/nginx \
–conf-path=/home/gamexg/web/etc/nginx/nginx.conf \
–error-log-path=/home/gamexg/web/var/log/nginx/error.log \
–pid-path=/home/gamexg/web/var/run/nginx/nginx.pid \
–lock-path=/home/gamexg/web/var/lock/nginx.lock \
–user=nginx \
–group=nginx \
–with-http_gzip_static_module \
–http-log-path=/home/gamexg/web/var/log/nginx/access.log \
–http-client-body-temp-path=/home/gamexg/web/var/tmp/nginx/client/ \
–http-proxy-temp-path=/home/gamexg/web/var/tmp/nginx/proxy/ \
–http-fastcgi-temp-path=/home/gamexg/web/var/tmp/nginx/fcgi/

make && make install

配置nginx
增加一段记录
location /site1 {
uwsgi_pass 127.0.0.1:9000;
include uwsgi_params;
}

编译安装 uWSGI

sudo aptitude install python-dev libxml2-dev
wget http://projects.unbit.it/downloads/uwsgi-0.9.6.8.tar.gz
tar zxfv uwsgi-0.9.6.8.tar.gz
cd uwsgi-0.9.6.8/
make

配置 django 使用 uWSGI
gamexg@vps1:~/web$ mkdir site
gamexg@vps1:~/web$ cd site
gamexg@vps1:~/web/site$ mkdir site1
gamexg@vps1:~/web/site$ cd site1/
gamexg@vps1:~/web/site/site1$ mkdir www
gamexg@vps1:~/web/site/site1$ mkdir www/static
gamexg@vps1:~/web/site/site1$ django-admin startproject site1
gamexg@vps1:~/web/site/site1$ cd site1/
gamexg@vps1:~/web/site/site1/site1$ ./manage.py startapp app1
gamexg@vps1:~/web/site/site1/site1$ vi myapp.py
gamexg@vps1:~/web/site/site1/site1$ cat myapp.py
import os
os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘test1.settings’
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

vi uwsgi.xml

127.0.0.1:9000
true /home/gamexg/web/var/uwsgi.pid 3 /home/gamexg/web/site/site1/site1 /home/gamexg/web/site/site1 myapp

启动
/home/gamexg/web/uwsgi-0.9.6.8/uwsgi -x /home/gamexg/web/site/site1/uwsgi.xml

No comment »

Windows 下以 nginx + fastcgi 运行 Django 或 web.py

从 http://www.saddi.com/software/flup/dist/ 下载 flup 并执行 setup.py install 安装。

从 http://www.kevinworthington.com/category/computers/nginx/ 下载nginx Windows版本的安装程序。
运行并安装。
打开配置文件 nginx/conf/nginx.conf 修改为:
[coolcode download=”nginx.conf”]
#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 64;
}

http {
include mime.types;
default_type application/octet-stream;

#log_format main ‘$remote_addr – $remote_user [$time_local] $request ‘
# ‘”$status” $body_bytes_sent “$http_referer” ‘
# ‘”$http_user_agent” “$http_x_forwarded_for”‘;

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

root /cygdrive/D/html;
index index.html index.htm;

charset utf-8;

#access_log logs/host.access.log main;

# 静态资源
location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{
expires 30d;
break;
}

location / {
# 指定 fastcgi 的主机和端口
fastcgi_pass 127.0.0.1:8051;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {
# deny all;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}

# HTTPS server
#
#server {
# listen 443;
# server_name localhost;

# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}
[/coolcode]
现在重启启动 nginx 后在Django项目下执
.\manage.py runfcgi method=threaded host=127.0.0.1 port=8051
就可以了。

这里没有设置 media 目录,现在是以扩展名来分辨是不是转发到Django的程序处理。
想要只允许 nginx 处理media 目录只需要将
location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
换成
location ~ ^/media/
就可以了。

主目录为d:\html,定义是:
root /cygdrive/D/html;

如果运行 web.py 的项目
[coolcode lang=”python” download=”a.py”]
#!/usr/bin/env python
import web
urls = (
‘/.*’, ‘hello’
)
class hello:
def GET(self):
print ‘aaa’
if __name__ == “__main__”: web.run(urls, globals())
[/coolcode]
只需要把执行
.\manage.py runfcgi method=threaded host=127.0.0.1 port=8051
换成执行
a.py 8051 fastcgi
就可以了。

附一个参数列表(发现一般是使用include 包含配置文件fastcgi_params):
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

Comments (1) »