From 7215bb6b6165048e195af7231caba88601b93468 Mon Sep 17 00:00:00 2001 From: shuvoDAI Date: Thu, 14 Nov 2024 15:46:30 +0600 Subject: [PATCH 1/3] update --- .dockerignore | 39 ++++++++ .gitignore | 90 +++++++++++++++++++ .env => django_project/.env | 0 Dockerfile => django_project/Dockerfile | 11 +-- django_project/django_project/settings.py | 6 +- entrypoint.sh => django_project/entrypoint.sh | 2 +- django_project/requirements.txt | 4 + docker-compose.yml | 20 ++--- nginx/Dockerfile | 4 +- requirements.txt | 2 - 10 files changed, 158 insertions(+), 20 deletions(-) create mode 100644 .dockerignore create mode 100644 .gitignore rename .env => django_project/.env (100%) rename Dockerfile => django_project/Dockerfile (64%) rename entrypoint.sh => django_project/entrypoint.sh (79%) create mode 100644 django_project/requirements.txt delete mode 100644 requirements.txt diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f1251b0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,39 @@ +# Ignore Python bytecode files +**/__pycache__ +**/*.pyc +**/*.pyo + +# Ignore version control folders +.git +.gitignore + +# Ignore virtual environments +env/ +venv/ +ENV/ + +# Ignore distribution / packaging folders +build/ +dist/ +.eggs/ +*.egg-info/ +*.whl + +# Ignore documentation builds +docs/_build + +# Ignore Dockerfile and Docker-compose files (optional) +Dockerfile +docker-compose.yml + +# Ignore environment and secrets files + + +# Ignore logs and coverage reports +*.log +*.coverage +.coverage +.pytest_cache/ + +# Ignore Jupyter Notebook checkpoints +.ipynb_checkpoints diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..29de556 --- /dev/null +++ b/.gitignore @@ -0,0 +1,90 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +venv/ +ENV/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg +*.whl + +# PyInstaller +# Usually these files are written by a python script from a template +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ +.pytype/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# Jupyter Notebook +.ipynb_checkpoints + +# Pyre type checker +.pyre/ + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Environment variables file +.venv + +# IDE files +.vscode/ +.idea/ +*.sublime-project +*.sublime-workspace diff --git a/.env b/django_project/.env similarity index 100% rename from .env rename to django_project/.env diff --git a/Dockerfile b/django_project/Dockerfile similarity index 64% rename from Dockerfile rename to django_project/Dockerfile index 496de0c..2d0118f 100644 --- a/Dockerfile +++ b/django_project/Dockerfile @@ -1,13 +1,14 @@ -FROM python:3.8.5-alpine +FROM python:3.12-slim + -RUN pip install --upgrade pip -COPY ./requirements.txt . -RUN pip install -r requirements.txt -COPY ./django_project /app WORKDIR /app +COPY . . + +RUN pip install --upgrade pip +RUN pip install -r requirements.txt COPY ./entrypoint.sh / ENTRYPOINT ["sh", "/entrypoint.sh"] diff --git a/django_project/django_project/settings.py b/django_project/django_project/settings.py index e32c871..63479d4 100644 --- a/django_project/django_project/settings.py +++ b/django_project/django_project/settings.py @@ -11,6 +11,9 @@ """ import os +from dotenv import load_dotenv + +load_dotenv() # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -26,7 +29,7 @@ # SECURITY WARNING: don't run with debug turned on in production! #DEBUG = True -DEBUG = os.getenv('DEBUG') +DEBUG = os.getenv('SECRET_KEY') == 'True' ALLOWED_HOSTS = ['*'] @@ -122,3 +125,4 @@ STATIC_ROOT = '/static/' STATIC_URL = '/static/' +CSRF_TRUSTED_ORIGINS = ['http://localhost', 'http://127.0.0.1', 'https://yourdomain.com'] diff --git a/entrypoint.sh b/django_project/entrypoint.sh similarity index 79% rename from entrypoint.sh rename to django_project/entrypoint.sh index ebb902f..342be09 100644 --- a/entrypoint.sh +++ b/django_project/entrypoint.sh @@ -5,5 +5,5 @@ python manage.py collectstatic --no-input DJANGO_SUPERUSER_PASSWORD=$SUPER_USER_PASSWORD python manage.py createsuperuser --username $SUPER_USER_NAME --email $SUPER_USER_EMAIL --noinput -gunicorn django_project.wsgi:application --bind 0.0.0.0:8000 +python manage.py runserver 0.0.0.0:8000 diff --git a/django_project/requirements.txt b/django_project/requirements.txt new file mode 100644 index 0000000..f7d899d --- /dev/null +++ b/django_project/requirements.txt @@ -0,0 +1,4 @@ +asgiref==3.8.1 +Django==5.1.3 +python-dotenv==1.0.1 +sqlparse==0.5.1 diff --git a/docker-compose.yml b/docker-compose.yml index 71a0faf..f883edd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,24 +1,24 @@ -version: '3.7' - services: django_gunicorn: - volumes: - - static:/static - env_file: - - .env build: - context: . + context: ./django_project + command: python manage.py runserver 0.0.0.0:8000 ports: - "8000:8000" + volumes: + - static:/app/static + env_file: + - ./django_project/.env + + nginx: build: ./nginx - volumes: - - static:/static ports: - "80:80" + volumes: + - static:/static depends_on: - django_gunicorn volumes: static: - diff --git a/nginx/Dockerfile b/nginx/Dockerfile index 1339474..ac44c90 100644 --- a/nginx/Dockerfile +++ b/nginx/Dockerfile @@ -1,3 +1,5 @@ -FROM nginx:1.19.0-alpine +# Use the latest Alpine-based NGINX image or a specific version +FROM nginx:alpine +# Copy your custom configuration file into the NGINX configuration directory COPY ./default.conf /etc/nginx/conf.d/default.conf diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index dc5efc9..0000000 --- a/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -Django==3.0.8 -gunicorn==20.0.4 \ No newline at end of file From aa4645bbb138dd98e41ae6b269301f4f9f924550 Mon Sep 17 00:00:00 2001 From: Md Golam Mostofa <68312838+shuvo881@users.noreply.github.com> Date: Sun, 17 Nov 2024 20:49:29 +0600 Subject: [PATCH 2/3] for https --- docker-compose-https.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 docker-compose-https.yml diff --git a/docker-compose-https.yml b/docker-compose-https.yml new file mode 100644 index 0000000..568770b --- /dev/null +++ b/docker-compose-https.yml @@ -0,0 +1,29 @@ +services: + backend: + build: + context: . + dockerfile: Dockerfile + container_name: backend_service + command: uvicorn src.sapi:app --host 0.0.0.0 --port ${BACKEND_PORT} --reload + ports: + - "${BACKEND_PORT}:${BACKEND_PORT}" + env_file: + - .env + volumes: + - .:/app + restart: always + + nginx: + build: ./nginx + container_name: nginx_service + ports: + - "80:80" + - "443:443" + depends_on: + - backend + restart: always + env_file: + - .env + volumes: + - ./nginx/default.conf:/etc/nginx/conf.d/default.conf + - /etc/letsencrypt:/etc/letsencrypt # Mounting SSL certificates and configs From e93ff09b6fa640bf410f6037bd023933a98be84a Mon Sep 17 00:00:00 2001 From: Md Golam Mostofa <68312838+shuvo881@users.noreply.github.com> Date: Sun, 17 Nov 2024 20:50:02 +0600 Subject: [PATCH 3/3] for https --- nginx/default-https.conf | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 nginx/default-https.conf diff --git a/nginx/default-https.conf b/nginx/default-https.conf new file mode 100644 index 0000000..f16e1b4 --- /dev/null +++ b/nginx/default-https.conf @@ -0,0 +1,35 @@ +upstream backend_ai { + server backend:8000; +} + +server { + listen 80; + server_name test.serenus.one; + + # Redirect HTTP to HTTPS + location / { + return 301 https://$host$request_uri; + } +} + +server { + listen 443 ssl http2; # Enable HTTP/2 for better performance + server_name test.serenus.one; + + # SSL settings provided by Certbot + ssl_certificate /etc/letsencrypt/live/test.serenus.one/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/test.serenus.one/privkey.pem; + include /etc/letsencrypt/options-ssl-nginx.conf; + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; + + # Add HSTS header for additional security (enforces HTTPS) + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; + + location / { + proxy_pass http://backend_ai; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } +}