windows7 python3 django nginx部署

danny posted @ 2014年4月06日 03:03 in python with tags windows python , 4267 阅读

终于轮到我写笔记了. 我的目标是, 简单. 直接贴配置好了.

django: 应用服务器

flup: windows下的python cgi支持

nginx: 网页服务器

第一个坑: django admin的静态文件 调试时没有问题,可是用nignx时有问题.

经过查看nginx的日志, 必须把静态文件带过去static的目录或者指定

 

第二个坑: 运行cgi时出现黑窗口如何解决

使用 RunHiddenConsole 来搞定

网上介绍了cgi运行的办法(会有黑窗口)  run_cgi8801.bat

@echo off
python manage.py runfcgi method=threaded host=127.0.0.1 port=8801
我自定义了一下cgi运行的方式 改造一下manage.py 变成 run_cgi8801.py
#!/usr/bin/python3
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

    from django.core.management import execute_from_command_line

    argv = ['manage.py', 'runfcgi', 'method=threaded', 'host=127.0.0.1', 'port=8801']
    execute_from_command_line(argv)
start_django_nginx.bat
@echo off
 
echo Starting django CGI...
RunHiddenConsole.exe  c:/python33/python.exe D:/workspace/home/run_cgi8801.py

echo Starting nginx...
RunHiddenConsole.exe d:/nginx/nginx.exe -p d:/nginx

pause

stop_django_nginx.bat

@echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
echo Stopping python FastCGI...
taskkill /F /IM python.exe > nul
pause

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  1024;
}


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;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        #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;
        #}
		
		# 静态资源
    location /static {
        alias D:/workspace/home/static; # your Django project's static files - amend as required
    }

        location / {
            # 指定 fastcgi 的主机和端口
            fastcgi_pass 127.0.0.1:8801;
            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;
        }
    }

    # 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  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

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

}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter