[关闭]
@nemos 2017-05-06T02:49:44.000000Z 字数 2049 阅读 833

Ngnix+uwsgi部署

web


原理

the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django

安装

sudo apt-get install nginx
apt-get install python-dev #不安装这个,下面的安装可能会失败
pip install uwsgi

指令

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart

sudo service nginx start
sudo service nginx stop
sudo service nginx restart

配置

uwigi

1

uwsgi_params拷到项目目录下

cp /etc/nginx/uwsgi_params /path/to
2

在项目目录下创建 xxx_uwsgi.ini

[uwsgi]
# Django-related settings
# the base directory (full path)
chdir           = /path/to/your/project
# Django's wsgi file
module          = project.wsgi
# the virtualenv (full path)
# home            = /path/to/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
3

从配置文件中运行

uwsgi --ini xxx_uwsgi.ini

nginx

1

在项目目录下创建xxx_nginx.conf

# 连接到django配置
upstream django {
    # server unix://xxx.sock; #使用sock协议,并指定sock文件
    # server 127.0.0.1:8001; #使用tcp协议,指定端口
}
# 服务器的配置
server {
    listen      80; 
    server_name .example.com;
    charset     utf-8;
    client_max_body_size 75M; # max upload size
    location /media  {
        alias /path/to/your/mysite/media;
    }
    location /static {
        alias /path/to/your/mysite/static;
    }
    # 非/media和/staic请求转接到django
    location / {
        uwsgi_pass  django;
        include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}
2

激活,连接配置文件

ln -s /path/to/nginx.conf /etc/nginx/sites-enabled/

django

1.

在setting中添加

STATIC_ROOT = os.path.join(BASE_DIR, "static/")
2

生成静态文件

python manage.py collectstatic

开机启动

/etc/rc.local 添加到exit 0之前
/usr/local/bin/uwsgi --socket /path/to/mysite.sock --module /path/to/mysite.wsgi --chmod-socket=666


进程保护

1

安装 supervisor

(sudo) pip install supervisor
2

生成配置文件

(sudo) echo_supervisord_conf > /etc/supervisord.conf
3

添加

[program:project]
command=/path/to/uwsgi --ini /path/to/uwsgi.ini
directory=/path/to/project
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
4

启动supervisor

(sudo) supervisord -c /etc/supervisord.conf
5

重启项目

(sudo) supervisorctl -c /etc/supervisord.conf restart project
ps

其他命令

(sudo) supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]

nginx+uwsgi官方文件
nginx+uwsgi简书

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注