我是Django的新手。试图让我的应用程序在Nginx + gunicorn下运行,但遇到了一些问题。如果有人有见识,我将不胜感激。我已经找到了一些可以在Google上尝试的方法,但gunicorn仍然显得很虚弱。
1)我已经将Django项目从我的开发箱同步到Nginx主机。如果我尝试使用开发服务器启动,则除非使用Python 3,否则它将无法正常工作。
我在下面搜索了...from exec
错误,发现如果以python3
为前缀,我可以使它工作。似乎是针对较大问题的解决方法,但是由于我一直关注的所有教程都未提及这种启动方式。
炸弹:
(venv) $ python manage.py runserver 127.0.0.1:8777
File "manage.py", line 16
) from exc
^
SyntaxError: invalid syntax
这有效:
(venv) $ python3 manage.py runserver 127.0.0.1:8777
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
November 22, 2019 - 15:46:59
Django version 2.2.7, using settings 'auth.settings'
Starting development server at http://127.0.0.1:8777/
Quit the server with CONTROL-C.
[22/Nov/2019 15:47:27] "GET /accounts/login/ HTTP/1.0" 200 1229
^C(venv) $
2)上面,如果使用开发服务器,我可以在Nginx上很好地运行应用程序。但是,如果通过gunicorn
执行,事情就会崩溃。怎么来的? Python说了cannot import name path
,但它似乎可以在开发服务器上正常运行(登录页面也可以正确显示在浏览器中),所以我对问题的所在有些困惑。
(venv) $ gunicorn --bind 127.0.0.1:8777 auth.wsgi
[2019-11-22 09:43:45 +0000] [2239] [INFO] Starting gunicorn 19.7.1
[2019-11-22 09:43:45 +0000] [2239] [INFO] Listening at: http://127.0.0.1:8777 (2239)
[2019-11-22 09:43:45 +0000] [2239] [INFO] Using worker: sync
[2019-11-22 09:43:45 +0000] [2243] [INFO] Booting worker with pid: 2243
Internal Server Error: /accounts/login/
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py", line 172, in _get_response
resolver_match = resolver.resolve(request.path_info)
File "/usr/lib/python2.7/dist-packages/django/urls/resolvers.py", line 364, in resolve
for pattern in self.url_patterns:
File "/usr/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/lib/python2.7/dist-packages/django/urls/resolvers.py", line 407, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/lib/python2.7/dist-packages/django/urls/resolvers.py", line 400, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/django/auth2/auth/urls.py", line 17, in <module>
from django.urls import path, include
ImportError: cannot import name path
[2019-11-22 09:43:58 +0000] [2239] [INFO] Handling signal: winch
[2019-11-22 09:43:58 +0000] [2239] [INFO] Handling signal: winch
^C[2019-11-22 09:45:06 +0000] [2239] [INFO] Handling signal: int
[2019-11-22 15:45:06 +0000] [2243] [INFO] Worker exiting (pid: 2243)
[2019-11-22 09:45:06 +0000] [2239] [INFO] Shutting down: Master
(venv) $ pip freeze
asn1crypto==0.24.0
backports.os==0.1.1
configparser==4.0.2
contextlib2==0.6.0.post1
cryptography==2.1.4
Django==1.11.26
enum34==1.1.6
future==0.18.2
gunicorn==19.9.0
idna==2.6
importlib-metadata==0.23
ipaddress==1.0.17
keyring==10.6.0
keyrings.alt==3.0
more-itertools==5.0.0
path.py==11.5.2
pathlib2==2.3.5
psycopg2-binary==2.8.4
pycrypto==2.6.1
pygobject==3.26.1
python-apt==1.6.4
pytz==2019.3
pyxdg==0.25
scandir==1.10.0
SecretStorage==2.3.1
six==1.13.0
sqlparse==0.2.4
zipp==0.6.0
(venv) $
答案 0 :(得分:0)
您需要确保用于启动 gunicorn 的virtualenv与您在本地创建的环境匹配。
如错误跟踪所示,您的venv
是使用python 2.7创建的(而您的pip freeze
显示了它是安装了Django 1.11的)。
但是您的项目似乎要在Django 3.x中使用Django 2.2进行设置(它使用path
)。当您执行runserver
时,这也在运行。
因此,您应该使用python3创建一个新的virtualenv并重新安装所有依赖项,包括Django 2.2和gunicorn。