Skip to content

Commit

Permalink
Merge pull request #6 from Flagro/nginx_production
Browse files Browse the repository at this point in the history
Nginx production
  • Loading branch information
Flagro authored Nov 14, 2023
2 parents 2534e5e + 31c90ce commit f51bf5b
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .env-example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DEBUG=True
ENVIRONMENT=development
SECRET_KEY="YOUR DJANGO KEY"
DB_NAME=postgres
DB_USER=postgres
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ RUN pip install pipenv && pipenv install --system

# Copy project
COPY . /code/

# Collect static files
RUN python manage.py collectstatic --noinput
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ typing-extensions = "*"
psycopg2-binary = "*"
django-oauth-toolkit = "*"
django-cors-headers = "*"
pip = "*"
gunicorn = "*"

[dev-packages]

Expand Down
36 changes: 31 additions & 5 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions conf/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
server {
listen 80;
server_name api.phrinifluent.com;

location /static/ {
alias /code/staticfiles/;
}

location / {
proxy_pass http://web:8000;
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;
}

# SSL configuration
listen 443 ssl;
ssl_certificate /etc/ssl/certs/phrinifluent.crt;
ssl_certificate_key /etc/ssl/certs/phrinifluent.key;

# Further SSL options and optimizations can be added here
}
29 changes: 25 additions & 4 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') # Nginx will serve from here in production

# Add MEDIA_URL and MEDIA_ROOT if you have media files
# MEDIA_URL = '/media/'
# MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
Expand All @@ -24,11 +31,25 @@
SECRET_KEY = config('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config('DEBUG', default=False, cast=bool)
# Environment (Development or Production)
ENVIRONMENT = config('ENVIRONMENT', default='development')

ALLOWED_HOSTS = ['0.0.0.0', 'localhost']

SECURE_SSL_REDIRECT = True
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = ENVIRONMENT == 'development'

# Allowed hosts
if ENVIRONMENT == 'production':
ALLOWED_HOSTS = ['api.phrinifluent.com']
else:
ALLOWED_HOSTS = ['localhost', '127.0.0.1']

# SSL Settings
if ENVIRONMENT == 'production':
SECURE_SSL_REDIRECT = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Other production specific settings...
else:
SECURE_SSL_REDIRECT = False

# Application definition

Expand Down
51 changes: 51 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: '3.8'

services:
web:
build: .
command: gunicorn core.wsgi:application --bind 0.0.0.0:8000
volumes:
- .:/code
- static_volume:/code/staticfiles
env_file:
- .env.prod
depends_on:
- db
networks:
- internal

nginx:
image: nginx:1.21
ports:
- "80:80"
- "443:443"
volumes:
- static_volume:/code/staticfiles
- ./conf/nginx.conf:/etc/nginx/conf.d/default.conf
- /etc/ssl/certs/phrinifluent.crt:/etc/ssl/certs/phrinifluent.crt:ro
- /etc/ssl/certs/phrinifluent.key:/etc/ssl/certs/phrinifluent.key:ro
depends_on:
- web
networks:
- internal

db:
image: postgres
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
networks:
- internal

# Volumes
volumes:
postgres_data:
static_volume:

# Networks
networks:
internal:
driver: bridge

0 comments on commit f51bf5b

Please sign in to comment.