diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..e3c244f80 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +# Dockerfile +FROM python:3.12 + +RUN apt-get update && apt-get install nginx --yes + +COPY requirements.txt requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +COPY deploy/prod-requirements.txt prod-requirements.txt +RUN pip install --no-cache-dir -r prod-requirements.txt + +COPY deploy/nginx.conf /etc/nginx/sites-enabled/default + +# Mounts the application code to the image +COPY . app +WORKDIR /app + +EXPOSE 8080 + +# runs the production server +CMD ["/app/deploy/entrypoint.sh"] diff --git a/README.md b/README.md index 3514b897d..0fdd0c2c5 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,32 @@ You can now visit your site at `http://localhost:8000/`. The dashboard and admin interface are available at `http://localhost:8000/dashboard/` and `http://localhost:8000/admin/`. Use the superuser created in step three of the commands above. + +### Database +The default setup of this project uses a SQLite database, which is persisted on the file system. If you want to use another +database you can change the `DATABASES` config option in the `settings.py` file. This file already contains an example +configuration on how to use the [Postgres](https://www.postgresql.org/) database specified in the docker-compose file. + +### Docker +You can also run the project using [Docker](https://www.docker.com/). To build the image and run the container you can run: +```bash +docker build -t django-wedding-website . +docker run -it -p 8080:8080 \ + -e DJANGO_SUPERUSER_PASSWORD=changeme \ + -e DJANGO_SUPERUSER_USERNAME=admin \ + -e DJANGO_SUPERUSER_EMAIL=admin@example.com \ + django-wedding-website +``` +You can now visit your site at `http://localhost:8080` + +#### Docker Compose +To run the project with a Postgres database, you can +- Change the `DATABASES` configuration in `settings.py` to use the Postgres database +- Start the Postgres Database and the project container with `docker-compose up --build` +- You can now visit your site at `http://localhost:8080` + +> Note that if you want to make a production deployment with Docker you need to backup the SQLite or Postgres database! + ## Customization I recommend forking this project and just manually modifying it by hand to replace everything with what you want. diff --git a/bigday/settings.py b/bigday/settings.py index 23794e2f5..1a05678b7 100644 --- a/bigday/settings.py +++ b/bigday/settings.py @@ -12,9 +12,14 @@ import os +import environ + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +# Initialise environment variables +env = environ.Env() +environ.Env.read_env(os.path.join(BASE_DIR, '.env')) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ @@ -23,7 +28,7 @@ # This is a default value and must be changed! # Rename "localsettings.py.template" to 'localsettings.py' and edit your settings. # To protect your credentials from leaking to your Git server we added 'localsettings.py' to the gitignore -SECRET_KEY = 'u7!-y4k1c6b44q507nr_l+c^12o7ur++cpzyn!$65w^!gum@h%' +SECRET_KEY = env('SECRET_KEY', default='u7!-y4k1c6b44q507nr_l+c^12o7ur++cpzyn!$65w^!gum@h%') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -92,7 +97,16 @@ 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), - } + }, + # if you want to use the postgres database just uncomment the following lines and comment out the sqlite3 lines + # 'default': { + # 'ENGINE': 'django.db.backends.postgresql', + # 'NAME': env('POSTGRES_DB'), + # 'USER': env('POSTGRES_USER'), + # 'PASSWORD': env('POSTGRES_PASSWORD'), + # 'HOST': env('POSTGRES_SERVER'), + # 'PORT': env('POSTGRES_PORT'), + # } } diff --git a/deploy/entrypoint.sh b/deploy/entrypoint.sh new file mode 100755 index 000000000..9e81735ea --- /dev/null +++ b/deploy/entrypoint.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +python manage.py collectstatic --noinput + +# i commit my migration files to git so i dont need to run it on server +# ./manage.py makemigrations app_name +python manage.py migrate + +# Create the superuser + +python manage.py createsuperuser --noinput + +/usr/sbin/nginx -g 'daemon off;' & + +gunicorn bigday.wsgi --bind 0.0.0.0:8000 \ No newline at end of file diff --git a/deploy/nginx.conf b/deploy/nginx.conf new file mode 100644 index 000000000..c77f3b20f --- /dev/null +++ b/deploy/nginx.conf @@ -0,0 +1,31 @@ +upstream app_server_djangoapp { + server localhost:8000 fail_timeout=0; +} + +server { + listen 8080; + server_name localhost; + + access_log /var/log/nginx/wedding-access.log; + error_log /var/log/nginx/wedding-error.log info; + + keepalive_timeout 5; + + root /app; + + location /static { + autoindex on; + alias /app/static_root; + } + + location / { + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_redirect off; + + if (!-f $request_filename) { + proxy_pass http://app_server_djangoapp; + break; + } + } +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..ff55f91ed --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,30 @@ +version: "3" +services: + # WARNING: If you want to use this docker-compose file in production, you need to backup your database! + postgres: + container_name: postgres-wedding + image: postgres + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=password + - POSTGRES_DB=wedding + ports: + - 5432:5432 + wedding: + container_name: wedding + build: . + environment: + - DEBUG=False + - SECRET_KEY=changeme + - POSTGRES_SERVER=postgres-wedding + - POSTGRES_USER=postgres + - POSTGRES_DB=wedding + - POSTGRES_PASSWORD=password + - POSTGRES_PORT=5432 + - DJANGO_SUPERUSER_PASSWORD=changeme + - DJANGO_SUPERUSER_USERNAME=admin + - DJANGO_SUPERUSER_EMAIL=admin@example.com + ports: + - 8080:8080 + depends_on: + - postgres diff --git a/requirements.txt b/requirements.txt index 2d6e84caf..c3040efaa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ django==4.2.* Fabric==1.10.2 psycopg2-binary==2.9.3 +django-environ==0.11.2