To make my django web project run, I may or may not have made a blunder in production environment. I am already way past the deadline. Earlier the site was working but static content was not displaying. So after making a few changes, all I am getting is different errors everytime. I installed cerbot for ssl. That may have also been causing a lot of issues.
settings.py
mport os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'django-insecure-%z88$&huycw6@u%i7-0e#h^_3+ffna$^uk+fvsm=9&=-6rsnz$'
DEBUG = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '134.209.104.166', 'binodverma.com', 'wwww.binodverma.com']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'userauth',
'blog',
'tool_kit',
'appointments',
'contact',
'adminp',
'resource_store',
'implinks',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'spiderweb.urls'
WSGI_APPLICATION = 'spiderweb.wsgi.application'
import dj_database_url
DATABASES = {
'default': dj_database_url.config(default='sqlite:///db.sqlite3')
}
BASE_DIR = Path(__file__).resolve().parent.parent
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
# Static root for production
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
# Additional directories where Django looks for static file
AUTH_USER_MODEL = 'userauth.CustomUser'
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend', # Default
]
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Media files (Uploaded content)
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
# Tells Django to use the X-Forwarded-Host header from the proxy, allowing it to know the original host requested by the client.
USE_X_FORWARDED_HOST = True
# Tells Django to use the X-Forwarded-Port header from the proxy, indicating the port number used by the client.
USE_X_FORWARDED_PORT = True
# Instructs Django to trust the X-Forwarded-Proto header, which is set by the proxy server, to determine whether the request is secure (HTTPS).
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Forces all HTTP requests to be redirected to HTTPS.
SECURE_SSL_REDIRECT = True
# Ensures that the CSRF cookie is only sent over HTTPS connections.
CSRF_COOKIE_SECURE = True
# Ensures that the session cookie is only sent over HTTPS connections.
SESSION_COOKIE_SECURE = True
# Enables HTTP Strict Transport Security (HSTS) for the specified duration (in seconds), forcing browsers to only connect via HTTPS.
SECURE_HSTS_SECONDS = 31536000
# Applies HSTS policy to all subdomains.
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
# Allows the domain to be included in browsers' HSTS preload list, ensuring maximum protection.
SECURE_HSTS_PRELOAD = True
# Enables the X-Content-Type-Options header, preventing browsers from MIME-sniffing a response away from the declared content-type.
SECURE_CONTENT_TYPE_NOSNIFF = True
# Controls the information sent in the Referer header, improving privacy and security by not sending the referrer from HTTPS to HTTP.
SECURE_REFERRER_POLICY = 'no-referrer-when-downgrade'
/etc/nginx/sites-available/spidernet
Redirect all HTTP traffic to HTTPS
server {
listen 80;
server_name 134.209.104.166 binodverma.com www.binodverma.com;
# Redirect all HTTP to HTTPS
return 301 https://$host$request_uri;
}
# Main server block for HTTPS
server {
listen 443 ssl; # Listen on port 443 for HTTPS
server_name binodverma.com www.binodverma.com;
ssl_certificate /etc/letsencrypt/live/binodverma.com/fullchain.pem; # SSL certificate path
ssl_certificate_key /etc/letsencrypt/live/binodverma.com/privkey.pem; # SSL certificate key path
include /etc/letsencrypt/options-ssl-nginx.conf; # SSL options for security
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # Diffie-Hellman parameter
# Location block for static files
location /static/ {
root /home/vishesh/spidernet/staticfiles;
}
# Location block for media files
location /media/ {
alias /home/vishesh/spidernet/media/; # Must be the same as MEDIA_ROOT
access_log /var/log/nginx/media_access.log;
error_log /var/log/nginx/media_error.log;
}
# Proxying to Gunicorn
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
access_log /var/log/nginx/django_access.log;
error_log /var/log/nginx/django_error.log;
}
}
# Fallback server block for non-matching hostnames
server {
listen 80;
server_name 134.209.104.166 binodverma.com www.binodverma.com;
return 404; # Return 404 for any unmatched requests
}
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/home/vishesh/spidernet
ExecStart=/home/vishesh/env/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
spiderweb.wsgi:application
[Install]
WantedBy=multi-user.target
(env) vishesh@cactus:~/spidernet$ ls
README.md adminp appointments blog contact db.sqlite3 gunicorn_config.py implinks manage.py media requirements.txt resource_store spiderweb static staticfiles templates test.py tool_kit userauth