最近在做目标检测项目,部署采用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都会被占用,有待解决