nginx+uwsgi+django部署过程

2 篇文章 0 订阅
1 篇文章 0 订阅

最近在做目标检测项目,部署采用nginx+uwsgi+django,中间遇到好多bug,最终的踩完坑之后终于可以跑起来,主要是配置文件要注意,以下是本人最终的配置。

1、centos安装nginx:yum -y install nginx

常用命令:service nginx start|stop|restart|reload

修改配置文件:vim /etc/nginx/nginx.conf

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    
    proxy_set_header Host $host:80;
    proxy_buffering off;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  192.168.5.30;
        #server_name 127.0.0.1;
        root         /usr/share/nginx/html;
        
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

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

        
        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
        location /static/ {
            alias  /home/data/SSD/django_model_deploy/static/;
            index  index.html index.htm;
        }
        location /templates/ {
            alias  /home/data/SSD/django_model_deploy/templates/;
        }
    }

nginx中的地址和static和templates修改一下即可,注意修改配置文件之后,一定要重启nginx

2、切换到自己的环境中source activate django;安装环境最好使用conda

conda create -n env_name python=3.6

3、安装uwsgi:pip3 install uwsgi直接pip安装很省事,不过如果安装的有问题,也可以采用如下方式安装

wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
tar zxvf uwsgi-latest.tar.gz
cd uwsgi-2.0.11.1/
python uwsgiconfig.py --build
python setup.py install

不过以这种方式安装最好在最后两步使用自己准备使用的环境,比如/usr/bin/python3,否则是默认的python环境,可能是编译的uwsgi会默认python2版本

4、安装Django,pip3 install django==3.1.7即可

5、切换到manage.py同级目标下,新增django_wsgi.py和uwsgi-http.xml两个文件

django_wsgi.py:

#!/usr/bin/env python3
#coding: utf-8
import os,sys,django

os.environ.setdefault("DJANGO_SETTINGS_MODULE","winton_bom.settings")
from django.core.handlers.wsgi import WSGIHandler
django.setup()
application=WSGIHandler()

uwsgi-http.xml:

<!-- 若以http方式访问 -->
<uwsgi>
    <http>:8077</http>
    <chdir>/home/data/SSD/django_model_deploy</chdir>
    <module>django_wsgi</module>
    <master>true</master>
    <processes>20</processes> <!-- 进程数 -->
    <daemonize>uwsgi.log</daemonize>
    <anaconda3>/root/.conda/envs/django/bin/python3</anaconda3>
    <virtualenv>/root/.conda/envs/django</virtualenv>
</uwsgi>

6、在使用uwsgi之前最好先以python3 manage runserver 8000运行测试一下保证没有问题,再启动nginx和uwsgi

7、最终在自己之前创建的django环境中启动uwsgi服务:

进入环境:
conda activate django或者source activate django

启动uwsgi命令:
uwsgi -x uwsgi-http.xml

8、如果django遇到STATIC_ROOT之类的错误:可以在urls.py和setting.py中加入

urls.py中加入:
import django
from django.conf.urls.static import static
urlpatterns = [
    url(r'^static/(?P<path>.*)$', django.views.static.serve,{'document_root': settings.STATIC_ROOT })
]

setting.py中加入:
STATIC_ROOT = os.path.join(BASE_DIR,"static")

9、测试成功!

10、还有一个问题,就是使用自己训练的模型进行预测的时候,所有的cores都会被占用,有待解决

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值