在mac上使用Nginx+uWSGI部署django,nginxdjango


1.安装nginx

brew install nginx

1.nginx常用命令

ps -ef|grep nginx  #查看nginx进程

nginx                    #启动nginx

nginx -s stop        #停止nginx

2.nginx 配置

找到nginx.conf文件,主要修改server

#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       8080;       #监听端口
        server_name  localhost;  #这里写自己的域名(没有就用localhost)

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            uwsgi_pass 127.0.0.1:9000;
            include uwsgi_params;
        }

        #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 ssl;
    #    server_name  localhost;

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

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}


2.创建虚拟环境安装django,uwsgi

这里使用virtualenv

virtualenv test      #创建test虚拟环境

source test/bin/activate   #激活test虚拟环境

pip install django     #安装django,之后的配置数据库,数据迁移就不说了

django-admin.py startproject myweb   #创建django项目

cd myweb

虚拟环境中安装uwsgi

pip install uwsgi

uWSGI支持通过多种方式加载配置文件:ini,xml,yaml,json,这里使用.ini,在myweb目录下创建uwsgi.ini文件,这里是个简单的uwsgi配置。

[uwsgi]
socket = 127.0.0.1:9000
chdir = /Users/sliu/test-nginx/testnginx   #django项目地址
wsgi-file = testnginx/wsgi.py   #django项目下的wsgi.py文件,高版本django自带,也可自己新建
home = /Users/sliu/test-nginx/test-nginx   #虚拟环境地址
master = true
processes = 4                                
threads = 2

uwsgi --ini uwsgi.ini    #在uwsgi.ini所在的目录下启动uwsgi

nginx                          #新建终端窗口启动nginx

python manage.py runserver  :9001      #启动django项目(端口不要和uwsgi端口冲突)

之后浏览器中访问http://本机ip:8080/    或者你的域名+nginx监听端口。

相关内容

    暂无相关文章