- Zig 95.5%
- Shell 4.2%
- Dockerfile 0.3%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| doc | ||
| scripts | ||
| src | ||
| test | ||
| .gitignore | ||
| build.zig | ||
| build.zig.zon | ||
| CLAUDE.md | ||
| README.md | ||
apache-sites
A terminal UI for managing Apache web site configurations on Ubuntu. Sites are kept in small TOML files under /etc/apache-sites. They are rendered into Apache config and installed only after Apache's own configtest accepts them.
See doc/requirements.md for the requirements this implements.
This application was built with the help of an LLM agent.
Building
Requires Zig 0.17.1.
zig build # debug build -> zig-out/bin/apache-sites
zig build test # unit tests
zig build release # static binaries:
# zig-out/release/linux-amd64/apache-sites
# zig-out/release/linux-aarch64/apache-sites
test/docker/run.sh # end-to-end tests against real Apache + PHP-FPM in Docker
Installing
sudo install -m 755 zig-out/release/linux-amd64/apache-sites /usr/local/bin/
sudo apache-sites init # creates /etc/apache-sites and config.toml
sudo apache-sites # start the editor
Using the editor
- Sites list:
nnew,Enteredit,ddelete/undelete,eenable/disable,vview the rendered config,ccertificates,sglobal settings,isystem info (interfaces, PHP pools, load problems, last apply log),aapply,udiscard all pending changes,qquit. - Site form: arrow keys move.
EnterorSpaceedits or toggles a field,←/→cycle choices,Ctrl-S/F2keep the edits as pending changes,Escgoes back, andCtrl-P/F3previews the rendered config. Problems with the site are listed live at the bottom. A new site's log file name defaults to the first part of its hostname (wikiforwiki.example.com). AllowOverride offersNone,All, or a custom value you type in. - Users (shown when Basic auth is on):
aadd,Enter/pset password,ddelete. Passwords are hashed with bcrypt. - Settings: edits
config.toml(default site, catch-all certificate, certbot directory, ACME webroot, paths, commands, bcrypt cost). Same keys as the site form;Ctrl-S/F2keeps the edits as pending changes. Commands are edited as one line; quote arguments that contain spaces. The file is rewritten on apply, so comments in it are not kept. - Certificates:
apastes in a certificate and then its key, anddremoves or restores an unmanaged certificate (one you installed yourself). Let's Encrypt certificates are read-only (manage them with certbot).
Every change is collected into one batch. Nothing touches disk until you apply. Quitting with pending changes asks before discarding them.
What "apply" does
- Validates every site and refuses to continue if there are errors.
- Renders all enabled sites into
/etc/apache-sites/rendered.new/and installs any newly pasted certificates. - Swaps the new rendered directory into place, keeping the old one. Then it installs
/etc/apache2/conf-available/apache-sites.conf, enables it (fromconf-enabled, orsites-enabledwhendefault_site = "apache"), and enables any Apache modules the sites need (a2enmod). - Runs
apache2ctl configtest. If that fails, every change from steps 2–3 is rolled back and Apache is not reloaded. Your edits stay pending so you can fix them. - Saves
config.toml, the site files and password files, removes deleted certificates, and runssystemctl reload apache2.
Command line
apache-sites [--root DIR] [--config-dir DIR] [command]
edit | list | check | render [HOST] | apply | scan | passwd HOST USER | init
apply, check, and render work on the saved site files, so sites can also be edited by hand. passwd reads the password from stdin.
Files
/etc/apache-sites/
config.toml global settings (commands, paths, default_site, acme_webroot, ...)
sites/<hostname>.toml one file per site
passwords/<hostname>.htpasswd
tls/<name>.crt, <name>.key unmanaged (manually installed) certificates (key mode 0600)
rendered/<hostname>.conf generated Apache config (do not edit)
rendered/default.d/ the default site, included first
/etc/apache2/conf-available/apache-sites.conf IncludeOptional of rendered/*.conf
Example site file (every key is optional except hostname):
hostname = "wiki.example.com"
aliases = ["www.wiki.example.com"]
enabled = true
log = "wiki" # ${APACHE_LOG_DIR}/wiki.log; "" = no access log
interfaces = ["eth0"] # [] = all addresses
allow_from = ["192.168.0.0/16"] # [] = everyone
basic_auth = false
tls = true
cert = "" # "" = best match; or "letsencrypt:NAME" / "unmanaged:NAME"
service = "php" # static | php | proxy
document_root = "/srv/wiki"
directory_index = "index.php index.html"
allow_override = "None"
php_version = "8.5"
php_user = "dokuwiki"
proxy_url = ""
proxy_nocanon = false
allow_encoded_slashes = "off" # off | on | nodecode
proxy_websockets = false
proxy_preserve_host = false
raw = '''
Header set X-Frame-Options "SAMEORIGIN"
'''
Notes and behaviours worth knowing
- Interfaces. Sites name interfaces (
eth0), not addresses. They are turned into the interface's current IPv4/IPv6 addresses each time you apply, so re-apply after an address changes. Link-local IPv6 is skipped. A site set to "all addresses" also lists the specific addresses used by interface-bound sites. Otherwise Apache would never match it on those addresses. - Default site. Apache sends a request whose hostname matches no site (a bare IP, a stray DNS name, a scanner) to the first vhost it loaded for that address:port. Ubuntu loads
conf-enabled/beforesites-enabled/, so without intervention that would be the alphabetically first apache-sites site.default_siteinconfig.tomlcontrols this:"reject"(default): a catch-all vhost inrendered/default.d/, loaded before every site, answers 421 Misdirected Request on every address and port our sites use. Port 443 uses Ubuntu's snakeoil certificate (default_cert/default_key; needs thessl-certpackage)."<hostname>": that site is rendered intodefault.d/so it loads first and becomes the default wherever it listens."apache": Apache's own default (000-default) stays in charge. The include file stays inconf-availablebut is enabled assites-enabled/zzz-apache-sites.confinstead of fromconf-enabled, so it loads after your other sites. This doesn't cover addresses that interface-bound sites listen on (a warning says so)."none": no handling; the alphabetically first site wins (warns).
- Certificates. TLS defaults to on for a new site when a certificate matches its hostname, exactly or by wildcard. Pasted keys must be unencrypted. A certificate that doesn't match its key is caught by Apache's configtest.
- certbot. Port 80 of a TLS site is a plain redirect to HTTPS. If you renew with
certbot certonly --webroot -w DIR, setacme_webroot = "DIR"inconfig.toml. Port 80 then serves/.well-known/acme-challenge/from there instead of redirecting it. - Logging. An empty log name disables access logging. Errors still go to Apache's global error log.
- WebSockets use
ProxyPass ... upgrade=websocket(Apache 2.4.47+).
Development
test/make-fixture.sh DIR builds a fake filesystem root with PHP pools, a self-signed "Let's Encrypt" cert, and a config that replaces the Apache commands with harmless ones. Then run zig-out/bin/apache-sites --root DIR to try the editor without touching the host's Apache.
Source layout: src/toml.zig (TOML subset), site.zig (data model), scan.zig (PHP pools, certificates, interfaces), render.zig (validation and Apache rendering), state.zig (editing session), apply.zig (transactional apply), htpasswd.zig, and tui/ (terminal layer, widgets, app).