diff --git a/MasterProject/Dockerfile b/MasterProject/Dockerfile index ea7b8b1..22614ec 100644 --- a/MasterProject/Dockerfile +++ b/MasterProject/Dockerfile @@ -15,7 +15,7 @@ RUN apt-get update && \ COPY . /django -RUN python manage.py collectstatic --noinput +RUN python manage.py collectstatic --noinput --clear RUN chmod +x docker-entrypoint.sh diff --git a/MasterProject/MasterProject/settings.py b/MasterProject/MasterProject/settings.py index 67a2a06..a555d95 100644 --- a/MasterProject/MasterProject/settings.py +++ b/MasterProject/MasterProject/settings.py @@ -30,7 +30,16 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = os.environ.get('DEBUG') -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = os.environ.get( + 'ALLOWED_HOSTS_DJANGO', + 'django,localhost,127.0.0.1,0.0.0.0,[::1]' +).split(',') + + +# Configurations for nginx implementation + +SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') +CSRF_TRUSTED_ORIGINS = ('localhost') # Application definition diff --git a/MasterProject/docker-compose.yml b/MasterProject/docker-compose.yml index 62d6b4f..dcdbc47 100644 --- a/MasterProject/docker-compose.yml +++ b/MasterProject/docker-compose.yml @@ -12,6 +12,8 @@ services: ports: - ${DJANGO_PROD_PORT}:${DJANGO_EXPOSE_PORT} container_name: snapspeak_server + volumes: + - staticfiles_vol:/django/staticfiles command: sh -c "/django/docker-entrypoint.sh" mongodb: @@ -37,6 +39,22 @@ services: - .env container_name: snapspeak_celery_worker command: celery -A MasterProject worker --loglevel=info + + nginx: + depends_on: + - app + build: + context: ./nginx + dockerfile: Dockerfile + image: snapspeak-nginx:v1 + container_name: snapspeak_nginx + env_file: + - .env + ports: + - ${NGINX_PROD_PORT}:${NGINX_EXPOSE_PORT} + volumes: + - staticfiles_vol:/django/staticfiles volumes: - mongo_data: \ No newline at end of file + mongo_data: + staticfiles_vol: diff --git a/MasterProject/nginx/Dockerfile b/MasterProject/nginx/Dockerfile new file mode 100644 index 0000000..2560b1e --- /dev/null +++ b/MasterProject/nginx/Dockerfile @@ -0,0 +1,7 @@ +FROM nginx:1.25-alpine + +RUN mkdir -p /etc/nginx/templates + +COPY ./default.conf.template /etc/nginx/templates + +CMD ["/bin/sh", "-c","envsubst '${DJANGO_EXPOSE_PORT} ${NGINX_EXPOSE_PORT}' < /etc/nginx/templates/default.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"] \ No newline at end of file diff --git a/MasterProject/nginx/default.conf.template b/MasterProject/nginx/default.conf.template new file mode 100644 index 0000000..aedc9b8 --- /dev/null +++ b/MasterProject/nginx/default.conf.template @@ -0,0 +1,18 @@ +upstream django { + server app:${DJANGO_EXPOSE_PORT}; +} + +server { + listen ${NGINX_EXPOSE_PORT}; + + location / { + proxy_pass http://django; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_redirect off; + } + + location /static/ { + alias /django/staticfiles/; + } +} diff --git a/README.md b/README.md index 377a720..970af7b 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,9 @@ SnapSpeak can also be deployed using Docker for easier setup and portability. Fo REDIS_EXPOSE_PORT=6379 CELERY_BROKER_URL = 'redis://redis:6379/0' + + NGINX_EXPOSE_PORT=80 + NGINX_PROD_PORT=80 ``` Replace `` with a securely generated secret key for your Django application. You can use online tools or Django's `django.core.management.utils.get_random_secret_key()` method to generate a new key. Make sure to keep this key confidential and never share it publicly. @@ -152,7 +155,7 @@ SnapSpeak can also be deployed using Docker for easier setup and portability. Fo This command will build the Docker image and start the SnapSpeak application along with its dependencies. -6. Access the SnapSpeak application by visiting [http://localhost:8001/](http://localhost:8001/) in your web browser. +6. Access the SnapSpeak application by visiting [http://localhost:80](http://localhost:80/) or [http://localhost](http://localhost/) in your web browser. The application should now be running in a Docker container, providing a convenient and isolated environment for SnapSpeak. @@ -164,4 +167,4 @@ SnapSpeak can also be deployed using Docker for easier setup and portability. Fo This will stop and remove the containers. -**Note**: Ensure that port 8001 on your local machine is available and not occupied by another service before running Docker Compose. Adjust the `docker-compose.yml` file if you need to change the port configuration. +**Note**: Ensure that ports 8001 & 80 on your local machine are available and not occupied by another service before running Docker Compose. Adjust the `docker-compose.yml` file if you need to change the port configuration.