我正在尝试使用带mod_wsgi的Apache Web服务器在Windows服务器上运行Django应用程序。
我正在使用celery运行一些异步任务,并且该应用程序在localhost中正常运行,但是在网络服务器上运行时出现以下错误:
[Wed Sep 18 14:12:48.787255 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] mod_wsgi (pid=9608): Target WSGI script 'C:/Apache24/htdocs/webapp/djmaccherone/wsgi.py' cannot be loaded as Python module.
[Wed Sep 18 14:12:48.787255 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] mod_wsgi (pid=9608): Exception occurred processing WSGI script 'C:/Apache24/htdocs/webapp/djmaccherone/wsgi.py'.
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] Traceback (most recent call last):\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:/Apache24/htdocs/webapp/djmaccherone/wsgi.py", line 35, in <module>\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] import djcelery\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\djcelery\\__init__.py", line 34, in <module>\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] from celery import current_app as celery # noqa\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\celery\\local.py", line 509, in __getattr__\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] module = __import__(self._object_origins[name], None, None, [name])\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\celery\\_state.py", line 17, in <module>\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] from celery.utils.threads import LocalStack\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\celery\\utils\\__init__.py", line 9, in <module>\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] from .nodenames import worker_direct, nodename, nodesplit\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\celery\\utils\\nodenames.py", line 9, in <module>\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] from kombu.entity import Exchange, Queue\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\kombu\\entity.py", line 6, in <module>\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] from .abstract import MaybeChannelBound, Object\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\kombu\\abstract.py", line 6, in <module>\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] from .connection import maybe_channel\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\kombu\\connection.py", line 23, in <module>\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] from kombu import exceptions\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\kombu\\exceptions.py", line 6, in <module>\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] from amqp import ChannelError, ConnectionError, ResourceError\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\amqp\\__init__.py", line 33, in <module>\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] from .connection import Connection # noqa\r
[Wed Sep 18 14:12:48.792143 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\amqp\\connection.py", line 21, in <module>\r
[Wed Sep 18 14:12:48.793115 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] from .transport import Transport\r
[Wed Sep 18 14:12:48.793115 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "C:\\virtualenvs\\webapp\\Lib\\site-packages\\amqp\\transport.py", line 8, in <module>\r
[Wed Sep 18 14:12:48.793115 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] import ssl\r
[Wed Sep 18 14:12:48.793115 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] File "c:\\programdata\\anaconda3\\Lib\\ssl.py", line 98, in <module>\r
[Wed Sep 18 14:12:48.793115 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] import _ssl # if we can't import it, let the error propagate\r
[Wed Sep 18 14:12:48.793115 2019] [wsgi:error] [pid 9608:tid 1352] [client 35.xxx.xxx.xx:58389] ImportError: DLL load failed: The specified module could not be found.\r
我正在使用以下设置。
wsgi.py
import os
import site
import sys
venv_file = 'C:/virtualenvs/webapp/Scripts/activate_this.py'
exec(open(venv_file).read(), {'__file__': venv_file})
#activate_this_file = 'C:/virtualenvs/webapp/Scripts/activate_this.py'
#exec(activate_this_file, dict(__file__= activate_this_file))
# Add the app's directory to the PYTHONPATH
sys.path.append('C:/Apache24/htdocs/webapp')
sys.path.append('C:/Apache24/htdocs/webapp/djmaccherone')
#sys.path.append('C:/virtualenvs/webapp/Lib/site-packages/django')
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('C:/virtualenvs/webapp/Lib/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'djmaccherone.settings'
#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djmaccherone.settings")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djmaccherone.settings')
import djcelery
djcelery.setup_loader()
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
os.environ.setdefault('FORKED_BY_MULTIPROCESSING', '1')
app = Celery('project', broker='redis://localhost:6379/0', backend='redis://localhost')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks(settings.INSTALLED_APPS)
settings.py
ALLOWED_HOSTS = [
'localhost',
'35.xxx.xxx.xx',
'127.0.0.1',
]
INSTALLED_APPS = [
'djcelery',
...,
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_celery_results',
]
djcelery.setup_loader()
BROKER_URL = 'redis://localhost:6379/0'
BROKER_TRANSPORT = 'redis'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Europe/Rome'
CELERYD_LOG_FILE = 'logfile.log'
httpd.conf
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
LoadFile "c:/programdata/anaconda3/python37.dll"
LoadModule wsgi_module "c:/programdata/anaconda3/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/programdata/anaconda3"
Listen 8081
<VirtualHost *:8081>
ServerName frontend.com
ServerAlias www.frontend.com
ErrorLog "logs/frontend.error.log"
CustomLog "logs/frontend.access.log" combined
WSGIPassAuthorization On
WSGIScriptAlias / C:/Apache24/htdocs/webapp/project/wsgi.py
<Directory C:/Apache24/htdocs/webapp/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static C:/Apache24/htdocs/webapp/static
<Directory C:/Apache24/htdocs/webapp/static>
Require all granted
</Directory>
</VirtualHost>
您能帮我解决错误吗?
产生错误的行似乎是
from celery import Celery
在 celery.py 文件中。
谢谢!