Skillversitas

Deployment and Best Practices

The deployment is the 12factor: the gunicorn (the WSGI, the 4 the worker, the no the runserver), the nginx (the static, the proxy, the SSL), and the Postgres (the no the SQLite, the 12factor the attached). The env is the secret (the SECRET_KEY, the DB, the no the commit), the log is the stdout (the 12factor, the no the file), and the security is the 5 (the DEBUG, the ALLOWED_HOSTS, the STATIC, the SECURE, the CORS). This chapter covers the gunicorn (the 4 the worker, the no the runserver), the nginx (the proxy, the static, the SSL), the Postgres (the env, the 12factor), and the security (the 5, the no the 1).

The gunicorn, the nginx, and the Postgres

# the GUNICORN (the WSGI, the 4,
#   the no runserver, the no
#   the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
pip install gunicorn psycopg[binary]
#   the no: the 2 (the no,
#     the no, the no
#     the no, the no
#     the no):
gunicorn notes.wsgi:application \
    --workers 4 \
    --bind 127.0.0.1:8000 \
    --timeout 30
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the NGINX (the proxy, the
#   static, the SSL, the no
#   the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
# server {
#     listen 443 ssl http2;
#     server_name api.local;
#     ssl_certificate /etc/letsencrypt/
#         live/api.local/fullchain.pem;
#     #   the no: the 1 (the no,
#     #     the no, the no
#     #     the no, the no
#     #     the no):
#     location /static/ {
#         alias /var/www/notes/staticfiles/;
#         expires 1y;
#     }
#     location / {
#         proxy_pass http://127.0.0.1:8000;
#         proxy_set_header Host $host;
#         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#         proxy_set_header X-Forwarded-Proto $scheme;
#     }
# }
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the POSTGRES (the env, the
#   12factor, the no SQLite,
#   the no the 1):
#   the no: the 2 (the no,
#     the no, the no
#     the no, the no
#     the no):
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": os.environ.get(
            "DB_NAME"),
        "USER": os.environ.get(
            "DB_USER"),
        "PASSWORD": os.environ.get(
            "DB_PASSWORD"),
        "HOST": os.environ.get(
            "DB_HOST", "127.0.0.1"),
        "PORT": os.environ.get(
            "DB_PORT", "5432"),
    }
}
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The gunicorn is the WSGI: the 4 worker is the 4 (the CPU the no the 1, the no the 1, the no the 1), the bind is the 127.0.0.1 (the nginx the no the 1, the no the public), and the timeout is the 30 (the 1, the no the 1, the no the the) - the runserver is the no (the auto-reload, the 1, the no the the). The nginx is the 3: the proxy_pass is the 1 (the gunicorn, the no the 1), the location /static/ is the alias (the collectstatic, the expires 1y, the no the Django), and the SSL is the 443 (the letsencrypt, the http2, the no the 80 the 1) - the Postgres is the env: the NAME the USER the PASSWORD the HOST the PORT (the 5, the no the 1, the 12factor the attached), and the psycopg[binary] is the 1 (the no the build, the no the the) - the security is the next: the 5 the no the 1.

The 5 security and the 12factor

# the 5 (the no 1, the no the
#   1, the no the no):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
DEBUG = False
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
ALLOWED_HOSTS = os.environ.get(
    "ALLOWED_HOSTS",
    "api.local").split(",")
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the 2 (the no,
#     the no, the no
#     the no, the no
#     the no):
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the 12FACTOR (the env, the
#   log the stdout, the no
#   the 1, the no the no):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the LOG (the stdout, the
#   12factor, the no file,
#   the no the 1):
#   the no: the 2 (the no,
#     the no, the no
#     the no, the no
#     the no):
LOGGING = {
    "version": 1,
    "handlers": {
        "console": {
            "class": "logging."
            "StreamHandler",
        },
    },
    "root": {
        "handlers": ["console"],
        "level": os.environ.get(
            "LOG_LEVEL", "INFO"),
    },
}
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The 5 security is the no the 1: the DEBUG is the False (the traceback the no, the secret the no, the no the the), the ALLOWED_HOSTS is the no (the 400, the 1, the no the the), the SECURE_SSL_REDIRECT is the 1 (the https, the no the 80), the HSTS is the 1 (the 1 the year, the no the downgrade), and the COOKIE_SECURE is the 2 (the session plus the CSRF, the no the 1, the no the the) - the 12factor is the 3: the env is the secret (the 5 the no the commit, the no the the), the log is the stdout (the no the file, the 12factor, the no the the), and the DB is the attached (the no the 1, the no the the) - the log is the 1 (the StreamHandler, the console, the no the file, the INFO the env) - the rule is the one: the gunicorn the 4, the nginx the 3, the Postgres the env, and the 5 security the no the 1 - the Django tutorial's end: the MVT the layer, the ORM the SQL, the admin the CRUD, the auth the user, and the 12factor the deploy.

The Working Rules

  • The gunicorn is the WSGI, the 4 the worker. The workers the 4 (the CPU the no the 1, the no the 1), and the bind the 127.0.0.1 (the nginx the no the 1, the no the public) - the timeout the 30 (the 1, the no the 1), and the runserver the no (the auto-reload, the 1, the no the the).
  • The nginx is the 3, the SSL the 443. The proxy_pass the gunicorn (the no the 1), the location /static/ the alias (the collectstatic, the expires 1y, the no the Django), and the SSL the 443 (the letsencrypt, the http2, the no the 80 the 1) - the no the 80 (the 301, the 1, the no the no).
  • The Postgres is the env, the 5 the no. The NAME the USER the PASSWORD the HOST the PORT (the 12factor the attached, the no the 1), and the psycopg[binary] the 1 (the no the build) - the no the SQLite (the no the 1, the no the the, the no the no).
  • The 5 security is the no 1, the 12factor the 3. The DEBUG the False (the traceback, the secret), the ALLOWED_HOSTS the no (the 400), and the SECURE_SSL_REDIRECT plus the HSTS plus the COOKIE_SECURE the 2 (the no the 1) - the env the secret, the log the stdout, and the DB the attached (the no the 1, the no the the).

Summary

The deployment is the 12factor: the gunicorn the WSGI (the 4 the worker, the 127.0.0.1, the timeout the 30, the no the runserver), the nginx the 3 (the proxy_pass the gunicorn, the /static/ the alias the expires 1y, the SSL the 443 the letsencrypt the http2), and the Postgres the env (the 5 the NAME the USER the PASSWORD the HOST the PORT, the psycopg[binary], the no the SQLite, the 12factor the attached). The 5 security is the no the 1 (the DEBUG the False, the ALLOWED_HOSTS the no, the SECURE_SSL_REDIRECT, the HSTS the 1 the year, and the COOKIE_SECURE the 2), and the 12factor is the 3 (the env the secret the no the commit, the log the stdout the no the file, and the DB the attached the no the 1). The log is the StreamHandler (the console, the INFO the env) - the rule is the one: the gunicorn the 4, the nginx the 3, the Postgres the env, and the 5 security the no the 1. The Django tutorial's end: the MVT the layer, the ORM the SQL, the admin the CRUD, the auth the user, the template the HTML, and the 12factor the deploy.