Nginx + Uwsgi + Virtualenv 部署Django程序

!记得自行修改各配置文件路径,下面每部分配置文件的路径可能并不统一

1. Install

pip install virtualenv
# nginx-full功能完整,支持ssl
apt-get install nginx-full
#uwsgi
apt-get install python-dev
pip install uwsgi

2. Use Virtualenv

详细参考: Python--Virtualenv简明教程

mkdir project_folder&cd project_folder
virtualenv env #创建一个env的目录,用于隔离的环境
# 激活环境,激活之后终端前会带(ENV)
source ./project_folder/env/bin/activate
# 退出环境
deactivate

# pip生成requirements.txt
pip freeze > ./requirements.txt
# pip根据依赖文件,批量安装依赖
pip install -r path/requirements.txt

移动目录后,最好重建env目录

3. Django nginx uwsgi 部署

详细参考1:基于nginx和uWSGI在Ubuntu上部署Django uwsgi和nginx详细

详细参考2:自强学堂:Django 部署(Nginx) supervisor管理进程

3.1 原理

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

3.2 拷贝和创建配置文件

3.2.1 拷贝uwsgi_params

#先拷贝django到project_folder目录下
#测试django项目是否正常
python manage.py runserver 0.0.0.0:8000 # 如果正常 the web client <-> uWSGI <-> Django
#将uwsgi_params拷贝到django项目文件夹下
cp /etc/nginx/uwsgi_params project_folder/django/

3.2.2在django目录下创建mysite_nginx.conf

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}

/etc/nginx/sites-enabled目录下创建本文件的连接,使nginx能够使用它:
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

3.2.3 测试uwsgi

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666

3.2.4 创建mysite_uwsgi.ini文件

# mysite_uwsgi.ini file
[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
# 多线程支持
enable-threads = true
# 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

使用配置文件测试uwsgi

uwsgi --ini mysite_uwsgi.ini

4. 使用supervisor来管理进程

# install 
pip install supervisor
# create config file
echo_supervisord_conf > /etc/supervisord.conf

4.1 添加网站到supervisord配置文件末尾

[program:zqxt]
command=/path/to/uwsgi --http :8003 --chdir /path/to/zqxt --module zqxt.wsgi
directory=/path/to/zqxt
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true

4.2 command

#启动supervisord
supervisord -c /etc/supervisord.conf
#重启单个项目zqxt
supervisorctl -c /etc/supervisord.conf restart zqxt
# supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
# 重新载入配置文件
supervisorctl -c /etc/supervisord.conf reread
# (no such process) 更新增加网站
supervisorctl -c /etc/supervisord.conf update

开机启动supervisor 定时启动supervisor

vim /etc/rc.local
# 在exit 0之前行添加 supervisord -c /etc/supervisord.conf

crontab -e
# 添加下面一行,每隔30分钟检查一次supervisor状态
30 *    * * *   supervisord -c /etc/supervisord.conf

你可能感兴趣的:(Nginx + Uwsgi + Virtualenv 部署Django程序)