uwsgi集成Django部署应用

安装uwsgi

1
python -m pip install uwsgi

测试 uwsgi 是否正常:

  • 新建 test.py 文件,内容如下:
1
2
3
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
  • 使用uwsgi运行test.py
1
uwsgi --http :8000 --wsgi-file test.py
  • 使用uwsgi运行django程序
1
uwsgi --http 0.0.0.0:8003 --file hbec_fof_algorithm_service/wsgi.py
  • 访问$ip:8000,无法加载静态文件

  • 参考配置方法

    • 生成静态文件
      1
      2
      3
      4
      5
      6
      $> prjdir=/root/uwsgiwork/hbec_fof_algorithm_service
      $> conf=/root/conf/fof.ini
      $> cd $prjdir
      $> python manage.py collectstatic
      # 再次启动
      $> uwsgi $conf --static-map /static=$prjdir/common_static/
    • 启动成功

结束服务

1
netstat -tanup |grep uwsgi|awk -F "/" '{print $1}'|awk '{print $7}'|xargs -i kill -9 {}
  • 日志
    1
    uwsgi.log

配置参考

官方文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[uwsgi]
djangoproject=/root/project/hbec-fof-algorithm-service/hbec_fof_algorithm_service
chdir=%(djangoproject)
wsgi-file=%(djangoproject)/hbec_fof_algorithm_service/wsgi.py
module = wsgi
daemonize=%(djangoproject)/logs/uwsgi.log
pidfile=%(djangoproject)/uwsgi.pid
print=%(daemonize)
#socket = 127.0.0.1:9090
http=0.0.0.0:8888
stats=/tmp/myapp.status
memory-report=true
log-format = %(addr) - %(user) [%(ltime)] "%(method) %(uri) %(proto)" %(status) %(size) "%(referer)" "%(uagent)"
master = true //主进程
#vhost = true //多站模式
#no-site = true //多站模式时不设置入口模块和文件
workers = 2 //子进程数
processes = 4 //最大子进程数
threads = 5 //每个worker的线程数
#stats = 0.0.0.0:9191 //通过该端口可以监控 uwsgi 的负载情况
reload-mercy = 10
vacuum = true //退出、重启时清理文件
max-requests = 1000
eable-threads=True
#buffer-size=65535
#harakiri=30
#listen=1024
#limit-as = 512
#pidfile = /var/run/uwsgi9090.pid //pid文件,用于下面的脚本启动、停止该进程
#daemonize = /root/uwsgi9090.log