I'm in the process of trying to deploy a Django project, and I'm running into difficulties getting it on the web. Let me run through the issues I'm having at the moment:
I SSH into a remote server and am trying to deploy the project there. I don't currently have a domain name, so right now I'm using the IP address.
I have set up both Gunicorn and Nginx and have them running, but when I go to the browser and try to access the site via 'https://...', I get the following error:
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.
- I have a Gunicorn .service file under '/etc/systemd/system' called 'apegd_ai.service'. The configuration is as follows:
[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
- I have an Nginx configuration file called apegd_ai.conf under /etc/nginx/sites-enabled, which looks like this:
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;
}
- I have a urls.py file, which looks like this:
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')),
]
- I have a settings.py file, which looks like this (I know debug is on, haha):
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
- When I try to acess the app through the browser, I get the following error:
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.
Both my SSL key and certificate are set up and exist in my SSL folder within my Nginx directory.
When I run 'sudo systemctl status nginx' and 'sudo systemctl status apegd.service', both Nginx and Gunicorn both seem to be running fine.
The '$DJANGO_SECRET_KEY' is set as an environment variable and when I echo it, the terminal correctly outputs it.
All and all, I'm at a loss, and since I've never deployed a Django project before, I'm definitely at a loss, haha. Would anyone have any insight into why I can't access my applicaiton from my browser? Thank you very, very much!
It seems the django application is having problems reading it even though you are able to echo it from the terminal. Best practice would be just create a .env file and place in the root of your project (where manage.py is).
Structure the .env file like this with each key value pair that you may need in new lines.
And then modify your code in settings.py to load environment variables also from the .env file. Also note the additional import
environwhich comes fromdjango-environlibrary (need to add to your requirements and pip install..). Load other environment variables you may need in the same way..