django + uwsgi + supervisor + nginx 部署

环境安装

yum install python-devel.x86_64 nginx supervisor gcc python-pip -y
pip install uwsgi

测试virtualenv Django环境搭建

pip install virtualenv
mkdir myproject
cd myproject/
virtualenv --no-site-packages venv
source venv/bin/activate
pip install Django==1.9
django-admin startproject mysite
cd mysite/
python manage.py startapp blog
python manage.py runserver 0.0.0.0:8080

测试uwsgi启动Django程序

# uwsgi --http :8001 --chdir /path/to/project --home=/path/to/env --module project.wsgi

uwsgi --http :8001 --chdir /root/myproject/mysite  --home=/root/myproject/venv --module mysite.wsgi

创建uwsgi配置文件

mysite-uwsgi.ini

[uwsgi]
# http = :8080
#the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /root/myproject/mysite
# the virtualenv directory(full path)
home = /root/myproject/venv
# Django's wsgi file
wsgi-file = mysite/wsgi.py
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2
 
#monitor uwsgi status
stats = 127.0.0.1:9191
# clear environment on exit
vacuum          = true

配置suppervisor服务

/etc/supervisord.d/mysite.ini

[program:mysite]
command=/usr/bin/uwsgi --ini /root/myproject/mysite-uwsgi.ini
directory=/root/myproject/mysite
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true

启动supervisor服务

systemctl enable supervisord
systemctl start supervisord

配置nginx服务

/etc/nginx/conf.d/mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///root/myproject/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 192.168.75.104; # 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 /root/myproject/mysite/media;  # your Django project's media files - amend as required
    }
 
    location /static {
        alias /root/myproject/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     uwsgi_params; # the uwsgi_params file you installed
    }
}

启动nginx服务

# 检查配置文件是否有语法错误
nginx -t
# 启动服务
systemctl enable nginx && systemctl start nginx

参考文档

官方文档

推荐阅读更多精彩内容