美文网首页
python3 uwsgi django nginx配置

python3 uwsgi django nginx配置

作者: 博楠同学 | 来源:发表于2019-10-13 15:21 被阅读0次

系统环境 ubuntu1604

1、安装python3 环境

apt install python3  python3-pip

2、安装uwsgi

pip3 install uwsgi

查看uwsgi是否正常
新建test.py文件

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

这里需要注意下如果是python2 test.py文件需要下面内容

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"

然后运行

uwsgi --http :9000 --wsgi-file test.py

在浏览器那日输入http:服务器ip:9000 查看是否有Hello world输出,有代表正常

3、安装django

pip3 install django

测试django是否运行正常,

cd /home/zhang
django-admin startproject   mysite
cs mysite
python3 manager runserver 0.0.0.0:8000

在服务器上面如果想用ip访问,需要在mysite目录下的settings.py文件中

ALLOWED_HOSTS = []
修改为
ALLOWED_HOSTS = ["*"]

然后在浏览器内http://服务器ip:8000 检查django是否运行正常

4、安装nginx

apt install nginx

然后在浏览器内http://服务器ip 检查nginx是否运行正常

5、uwsgi配置

uwsgi支持 ini、 xml等多种配置,以ini为例,在/etc/目录下新建uwsgi9000.ini,添加如下配置:

[uwsgi]
    # 指定和nginx通信的端口
    socket=127.0.0.1:9000
    # 项目路径
    chdir=/home/zhang/mysite
    # wsgi.py路径
    wsgi-file=mysite/wsgi.py
    # 进程数
    processes=4
    # 线程数
    thread=2
   [uwsgi]
    # 指定和nginx通信的端口
    socket=127.0.0.1:9000
    # 项目路径
    chdir=/home/zhang/mysite
    # wsgi.py路径
    wsgi-file=mysite/wsgi.py
    # 进程数
    processes=4
    # 线程数
    thread=2
    pidfile = /var/run/uwsgi9000.pid 
    daemonize = /home/zhang/uwsgi9000.log


6、nginx配置

这里需要注意最好不要直接修改nginx.conf
我得配置在/etc/nginx/sites-enabled目录下新增django文件,

 server{
        # 指定本项目监听端口,浏览器输入端口
        listen 80;
        # 域名
        server_name django;
        # 指定字符集
        charset utf-8;
        # 指定收集静态文件路径
        location /static {
            alias /home/zhang/mysite/static;
      }
        # 和uwsgi通信端口和通信文件
        location /{
          include uwsgi_params;
          uwsgi_pass 127.0.0.1:9000;
      }
    }

收集静态文件

cd /home/zhang/mysite
 python3 manage.py collectstatic

设置完了后运行uwsgi

uwsgi -d --ini /etc/uwsgi9000.ini
nginx -s reload

相关文章

网友评论

      本文标题:python3 uwsgi django nginx配置

      本文链接:https://www.haomeiwen.com/subject/gqnhmctx.html