# Operations guide

URL: https://scalws.com/docs/operations/
Source: `docs/OPERATIONS.md` in the ScalWS repository

## Install

Supported systems (x86_64): AlmaLinux, Rocky Linux and Oracle Linux 8, 9 and 10 (for 10 see
the [EL10 note](https://scalws.com/docs/guide/installation/#el10)), Fedora 44, openSUSE Leap 16.0 and 15.6, SLES
16.0 and 15 SP7, Debian 12 and 13, Ubuntu 22.04, 24.04 and 26.04, Alpine Linux 3.24. The
per-system notes (SELinux, SUSE, Alpine and OpenRC, firewall, cgroup v1 on EL8, systemd
before 254) and the zypper and apk commands are in the
[installation guide](https://scalws.com/docs/guide/installation/).

From the ScalWS dnf repository (RHEL family, Fedora):

```sh
curl -o /etc/yum.repos.d/scalws.repo https://rpm.scalws.com/scalws.repo
dnf install scalws
```

From the ScalWS apt repository (Debian, Ubuntu):

```sh
FPR=0B49CE4488DC3A3E4F631218CBBBF99C69B99EF0
apt-get install -y curl gnupg
key=$(mktemp)
curl -fsSL https://apt.scalws.com/scalws-archive-keyring.gpg -o "$key"
# The file must hold exactly one primary key, the pinned one: stop if this says WRONG.
test "$(gpg --show-keys --with-colons "$key" \
    | awk -F: '$1=="pub"{p=1;next} p&&$1=="fpr"{print $10;p=0}')" = "$FPR" \
    && echo "repository key OK" || echo "WRONG repository key: stop here"
# Install only that key, re-exported from a scratch keyring (never the downloaded file).
export GNUPGHOME=$(mktemp -d)
gpg --quiet --import "$key"
gpg --export "$FPR" > /usr/share/keyrings/scalws-archive-keyring.gpg
rm -rf "$GNUPGHOME" "$key"; unset GNUPGHOME
echo 'deb [signed-by=/usr/share/keyrings/scalws-archive-keyring.gpg] https://apt.scalws.com stable main' \
    > /etc/apt/sources.list.d/scalws.list
apt update && apt install scalws
```

The repository is signed with the "ScalWS Archive Signing Key" (fingerprint
`0B49 CE44 88DC 3A3E 4F63  1218 CBBB F99C 69B9 9EF0`), the dnf repository with the same
key. Upgrades come with `apt upgrade`, `dnf upgrade scalws`, `zypper up scalws` or
`apk upgrade scalws`. ScalWS is not in the
distributions' official archives yet.

From a package file:

```sh
apt install ./scalws_<version>-<release>_amd64.deb   # Debian, Ubuntu
dnf install ./scalws-<version>-<release>.x86_64.rpm  # RHEL family, Fedora (glibc 2.28 or later)
```

The package installs `/usr/bin/scalwsd`, `/usr/bin/scalwsctl`, `/usr/bin/scalws-controller`,
`/usr/bin/scalws-console`, the systemd units, shell completions, the system user/group
`scalws` and `/etc/scalws/scalws.yaml` (ports 80 and 443, `0640 root:scalws`, kept on
upgrade; the complete list is in [Files, directories and services](https://scalws.com/docs/guide/files-and-services/)). The full reference configuration is in
`/usr/share/doc/scalws/examples/scalws.yaml`.

Two units start the server; enable one of them (they exclude each other):

* `scalws-controller` (default, Adaptive Runtime, docs/adaptive-runtime.md): starts and
  supervises the `scalwsd` instances (`/etc/scalws/controller.yaml`). The ScalWS Console
  manages the server through it: without the controller it shows "controller
  unavailable" and its actions are disabled. Managed PHP-FPM/Node.js/Python/container
  runtimes are refused under the controller (ADR-0036): use an existing PHP-FPM pool
  (`fastcgi:`) and proxy to applications you run. HTTP/3 is in the packages and off
  until a TLS listener sets `http3: true` (open UDP 443 yourself); under the controller it
  runs through the QUIC demux (docs/http3-multi-instance.md).
* `scalwsd.service`: one `scalwsd` process, for configurations the controller refuses.

A fresh installation enables and starts `scalws-controller` with the packaged
configuration (ports 80 and 443, Let's Encrypt, a welcome page for unknown hosts), unless
another program holds port 80 or 443 ([installation](https://scalws.com/docs/guide/installation/#first-start)).

```sh
scalwsctl check                       # validate + prepare exactly like a reload
scalwsctl site add example.com        # publish a site
usermod -aG scalws alice              # operators use scalwsctl via the admin socket
```

Packages for amd64 and arm64 (`ARCH=arm64`) are built with `scripts/package.sh` and verified with
`scripts/package.sh --verify` (clean Debian and Fedora containers: user, files,
permissions, configuration check, server start, `scalwsctl` over the socket, access denied
outside group `scalws`).

## Privileges

The unit runs as `scalws` with only `CAP_NET_BIND_SERVICE`, `Delegate=yes` for cgroup v2
(tenant limits) and a hardened sandbox. scalws never needs root; it logs a warning when
started as root.


Application processes never inherit scalws's capabilities (`setpriv --inh-caps=-all
--ambient-caps=-all --no-new-privs`) and receive SIGTERM if scalwsd dies, even when it is
killed outside systemd (`--pdeathsig`); processes left in cgroup leaves by a crashed
instance are killed at the next start. Under SELinux (the rpm's policy) the switch to the
applications' domain `scalws_app_t` is a secure exec, which clears the death signal: there
`scalwsd.service`'s `KillMode=control-group` stops the applications when scalwsd dies.

### Kubernetes

In Kubernetes, scale with replicas (and a HorizontalPodAutoscaler) instead of
`scalws-controller`; see [the Kubernetes guide](https://scalws.com/docs/guide/kubernetes/). scalwsd under the
controller ignores `server.health.address`, `server.reload.watch` and
`limits.shutdown_delay`.

### Tenant users (ADR-0026, optional)

`tenants[].user: web1` runs that tenant's PHP-FPM, Node.js and Python processes as the
Unix account `web1` (not root; container applications are refused for such tenants).
scalwsd then needs four more capabilities: this makes it root-equivalent if
compromised, the same trade-off as a root PHP-FPM master:

```ini
# /etc/systemd/system/scalwsd.service.d/tenant-users.conf
[Service]
AmbientCapabilities=CAP_NET_BIND_SERVICE CAP_SETUID CAP_SETGID CAP_CHOWN CAP_KILL
CapabilityBoundingSet=CAP_NET_BIND_SERVICE CAP_SETUID CAP_SETGID CAP_CHOWN CAP_KILL
```

Each such tenant gets its own socket directory (`<runtime_dir>/t/<hash>`, owner
`web1:scalws`, mode `0770`, with a default ACL that gives group `scalws` access to the
tenant's sockets); other tenants cannot list it. No setgid bit is needed, so the unit's
`--restrict-suid-sgid` stays (only on a runtime directory without POSIX ACLs does scalwsd
fall back to mode `2770`, which it refuses: then add `Environment=SCALWS_RESTRICT_SUID_SGID=false`
to the drop-in, for `scalwsd.service` and `scalws-controller.service`; a drop-in with
`RestrictSUIDSGID=false` no longer has any effect, since the units do not set it). Document roots must be readable by
the tenant (PHP, Node, Python) and by group `scalws` (static files served by scalwsd), e.g.
`chown -R web1:scalws /srv/web1 && chmod -R g+rX,o-rwx /srv/web1`. Changing a tenant's user
restarts its applications on reload.

A tenant who owns the directories on the way to its document root can replace the root by a
symbolic link. Set `root_symlinks: refuse` on such applications (panel mode does): the root
is then opened without following links, and a link there makes that application answer 503.
With the default `follow`, scalwsd still refuses a root whose link leads into, or over,
ScalWS's own directories (`/etc/scalws`, `/var/lib/scalws*`, `/run/scalws*`,
`/var/cache/scalws`, the ACME state, runtime and disk cache directories of the configuration
and scalws-controller's state) or the directories of the TLS certificates and keys the
configuration names (listener certificates, `server.admin.remote`; also where their links
lead, such as certbot's `archive/`): the serving process can read those keys.

## Admin API and scalwsctl (ADR-0017)

`/run/scalws/admin.sock` (mode `0660 scalws:scalws`, directory `0750`) accepts everything; every
change is logged under `scalws::audit` with the caller's uid/gid/pid. The loopback metrics
listener (`127.0.0.1:9900` in the default configuration) serves `/metrics`, `/healthz`,
read-only views and a read-only dashboard at `/dashboard`.

| Task | Command |
|---|---|
| Overview | `scalwsctl status`, `scalwsctl apps`, `scalwsctl tenants`, `scalwsctl top` |
| Apply the edited file | `scalwsctl reload` (or `systemctl reload scalwsd`) |
| What changed | `scalwsctl history`, `scalwsctl diff [--from G --to H]` |
| Undo | `scalwsctl rollback [--generation G]` |
| Restart one application (no dropped requests) | `scalwsctl restart tenant/app` |
| Worker count of a Node.js pool | `scalwsctl scale tenant/app N` |
| Cache | `scalwsctl purge --host H --path /p` · `--prefix /p` · `--tag T` · `--all` |
| Problems | `scalwsctl diagnose [tenant/app]` (exit 2 on critical findings) |
| Optimizer | `scalwsctl recommendations` |
| AI explanation (if enabled) | `scalwsctl explain tenant/app [--refresh]` |
| Detect a framework | `scalwsctl detect /srv/app` |

A reload that changes listeners, the metrics/admin endpoints, connection limits, cgroups,
cache memory, per-IP limits or the AI advisor is refused with "requires a restart":
`systemctl restart scalwsd`.

## Tenant resource limits (ADR-0010)

With cgroups enabled (`server.cgroups`), the busiest tenants and their limits:

```sh
scalws-highcpu            # CPU, percent of one CPU, over one second
scalws-highram            # memory in use (and share of the host)
scalws-highio             # disk read/write MiB/s and I/O pressure
scalws-highcpu -n 5 --json
```

Limit a tenant, named by its tenant name or its Unix user (`tenants[].user`):

```sh
scalws-limitcpu ali 50%          # half a CPU (200% = two CPUs)
scalws-limitram ali 30%          # 30% of the host's memory, or a size: 2GiB
scalws-limitio  ali 50%          # half a default tenant's share of disk time
scalws-limitio  ali 20MiB/s      # at most 20 MiB/s of reads and 20 MiB/s of writes
scalws-limitcpu ali --clear      # back to the configuration
scalws-limitram ali 2GiB --persist
```

A limit applies at once and lasts until scalwsd restarts (under scalws-controller, until
its instances are replaced); it survives reloads. `--persist` also writes it to the
tenant's `resources` in `scalws.yaml` (comments are kept; the previous file stays as
`scalws.yaml.bak.<time>`) and reloads. These are `scalwsctl limit cpu|ram|io` and
`scalwsctl high cpu|ram|io` under other names; they use the admin socket, so they run as
root or a member of the `scalws` group.

## Remote administration (optional)

```yaml
server:
  admin:
    remote: {address: "0.0.0.0:9443", cert: /etc/scalws/admin.pem, key: /etc/scalws/admin.key,
             client_ca: /etc/scalws/operators-ca.pem}
```

Only clients with a certificate issued by `client_ca` can connect (mutual TLS):
`scalwsctl --remote host:9443 --ca server-ca.pem --client-cert alice.pem --client-key alice.key status`.

## Certificates (ACME)

```yaml
server:
  listeners:
    - {name: http, address: "0.0.0.0:80"}                 # HTTP-01 challenges
    - {name: https, address: "0.0.0.0:443", tls: {acme: true}}
  acme:
    enabled: true
    contact: ["mailto:ops@example.com"]
    terms_of_service_agreed: true
tenants:
  - name: acme
    applications:
      - {name: shop, domains: [shop.example.com, www.shop.example.com], acme: true, ...}
```

`scalwsctl certs` shows each application's names, days left and errors. Renewal starts 30
days before expiry (`renew_before`) and is activated without a restart. Try the
configuration against the Let's Encrypt staging directory first
(`directory: https://acme-staging-v02.api.letsencrypt.org/directory`).

## Containers

`runtime: {type: container}` runs images with podman (default) or docker; install
`podman` and `catatonit` on the host. With cgroup enforcement and podman, containers are
created under the tenant's cgroup, so tenant limits include them. `scalwsctl scale` and
`scalwsctl restart` work as for Node.js pools.

## Performance settings

* `server.event_loops` (ADR-0029): unset = one event loop per CPU scalwsd may run on
  (honours `taskset`/cgroup CPU limits), `0` = one shared work-stealing runtime.
  `scalwsd --worker-threads N` sets the count.
* `server.open_file_cache` (ADR-0028): `{max_entries: 10000}` by default, every hit is
  checked against the file; `valid: 60s` trusts cached files for that long (like nginx's
  `open_file_cache_valid`), `max_entries: 0` turns it off.
* `server.sendfile` (ADR-0027, default on): large static files on plain HTTP/1.
* `server.ktls: true` (ADR-0030): kernel TLS for HTTPS/1.1 so large files use `sendfile`
  over TLS too. Load the module first (`modprobe tls`, persist it in
  `/etc/modules-load.d/tls.conf`); without it scalwsd logs a warning and keeps TLS in
  user space.
* `server.http1_server: own | hyper` (ADR-0046, default `own`; applies to connections
  accepted after a reload): `own` serves HTTP/1.1 with scalws's own server (strict framing,
  differential-tested against hyper, fuzzed, audited); `hyper` is the fallback.
* `server.upstream_client: own | hyper` (ADR-0045, default `own`, read at start-up) and,
  per proxy route, `client:` overriding it: the HTTP/1 client to upstreams and to managed
  Node/Python applications. `hyper` is the fallback here too.
* Release binaries are profile-guided (ADR-0047): `tools/pgo-build.sh` builds, trains and
  rebuilds `scalwsd` (`PGO_BOLT=1` adds a BOLT pass on hosts with last-branch records).
  A plain `cargo build --release` works too, only slower.

## .htaccess (ADR-0031)

`applications[].htaccess: true` reads `.htaccess` files from the application's `root`
down to the requested directory (mod_rewrite, Redirect/RedirectMatch, Require/Order/
Allow/Deny, `<Files>`/`<FilesMatch>`, ErrorDocument, DirectoryIndex, Header, Expires).
Edits apply within 2 seconds. Unsupported or refused lines (`php_value`, `[P]` proxy
rules, `SetHandler`, look-around regexes) are skipped and logged with the file name
(`journalctl -u scalwsd | grep htaccess`).

## Backup and restore

```sh
scalwsctl backup /var/backups/scalws-$(date +%F)     # config + certificates/keys + manifest
scalwsctl restore /var/backups/scalws-2026-10-05     # dry run: what would change
scalwsctl restore /var/backups/scalws-2026-10-05 --apply && scalwsctl reload
```

Restore keeps every replaced file as `<file>.bak.<unix time>` and validates the restored
configuration. Document roots and application data are not part of the backup.

## Upgrade

Install the new package; a running service is restarted by the package scripts. The
configuration file is never replaced. Check `scalwsctl status` for the version.

`scalwsd.service` restarts with a short gap. Under scalws-controller the restarted controller
adopts the running instances and rolls them onto the new binary one at a time, a new instance
active before an old one drains, without reading the configuration file again. It needs one
free slot below `instances.max`; when every slot is taken it only logs a warning.
`instances.roll_on_upgrade: false` in `controller.yaml` turns this off. In both cases move
the instances with `scalws-controller reload`. `scalws-controller status` shows
`"binary": "replaced"` for an instance still on the old file.
