python - uwsgi 无法从 .ini 文件中为 Django 提供服务
问题描述
重现步骤:
mkdir django-uwsgi
cd django-uwsgi
python -m venv venv
source venv/bin/activate
pip install Django uwsgi
django-admin startproject mysite
cd mysite
python manage.py migrate
尝试通过命令行使用 uwsgi 在本地为 django 提供服务:
uwsgi --module mysite.wsgi --http :8000
结果:有效!是的!
现在,创建一个uwsgi.ini
包含以下内容的文件:
[uwsgi]
module = mysite.wsgi
http = :8000
然后使用uwsgi --ini uwsgi.ini
结果
ModuleNotFoundError: No module named 'mysite.wsgi # define the django module to use'
好的...所以我们需要别的东西...
尝试添加当前目录...
chdir = {WORKSPACE}/django-uwsgi/mysite
是的?结果:
ModuleNotFoundError: No module named 'mysite.wsgi # define the django module to use'
尝试指定 virtualenv。结果:
virtualenv = {WORKSPACE}/django-uwsgi/venv
# or, or both
home = {WORKSPACE}/django-uwsgi/venv
是的?结果:
ModuleNotFoundError: No module named 'mysite.wsgi # define the django module to use'
这是一个完整的tracebakc:
$ uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.19.1 (64bit) on [Fri Jul 16 14:44:03 2021] ***
compiled with version: 9.3.0 on 16 July 2021 18:42:33
os: Linux-5.11.0-7614-generic #15~1622578982~20.04~383c0a9-Ubuntu SMP Wed Jun 2 00:57:14 UTC 2
nodename: pop-os
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /home/loicgasser/Workspace/django-uwsgi/mysite
detected binary path: /home/loicgasser/Workspace/django-uwsgi/venv/bin/uwsgi
chdir() to /home/loicgasser/Workspace/django-uwsgi/mysite
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 126880
your memory page size is 4096 bytes
detected max file descriptor number: 8192
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8010 # expose uWSGI directly to the public, change to http-socket when we'll use nginx fd 4
spawned uWSGI http 1 (pid: 756173)
uwsgi socket 0 bound to TCP address 127.0.0.1:46441 (port auto-assigned) fd 3
Python version: 3.8.3 (default, Feb 22 2021, 17:48:15) [GCC 9.3.0]
PEP 405 virtualenv detected: /home/loicgasser/Workspace/django-uwsgi/venv
Set PythonHome to /home/loicgasser/Workspace/django-uwsgi/venv
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x55cc5de2c3a0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
ModuleNotFoundError: No module named 'mysite.wsgi # define the django module to use'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 756172, cores: 1)
请注意,我使用 pyenv:
lrwxrwxrwx 1 loicgasser loicgasser 49 Jul 16 14:41 python -> /home/loicgasser/.pyenv/versions/3.8.3/bin/python
lrwxrwxrwx 1 loicgasser loicgasser 6 Jul 16 14:41 python3 -> python
我已经被困了一段时间......任何帮助将不胜感激。
解决方案
这是对我有用的配置。chdir 应该是 manage.py 所在的目录,这样当您提到 wsgi-file 时,它会从该路径转到该文件。还提供您的 virtualenv 的完整路径
[uwsgi]
http = 0.0.0.0:9999
virtualenv =/path/to/djangapp/env
chdir = /path/to/djangapp -- this is where manage.py exists
wsgi-file = djangapp/wsgi.py --wsgi file path from above path
processes = 4
threads = 2
vacuum = True
harakiri=20
stats = 0.0.0.0:8000
daemonize = /path/to/djangapp/logs/CustomerCare-@(exec://date +%%Y-%%m-%%d).log
log-reopen = true
推荐阅读
- kotlin - 从字符串拆分空间在 Kotlin 中不起作用
- pdf - 数字签名证书推荐
- python - 缓存 Django 页面以快速加载
- python - 如何在一行中编写嵌套的 if-elif-else 条件?
- git - Git - 从提交到主文件中永久排除文件(删除)
- symfony - 如何在我的项目 symfony 4 中卸载包 lexik / jwt-authentication-bundle
- reactjs - 如何在 React + Typescript 中为延迟加载创建 hoc
- bash - bash 星号转义在 echo 中打印 \*
- c# - .NET Core Fakes 和 VS 2019 Preview 6.9.0 Preview 2.0 找不到类型或命名空间名称
- docker - Docker 将文件从主机复制到映像