我正在尝试部署一个Django项目,但在将其部署到Web上时遇到了困难。让我简单介绍一下目前遇到的问题:
1.我通过SSH连接到一个远程服务器,并试图在那里部署项目。我目前没有域名,所以现在我使用的是IP地址。
1.我已经设置了Gunicorn和Nginx,并让它们运行,但当我进入浏览器并尝试通过'https://www.nginx.com'访问网站时,我得到以下错误:
Request Method: GET
Request URL: http://**.**.***.***/
Using the URLconf defined in apegd_ai_core.urls, Django tried these URL patterns, in this order:
admin/
The empty path didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
字符串
1.我在“/etc/systemd/system”下有一个名为“apegd_ai. service”的Gunicorn .service文件。配置如下:
[Unit]
Description=Gunicorn instance to serve application
After=network.target
[Service]
User=john
Group=john
WorkingDirectory=/home/john/graph_django/apegd_ai_core
ExecStart=/home/bobby/Documents/graph_project/vDect2/bin/gunicorn -w 4 -b 0.0.0.0:8007 --error-logfile /home/john/gra>
Environment="DJANGO_SECRET_KEY=encrptedsecretkey"
[Install]
WantedBy=multi-user.target
型
1.我在/etc/nginx/sites-enabled下有一个名为apegd_ai.conf的Nginx配置文件,看起来像这样:
server {
listen 80;
server_name **.**.***.***;
location /static/ {
alias /home/jared/graph_django/apegd_ai_core/staticfiles;
}
location / {
proxy_pass http://127.0.0.1:8007;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 443 ssl;
server_name **.**.***.***;
location /static/ {
alias /home/jared/graph_django/apegd_ai_core/staticfiles;
}
ssl_certificate /etc/nginx/ssl/selfsigned.crt;
ssl_certificate_key /etc/nginx/ssl/selfsigned.key;
location / {
proxy_pass http://127.0.0.1:8007;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
型
1.我有一个urls.py文件,它看起来像这样:
from django.contrib import admin
from django.urls import include, path,re_path
urlpatterns = [
path("admin/", admin.site.urls),
path(" ",include('apegd_ai_core.urls')),
]
型
1.我有一个settings.py文件,看起来像这样(我知道调试是开着的,哈哈):
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
APEGHDAI_DIR = os.path.join(BASE_DIR, 'api', 'APEGHDAI')
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['**.**.***.***']
CORS_ALLOW_ALL_ORIGINS = True
型
1.当我尝试通过浏览器访问应用程序时,我得到以下错误:
Traceback (most recent call last):
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 135, in>
self.handle_request(listener, req, client, addr)
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 178, in>
respiter = self.wsgi(environ, resp.start_response)
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/django/core/handlers/wsgi.py", line 124>
response = self.get_response(request)
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/django/core/handlers/base.py", line 140>
response = self._middleware_chain(request)
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/django/core/handlers/exception.py", lin>
response = response_for_exception(request, exc)
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/django/core/handlers/exception.py", lin>
response = handle_uncaught_exception(
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/django/core/handlers/exception.py", lin>
return debug.technical_500_response(request, *exc_info)
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/django/views/debug.py", line 67, in tec>
html = reporter.get_traceback_html()
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/django/views/debug.py", line 410, in ge>
c = Context(self.get_traceback_data(), use_l10n=False)
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/django/views/debug.py", line 379, in ge>
"settings": self.filter.get_safe_settings(),
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/django/views/debug.py", line 154, in ge>
settings_dict[k] = self.cleanse_setting(k, getattr(settings, k))
File "/home/sidharth/Documents/graph_project/vDect2/lib/python3.8/site-packages/django/conf/__init__.py", line 111, in >
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
型
1.我的SSL密钥和证书都设置好了,存在于我的Nginx目录下的SSL文件夹中。
1.当我运行“sudo systemctl status nginx”和“sudo systemctl status apegd. service”时,Nginx和Gunicorn似乎都运行得很好。
1.'$DJANGO_SECRET_KEY'被设置为一个环境变量,当我回显它时,终端正确地输出它。
总而言之,我不知所措,因为我以前从来没有部署过Django项目,我肯定不知所措,哈哈。有人能告诉我为什么我不能从浏览器访问我的应用程序吗?非常非常感谢!
1条答案
按热度按时间bvjxkvbb1#
看起来django应用程序在阅读它时有问题,即使你可以从终端回显它。最好的做法是创建一个.env文件并放置在你的项目的根目录中(manage.py所在的位置)。
像这样构造.env文件,在新的行中使用您可能需要的每个键值对。
字符串
然后在settings.py中修改您的代码,以便也从.env文件加载环境变量。还要注意来自
django-environ
库的额外导入environ
(需要添加到您的要求并pip安装..)。以相同的方式加载您可能需要的其他环境变量。型