ubuntu+uwsgi+nginx+django+supervisor部署

时间:2021-09-01 21:05:27

1、apt-get install uwsgi
django项目中新建uwsgi.xml配置

<uwsgi>
    <socket>127.0.0.1:8001</socket>
    <chdir>/opt/dockeradmin</chdir><!-->项目路径,用来处理请求<-->
    <pythonpath>/opt/dockeradmin</pythonpath> <!-->是应用文件所在的路径,也就是项目所在路径,也可写成'..'意为当前路径<-->
    <module>wsgi</module><!-->wsgi.py文件位置,如果在某个app下面则写成appname.wsgi,因为这个问题曾经报错no python application found, check your startup logs for errors,折腾死了<-->
    <processes>4</processes>
    <py-autoreload>1</py-autoreload>
    <pidfile>/tmp/dockeradmin.pid</pidfile>
</uwsgi>

socket : 8001为项目启动端口
chdir : 为项目所在的绝对路径
使用uwsgi启动项目 : 进入项目目录 –> uwsgi -x wsgi.xml
daemonize 为后台守护开启,指定uwsgi日志路径

uwsgi -x uwsgi.xml –daemonize /var/log/hello.log为配合nginx时的启动方式,如果想要单独使用uwsgi跑django则uwsgi –http :8001 –processes 2 –wsgi-file wsgi.py

2、apt-get install nginx
默认的conf文件是在/etc/nginx/nginx.conf,重启nginx的命令:service nginx start|stop|restart

uwsgi_params是nginx自动生成时的文件,可以放在项目下,也可直接填写nginx所在位置的uwsgi_params,看网上很多配置是把该文件拷贝到项目下,其实没什么必要

/etc/nginx/nginx.conf内容:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush on;

    keepalive_timeout  65;

    #gzip on;

    include /etc/nginx/conf.d/*.conf;

    upstream project1 {
        server 127.0.0.1:8001; # for a web port socket (we'll use this first) 
    }

    upstream project2 {
        server 127.0.0.1:8002; # for a web port socket (we'll use this first)
    }

    server{
        listen       8091;
        #server_name somename alias another.alias;

        location / {
            include /opt/dockeradmin/uwsgi_params;
            uwsgi_pass project1;
        }

        location /static/ {
            alias  /opt/dockeradmin/static/;
        }
    } 

    server{
        listen       8092;
        #server_name somename alias another.alias;

        location / {
            include /etc/nginx/uwsgi_params;
            uwsgi_pass project2;
        }   

        location /static/ {
            alias  /root/project/python-publish/pub-update/static/;
        }   
    }    
}

当debug=False时,即生产环境当中,需要在nginx中配置静态文件的加载,不应该用下面这种方式

url(r’^static/(?P.*)$’,serve, { ‘document_root’: settings.STATIC_ROOT,}), # 解决Debug=False的时候静态资源不生效的问题

这样会增加django压力

3、使用supervisor守护uwsgi
apt-get install supervisor
编辑supervisor配置文件/etc/supervisor/supervisord.conf ,在最后添加programe

    [program:python-update]
     command=/usr/local/bin/uwsgi --xml /root/project/python-publish/pub-update/wsgi.xml
     directory=/root/project/python-publish/pub-update
     startsecs=0
     stopwaitsecs=0
     autostart=true
     autorestart=true

4、仅使用uwsgi+nginx来启动django的话还需要把uwsgi后台启动或者开机启动,ubuntu开机启动参考upstart,centos开机启动参考systemd

最后访问8091、8092端口即可