diff --git a/README.md b/README.md index 9ef27ed..6b44200 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ and resellers. The project is in early development. The web panel gives operators a workspace to manage hosting accounts, request Apache configuration previews, and follow their progress. See [Local development](docs/development.md) to run it. +See [Single-server deployment](docs/deployment.md) to install it with Apache, +MariaDB, and RabbitMQ. ## Planned scope diff --git a/deploy/apache/panel-origin.conf.example b/deploy/apache/panel-origin.conf.example new file mode 100644 index 0000000..f3f5439 --- /dev/null +++ b/deploy/apache/panel-origin.conf.example @@ -0,0 +1,14 @@ +Listen 192.168.10.20:8080 + + + ServerName panel.example.net + + RemoteIPHeader X-Forwarded-For + RemoteIPInternalProxy 192.168.10.10 + + + Require expr "%{CONN_REMOTE_ADDR} == '192.168.10.10'" + + + Include /etc/coulobre/panel-proxy.inc + diff --git a/deploy/apache/panel-proxy.inc b/deploy/apache/panel-proxy.inc new file mode 100644 index 0000000..ea06fc7 --- /dev/null +++ b/deploy/apache/panel-proxy.inc @@ -0,0 +1,18 @@ +ProxyRequests Off +ProxyPreserveHost On +ProxyAddHeaders Off +RequestHeader unset X-Forwarded-Protocol +RequestHeader unset X-Forwarded-Ssl +RequestHeader set X-Forwarded-Proto "https" +RequestHeader set X-Forwarded-For "expr=%{REMOTE_ADDR}" + +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/ diff --git a/deploy/apache/panel-tls.conf.example b/deploy/apache/panel-tls.conf.example new file mode 100644 index 0000000..f11377b --- /dev/null +++ b/deploy/apache/panel-tls.conf.example @@ -0,0 +1,13 @@ + + ServerName panel.example.net + Redirect permanent / https://panel.example.net/ + + + + ServerName panel.example.net + SSLEngine On + SSLCertificateFile /etc/ssl/coulobre-panel/fullchain.pem + SSLCertificateKeyFile /etc/ssl/coulobre-panel/privkey.pem + + Include /etc/coulobre/panel-proxy.inc + diff --git a/deploy/panel.env.example b/deploy/panel.env.example new file mode 100644 index 0000000..65e4928 --- /dev/null +++ b/deploy/panel.env.example @@ -0,0 +1,10 @@ +DJANGO_SETTINGS_MODULE=coulobre.settings +COULOBRE_ALLOWED_HOSTS=panel.example.net +COULOBRE_HTTPS_PROXY=1 +COULOBRE_STATIC_ROOT=/var/www/coulobre-panel-static +COULOBRE_SECRET_KEY= +COULOBRE_DB_NAME=coulobre_panel +COULOBRE_DB_USER=coulobre +COULOBRE_DB_PASSWORD= +COULOBRE_DB_HOST=/run/mysqld/mysqld.sock +COULOBRE_BROKER_URL=amqp://coulobre:@127.0.0.1:5672/coulobre diff --git a/deploy/systemd/coulobre-beat.service b/deploy/systemd/coulobre-beat.service new file mode 100644 index 0000000..e2bf391 --- /dev/null +++ b/deploy/systemd/coulobre-beat.service @@ -0,0 +1,24 @@ +[Unit] +Description=Coulobre Panel Celery scheduler +Wants=network-online.target +After=network-online.target rabbitmq-server.service + +[Service] +Type=exec +User=coulobre-panel +Group=coulobre-panel +EnvironmentFile=/etc/coulobre/panel.env +WorkingDirectory=/opt/coulobre +StateDirectory=coulobre-panel +StateDirectoryMode=0700 +UMask=0077 +ExecStart=/opt/coulobre/bin/celery -A coulobre beat --loglevel=INFO --schedule=/var/lib/coulobre-panel/beat-schedule --pidfile= +Restart=on-failure +RestartSec=5s +NoNewPrivileges=true +PrivateTmp=true +ProtectHome=true +ProtectSystem=strict + +[Install] +WantedBy=multi-user.target diff --git a/deploy/systemd/coulobre-panel.service b/deploy/systemd/coulobre-panel.service new file mode 100644 index 0000000..05302c2 --- /dev/null +++ b/deploy/systemd/coulobre-panel.service @@ -0,0 +1,25 @@ +[Unit] +Description=Coulobre Panel web service +Wants=network-online.target +After=network-online.target mariadb.service + +[Service] +Type=exec +User=coulobre-panel +Group=www-data +EnvironmentFile=/etc/coulobre/panel.env +WorkingDirectory=/opt/coulobre +RuntimeDirectory=coulobre-panel +RuntimeDirectoryMode=0750 +UMask=0007 +ExecStart=/opt/coulobre/bin/gunicorn --bind unix:/run/coulobre-panel/panel.sock --umask 0o007 --no-control-socket --forwarder-headers= coulobre.wsgi:application +KillMode=mixed +Restart=on-failure +RestartSec=5s +NoNewPrivileges=true +PrivateTmp=true +ProtectHome=true +ProtectSystem=strict + +[Install] +WantedBy=multi-user.target diff --git a/deploy/systemd/coulobre-worker.service b/deploy/systemd/coulobre-worker.service new file mode 100644 index 0000000..2d8373e --- /dev/null +++ b/deploy/systemd/coulobre-worker.service @@ -0,0 +1,23 @@ +[Unit] +Description=Coulobre Panel Celery worker +Wants=network-online.target +After=network-online.target mariadb.service rabbitmq-server.service + +[Service] +Type=exec +User=coulobre-panel +Group=coulobre-panel +EnvironmentFile=/etc/coulobre/panel.env +WorkingDirectory=/opt/coulobre +UMask=0077 +ExecStart=/opt/coulobre/bin/celery -A coulobre worker --loglevel=INFO --concurrency=2 +KillMode=mixed +Restart=on-failure +RestartSec=5s +NoNewPrivileges=true +PrivateTmp=true +ProtectHome=true +ProtectSystem=strict + +[Install] +WantedBy=multi-user.target diff --git a/docs/deployment.md b/docs/deployment.md new file mode 100644 index 0000000..23824cb --- /dev/null +++ b/docs/deployment.md @@ -0,0 +1,175 @@ +# Install Coulobre Panel on one server + +This setup runs the panel, its Celery worker and scheduler, MariaDB, RabbitMQ, +and Apache on one Ubuntu server. Use Ubuntu 24.04 or later. Apache serves the +panel and its static files and connects to Gunicorn over a Unix socket. + +## Install the package and configure local services + +Run these commands from a Coulobre checkout on the server. + +```sh +sudo apt install apache2 mariadb-server rabbitmq-server python3-venv python3-dev \ + build-essential pkg-config default-libmysqlclient-dev +sudo useradd --system --user-group --no-create-home --home-dir /nonexistent \ + --shell /usr/sbin/nologin coulobre-panel +sudo python3 -m venv /opt/coulobre +sudo /opt/coulobre/bin/pip install '.[server,mariadb]' + +sudo install -d -m 0750 /etc/coulobre +sudo install -d -o coulobre-panel -g coulobre-panel -m 0755 /var/www/coulobre-panel-static +if ! sudo test -e /etc/coulobre/panel.env; then + sudo install -m 0600 deploy/panel.env.example /etc/coulobre/panel.env +fi +``` + +Generate separate random values for the Django secret key, the database +password, and the broker password. Use this command for each value. + +```sh +python3 -c 'import secrets; print(secrets.token_urlsafe(48))' +``` + +Start MariaDB and RabbitMQ. In `sudo mariadb`, create the panel database and +login. Substitute the generated database password. + +```sh +sudo systemctl enable --now mariadb rabbitmq-server +``` + +```sql +CREATE DATABASE coulobre_panel CHARACTER SET utf8mb4; +CREATE USER 'coulobre'@'localhost' IDENTIFIED BY 'replace-with-database-password'; +GRANT ALL PRIVILEGES ON coulobre_panel.* TO 'coulobre'@'localhost'; +``` + +Create a dedicated RabbitMQ virtual host and login. Enter the generated broker +password when prompted by `rabbitmqctl add_user`. + +```sh +sudo rabbitmqctl add_vhost coulobre +sudo rabbitmqctl add_user coulobre +sudo rabbitmqctl set_permissions -p coulobre coulobre '.*' '.*' '.*' +``` + +Set `listeners.tcp.1 = 127.0.0.1:5672` in `/etc/rabbitmq/rabbitmq.conf` and +restart RabbitMQ to bind its AMQP listener to loopback. Keep MariaDB and +RabbitMQ management and cluster ports private. [RabbitMQ listener settings](https://www.rabbitmq.com/docs/networking#listeners) + +Edit `/etc/coulobre/panel.env` with `sudoedit`. Set `COULOBRE_ALLOWED_HOSTS` +to the hostname browsers will use, enter the Django secret and the database +password, and place the broker password after `coulobre:` in +`COULOBRE_BROKER_URL`. The sample connects to MariaDB through its local Unix +socket and uses the dedicated RabbitMQ virtual host. + +## Prepare the panel + +Run the Django management commands as the service user with the same +environment the systemd services will use. + +```sh +panel_manage() { + sudo systemd-run --quiet --wait --pipe --collect --uid=coulobre-panel \ + --property=EnvironmentFile=/etc/coulobre/panel.env \ + /opt/coulobre/bin/python -m django "$@" +} + +panel_manage check --deploy +panel_manage migrate --noinput +panel_manage collectstatic --noinput + +sudo systemd-run --quiet --pty --collect --uid=coulobre-panel \ + --property=EnvironmentFile=/etc/coulobre/panel.env \ + /opt/coulobre/bin/python -m django createsuperuser +``` + +HTTPS proxy mode enables HSTS for the panel hostname. Django reports `W005` +and `W021` for the optional subdomain and browser preload settings. + +## Configure Apache and DNS + +Install the shared Apache directives and enable the modules for your setup. +Replace the example hostname in the chosen virtual host with the hostname +from `COULOBRE_ALLOWED_HOSTS`. + +```sh +sudo install -m 0644 deploy/apache/panel-proxy.inc /etc/coulobre/panel-proxy.inc +sudo a2enmod headers proxy proxy_http +``` + +### Public server + +Publish A or AAAA records for the IPv4 and IPv6 addresses that can reach the +panel. Allow TCP ports 80 and 443 at the firewall. If the server is behind NAT, +use the router's public address in DNS and forward those ports to Apache. +Install the TLS virtual host, enable `mod_ssl`, and set the certificate and key +paths for the panel hostname. + +```sh +sudo a2enmod ssl +sudo install -m 0644 deploy/apache/panel-tls.conf.example \ + /etc/apache2/sites-available/coulobre-panel.conf +``` + +### LAN only + +Use the same TLS virtual host with a hostname that internal DNS resolves to the +server's private address. Use a certificate that clients on the LAN trust, such +as one issued by your internal CA. Limit access to the panel's HTTP and HTTPS +ports to your private network. On a host with public and private web traffic, +use the private address in the panel's virtual hosts in place of `*`. + +### External reverse proxy + +Point the public DNS records at the reverse proxy. Configure it to redirect +HTTP to HTTPS, preserve the requested hostname when forwarding, and replace +`X-Forwarded-For` with the client address. The origin example uses HTTP on a +private network. Use a TLS connection to Apache if the upstream connection +crosses an untrusted network. + +```sh +sudo a2enmod remoteip +sudo install -m 0644 deploy/apache/panel-origin.conf.example \ + /etc/apache2/sites-available/coulobre-panel.conf +``` + +Set `192.168.10.20` to the private listener address on the Apache host and +`192.168.10.10` to the network peer address of your reverse proxy. Use the +proxy address in both `RemoteIPInternalProxy` and the `CONN_REMOTE_ADDR` +access rule. Restrict the origin port to the proxy at the firewall too. The +rule checks the peer connection, so it still applies when Apache recognizes a +different visitor IP from the trusted proxy. +[Apache client IP handling](https://httpd.apache.org/docs/2.4/mod/mod_remoteip.html) + +Each virtual host sends its HTTPS indicator to Gunicorn itself. The TLS +virtual host handles HTTPS at Apache, while the private origin is reserved +for the external proxy's HTTPS traffic. Apache passes a single visitor IP +based on the connection or a trusted proxy. [Django HTTPS proxy settings](https://docs.djangoproject.com/en/5.2/ref/settings/#secure-proxy-ssl-header) + +The addresses for hosted sites, mail, and authoritative DNS can differ from +the panel hostname. Configure the corresponding public DNS records for each +service using its reachable address. + +## Start the services + +Install and enable the three units. The panel's socket directory is available +to Apache workers. The scheduler stores its state in a private directory and +there is one scheduler for the installation. + +```sh +sudo install -m 0644 deploy/systemd/*.service /etc/systemd/system/ +sudo systemctl daemon-reload +sudo systemctl enable --now coulobre-panel.service coulobre-worker.service coulobre-beat.service + +sudo a2ensite coulobre-panel +sudo apache2ctl configtest +sudo systemctl reload apache2 +``` + +Sign in with the administrator account. Add a hosting account and request an +Apache preview to verify that RabbitMQ, the scheduler, and the worker are +connected. Follow their logs with `journalctl`. + +```sh +sudo journalctl -u coulobre-panel -u coulobre-worker -u coulobre-beat +``` diff --git a/docs/development.md b/docs/development.md index fc5b4ca..794eac5 100644 --- a/docs/development.md +++ b/docs/development.md @@ -595,68 +595,8 @@ 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/ -``` +See [Single-server deployment](deployment.md) for Gunicorn, Apache, and the +systemd services for the panel, worker, and scheduler. ## MariaDB