diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 8d29760..1535089 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -36,6 +36,11 @@ jobs: echo "DJANGO_ALLOWED_HOSTS=${{ secrets.DJANGO_ALLOWED_HOSTS }}" >> docker.env cat docker.env + - name: Save SSL Certificates + run: | + echo "${{ secrets.SSL_CERTIFICATE }}" > cert.pem + echo "${{ secrets.SSL_PRIVATE_KEY }}" > key.pem + - name: Build and Run with Docker Compose run: | docker compose --env-file docker.env -f docker-compose-build.yml up -d --build @@ -43,7 +48,8 @@ jobs: - name: Health Check run: | sleep 10 # Give the containers some time to start - curl --fail http://localhost:8000/ || (docker compose -f docker-compose-build.yml logs web && docker compose -f docker-compose-build.yml exec web cat /app/vis_phewas/debug.log && exit 1) + curl --fail https://localhost || (docker compose -f docker-compose-build.yml logs nginx && \ + docker compose -f docker-compose-build.yml exec web cat /app/vis_phewas/debug.log && exit 1) - name: Run Integration Tests run: | diff --git a/Dockerfile.django b/Dockerfile.django index 7cee399..223f5b4 100644 --- a/Dockerfile.django +++ b/Dockerfile.django @@ -31,6 +31,8 @@ RUN pip install --no-cache-dir -r requirements.txt # Copy only necessary files COPY ./vis_phewas /app/vis_phewas COPY ./LICENSE /app/LICENSE +COPY ./cert.pem /app/cert.pem +COPY ./key.pem /app/key.pem # Change directory to where manage.py is located WORKDIR /app/vis_phewas @@ -52,4 +54,5 @@ RUN python manage.py collectstatic --noinput --verbosity 3 ENV STATIC_ROOT /app/vis_phewas/staticfiles # Gunicorn setup -CMD ["gunicorn", "--bind", "0.0.0.0:8000", "vis_phewas.wsgi:application"] +CMD ["gunicorn", "--certfile=/app/cert.pem", "--keyfile=/app/key.pem", "--bind", "0.0.0.0:443", "vis_phewas.wsgi:application"] + diff --git a/docker-compose-build.yml b/docker-compose-build.yml index b369c91..c88a020 100644 --- a/docker-compose-build.yml +++ b/docker-compose-build.yml @@ -1,4 +1,3 @@ -# docker-compose-build.yml services: db: build: @@ -32,5 +31,17 @@ services: depends_on: - db + nginx: + image: nginx:latest + ports: + - "443:443" + - "80:80" + volumes: + - ./nginx.conf:/etc/nginx/conf.d/default.conf + - ./cert.pem:/etc/nginx/ssl/cert.pem + - ./key.pem:/etc/nginx/ssl/key.pem + depends_on: + - web + volumes: postgres_data: diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..e4ee5e1 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,29 @@ +server { + listen 443 ssl; + server_name _; # Replace with your domain name or IP address + + ssl_certificate /etc/nginx/ssl/cert.pem; + ssl_certificate_key /etc/nginx/ssl/key.pem; + + location / { + proxy_pass http://web:8000; # Proxy to the Django service + 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; + } + + location /static/ { + alias /app/vis-phewas/staticfiles/; # Adjust the path to your static files + } + +} + +server { + listen 80; + server_name _; # Replace with your domain name or IP address + + location / { + return 301 https://$host$request_uri; + } +} diff --git a/vis_phewas/vis_phewas/settings.py b/vis_phewas/vis_phewas/settings.py index 6e51361..2d833ba 100644 --- a/vis_phewas/vis_phewas/settings.py +++ b/vis_phewas/vis_phewas/settings.py @@ -60,7 +60,7 @@ MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', - 'whitenoise.middleware.WhiteNoiseMiddleware', # WhiteNoise Middleware for serving static files + # 'whitenoise.middleware.WhiteNoiseMiddleware', # WhiteNoise Middleware for serving static files 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', @@ -161,7 +161,7 @@ STATIC_ROOT = BASE_DIR / 'staticfiles' # WhiteNoise configuration for compressing static files -STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' +# STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' # Default primary key field type # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field