MENU
安装uwsgi
pip install uwsgi
测试是否安装成功

django官网有相关教程 How to use Django with uWSGI

参考 uwsgi官网

cd /home
# 创建测试文件
vi text.py
# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2

运行测试 uwsgi --http :8001 --wsgi-file test.py

登录服务器,进去控制台,安全组配置,开放相关端口(自定义TCP),如8001

打开浏览器 [域名]:8001 出现 hello world

添加指定项目目录和虚拟环境目录
uwsgi --chdir /home/mysite --home /home/mysite_env/ --http :8001 --module mysite.wsgi:application

没有显示静态文件,此时就需要NginX了

安装NginX

先更新 apt-get update

安装 apt-get install nginx

联系uwsgi和nginx

添加配置文件mysite.conf

cd /etc/nginx
cd sites-available
# rm default.conf
cd ..
vi mysite.conf
# mysite.conf
server {
    listen 80;
    server_name mysite;
    charset utf-8;

    # max upload size
    client_max_body_size 75M;

    location /static {
        alias /home/mysite/static;
    }

    location /media {
        alias /home/mysite/media;
    }

    location / {
        uwsgi_pass 127.0.0.1:8001;
        include /etc/nginx/uwsgi_params;
    }
}

创建配置文件

cd home
midir mysite_uwsgi
cd mysite_uwsgi
vi mysite.ini
# mysite.ini
[uwsgi]
chdir = /home/mysite
home = /home/mysite_env
module = mysite.wsgi:apalication

master = True
processes = 4
harakiri = 60 # 请求超时时间60s
max-requests = 5000

socket = 127.0.0.1:8001
uid = nginx
gid = nginx

pidfile = /home/mysite_uwsgi/master.pid
daemonize = /home/mysite_uwsgi/mysite.log
vacuum = True

启动uwsgi uwsgi --ini /home/mysite_uwsgi/mysite.ini

检查 ps -aux | grep uwsgi

多条进程即为正常

cd /etc/nginx/
# 创建软链接
ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/mysite.conf
ls sites-enabled/
# 测试
nginx -t
# 重启
service nginx restart
# 停止 stop 启动 start
# 重新加载
uwsgi --reload /home/mysite_uwsgi/master.pid
# 关闭相关进程
ps -aux | grep uwsgi |awk '{print $2}' |xargs kill -9

参考来自:【Django2.0教程】39.用Nginx+uWSGI部署