diff --git a/docs/development.md b/docs/development.md index 5cbddee..fc5b4ca 100644 --- a/docs/development.md +++ b/docs/development.md @@ -593,6 +593,71 @@ PHP-FPM configurations, and makes an HTTP request with the account's primary domain. A passing check confirms PHP execution through the generated socket. The container uses its own filesystem and loopback interface. +## Run the installed panel behind Apache + +Install the panel with the `server` extra to include Gunicorn. Use the +`mariadb` extra as well when the panel database runs on MariaDB. + +```sh +python -m pip install '.[server,mariadb]' +``` + +Set the panel hostname and enable HTTPS proxy mode. Use a persistent random +`COULOBRE_SECRET_KEY` for the panel, worker, and scheduler. Create the static +directory with write access for the user running `collectstatic` and read +access for Apache. + +```sh +export DJANGO_SETTINGS_MODULE=coulobre.settings +export COULOBRE_ALLOWED_HOSTS=panel.example.net +export COULOBRE_HTTPS_PROXY=1 +export COULOBRE_STATIC_ROOT=/var/www/coulobre-panel-static +python -m django check --deploy +python -m django migrate +python -m django collectstatic --noinput +``` + +HTTPS proxy mode enables HSTS for the panel hostname. Django reports `W005` +and `W021` for the optional subdomain and browser preload settings. + +Run Gunicorn as a dedicated unprivileged user. Give that user ownership of +`/run/coulobre-panel` with group `www-data` and mode `0750`. Run Gunicorn with +that directory's group so Apache workers can connect to its Unix socket. +Restrict socket access to the panel process and Apache workers, including when +the panel shares the server with hosted sites. + +```sh +gunicorn --bind unix:/run/coulobre-panel/panel.sock --umask 0o007 \ + --no-control-socket --forwarder-headers '' coulobre.wsgi:application +``` + +Enable the Apache `headers`, `proxy`, and `proxy_http` modules. Add these +directives inside the panel's HTTPS virtual host alongside the TLS certificate +configuration. Redirect HTTP to HTTPS in the panel's port 80 virtual host. +Apache sets the protocol header itself. Django uses it to recognize HTTPS +requests and verify the origin of form submissions. HTTPS proxy mode also +marks session and CSRF cookies as secure. +[Django HTTPS proxy settings](https://docs.djangoproject.com/en/5.2/ref/settings/#secure-proxy-ssl-header) + +```apache +ProxyRequests Off +ProxyPreserveHost On +RequestHeader unset X-Forwarded-Protocol +RequestHeader unset X-Forwarded-Ssl +RequestHeader set X-Forwarded-Proto "https" + +ProxyPass /static/ ! +Alias /static/ /var/www/coulobre-panel-static/ + + AllowOverride None + Options None + Require all granted + + +ProxyPass / "unix:/run/coulobre-panel/panel.sock|http://localhost/" +ProxyPassReverse / http://localhost/ +``` + ## MariaDB Create a dedicated MariaDB database and application user. Install the MariaDB or diff --git a/pyproject.toml b/pyproject.toml index 9f697a4..4238663 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,7 @@ coulobre-web-node = "coulobre.web_node:main" [project.optional-dependencies] dev = ["ruff>=0.11,<1"] mariadb = ["mysqlclient>=2.2.1,<3"] +server = ["gunicorn>=27.0.1,<28"] [tool.setuptools.package-data] "coulobre.hosting" = [ diff --git a/src/coulobre/settings.py b/src/coulobre/settings.py index aa19cac..c81ca83 100644 --- a/src/coulobre/settings.py +++ b/src/coulobre/settings.py @@ -12,11 +12,19 @@ def required_env(name): SECRET_KEY = required_env("COULOBRE_SECRET_KEY") -DEBUG = os.environ.get("COULOBRE_DEBUG") == "1" +HTTPS_PROXY = os.environ.get("COULOBRE_HTTPS_PROXY") == "1" +DEBUG = not HTTPS_PROXY and os.environ.get("COULOBRE_DEBUG") == "1" ALLOWED_HOSTS = os.environ.get( "COULOBRE_ALLOWED_HOSTS", "localhost,127.0.0.1,[::1]" ).split(",") +if HTTPS_PROXY: + SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") + SECURE_SSL_REDIRECT = True + SECURE_HSTS_SECONDS = 86400 + SESSION_COOKIE_SECURE = True + CSRF_COOKIE_SECURE = True + INSTALLED_APPS = [ "coulobre.admin.CoulobreAdminConfig", "django.contrib.auth", @@ -85,6 +93,8 @@ AUTH_PASSWORD_VALIDATORS = [ TIME_ZONE = "UTC" STATIC_URL = "/static/" +STATIC_ROOT = os.environ.get("COULOBRE_STATIC_ROOT") or None +WSGI_APPLICATION = "coulobre.wsgi.application" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" LOGIN_URL = "panel:login" LOGIN_REDIRECT_URL = "panel:accounts" diff --git a/src/coulobre/wsgi.py b/src/coulobre/wsgi.py new file mode 100644 index 0000000..52ba01e --- /dev/null +++ b/src/coulobre/wsgi.py @@ -0,0 +1,7 @@ +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "coulobre.settings") + +application = get_wsgi_application()