apipbx

Install apipbx

From a fresh server to a call between two phones.

What you need

You do not need Docker, Kubernetes or any secret store. apipbx brings its own PostgreSQL database and Asterisk configuration.

1. Add the package repository

sudo install -d -m 0755 /usr/share/keyrings
curl -fsSL https://packages.abt.me/pubkey.gpg | sudo gpg --dearmor -o /usr/share/keyrings/abtme.gpg
echo "deb [signed-by=/usr/share/keyrings/abtme.gpg] https://packages.abt.me/apt $(. /etc/os-release; echo $VERSION_CODENAME) main" \
  | sudo tee /etc/apt/sources.list.d/abtme.list
sudo apt update

The repository is signed. It carries a current Asterisk 22 LTS for each supported release (the distribution’s own Asterisk can be missing security fixes), so apt installs that one instead.

2. Install apipbx

sudo apt install apipbx

This installs Asterisk, PostgreSQL and the ODBC driver, then sets everything up for you:

Check that it is running:

sudo systemctl status apipbxd asterisk
KEY=$(sudo cat /etc/apipbx/admin.key)
curl -s -H "Authorization: Bearer $KEY" http://127.0.0.1:8080/v1/tenants

An empty list means the API answered.

Tell it your public address

If the server sits behind NAT, or has a public address Asterisk cannot see for itself, set it, so phones are told where to send audio:

sudo sh -c 'set -a; . /etc/apipbx/apipbx.env; set +a
  apipbxd settings set -external-ip 203.0.113.5 -local-net 10.0.0.0/8
  apipbxd apply'

apipbxd apply rewrites the Asterisk configuration from the settings stored in the database and reloads Asterisk only if something changed. It is safe to run any time, and a systemd timer runs it every 15 minutes to put back anything edited by hand.

3. Make a tenant and two extensions

A tenant is one customer of your phone system. Everything below belongs to a tenant.

KEY=$(sudo cat /etc/apipbx/admin.key)
API=http://127.0.0.1:8080/v1
auth="Authorization: Bearer $KEY"

curl -s -H "$auth" -d '{"slug":"acme","name":"Acme Ltd"}' $API/tenants
# the answer includes the tenant's id; the examples below use 1

curl -s -H "$auth" -d '{"number":"1001","name":"Reception"}' $API/tenants/1/extensions
curl -s -H "$auth" -d '{"number":"1002","name":"Sales"}'     $API/tenants/1/extensions

Each answer contains the SIP username (for example t1-1001) and a generated secret. Register a softphone or desk phone to the server’s address, port 5060, with those details. Then dial 1002 from the first phone.

Voicemail, ring groups, IVR menus, queues, time conditions and the rest are further resources under the same tenant; the feature list names them. The API description is served by your own server and in the control panel.

4. Connect a carrier

Add a trunk for your voice provider, then an inbound route for each number you hold and an outbound route for the numbers you dial out to:

# a SIP trunk that registers with your provider
curl -s -H "$auth" -d '{"name":"provider","tech":"pjsip","host":"sip.example.net","username":"acct","secret":"...","register":true}' \
  $API/tenants/1/trunks

A trunk can be pjsip or iax2. Route calls to extensions, ring groups, IVRs and other destinations with inbound routes; outbound routes take dial patterns and an ordered list of trunks, and fall over to the next one when a trunk fails.

5. Turn on the firewall

Asterisk is a target for scanners the moment it has a public address. apipbx has a host firewall that is installed off, so it never surprises you. Before you turn it on, say who may reach what:

You can do this from the control panel under This server, Firewall, or through the API (/v1/firewall). Turn it on last.

Also open on any other firewall in front of the server: SIP 5060 (UDP/TCP), IAX2 4569 (UDP) and RTP 10000-20000 (UDP) for what you use. The API on port 8080 is plain HTTP: keep it on the loopback address and put a TLS proxy in front if you want to reach it from elsewhere. AMI (port 5038) must stay on localhost.

6. Install the control panel

The panel is a separate package. On the same server, after apipbx:

sudo apt install apipbx-control

It sets itself up, registers the apipbx on the same machine as “this server”, and prints an initial admin password (also saved in /etc/apipbx-control/initial_admin_password; delete the file once you have noted it). It listens on 127.0.0.1:8090 over plain HTTP, and its sign-in cookies need HTTPS, so put a TLS proxy in front. With Caddy, which gets a Let’s Encrypt certificate for you:

sudo apt install caddy
sudo tee /etc/caddy/Caddyfile <<'CADDY'
panel.example.com {
    reverse_proxy 127.0.0.1:8090
}
CADDY
sudo systemctl reload caddy

panel.example.com must already point at the server. Sign in as admin. The panel can also manage other apipbx servers: add each on the Servers page with its address and a platform API key.

7. Manage it with Terraform (optional)

terraform {
  required_providers {
    apipbx = {
      source = "abtme/apipbx"
    }
  }
}

provider "apipbx" {
  endpoint = "https://pbx.example.com"   # or APIPBX_ENDPOINT
  token    = var.apipbx_token            # or APIPBX_TOKEN
}

resource "apipbx_tenant" "acme" {
  slug = "acme"
  name = "Acme Ltd"
}

resource "apipbx_extension" "reception" {
  tenant_id = apipbx_tenant.acme.id
  number    = "1001"
  name      = "Reception"
}

A platform API key can create tenants and manage everything; a tenant key can manage only its own tenant. The provider’s source and release notes are at github.com/abtme/terraform-provider-apipbx.

Moving from FreePBX

Make a backup in FreePBX (the .tar.gz its Backup module writes; tested with FreePBX 15) and copy it to the server. Look first: this prints what would be created, what needs a person’s eye and what is not imported, and changes nothing.

apipbxd import-freepbx -file backup.tar.gz -tenant-slug acme -tenant-name "Acme Ltd"

Then create it, with a platform key:

sudo apipbxd import-freepbx -file backup.tar.gz -tenant-slug acme -key-file /etc/apipbx/admin.key -apply

It creates a new tenant through the API, so every rule the API has applies to the imported data, and it never changes an existing tenant. Extensions keep their SIP and IAX2 secrets, so phones work once they are pointed at the new server. What comes across: extensions and voicemail boxes, IAX2 trunks, inbound and outbound routes, ring groups, time groups and conditions, conferences and the blacklist. What does not yet (and is listed in the report): queues, FreePBX’s other IVRs, PJSIP trunks, stored voicemail messages and call history. Afterwards, point your carriers at the new server and re-register your phones.

The backup holds credentials, so keep it where only you can read it. The tool prints no secrets.

Upgrading

sudo apt update && sudo apt upgrade

The database upgrades itself when apipbxd starts, and your settings, keys and data are left alone. When Asterisk itself is upgraded over a server where apipbx has written its configuration, dpkg asks whether to keep the changed pjsip.conf and other generated files. Answer N (keep the installed ones), or for unattended upgrades use:

sudo apt-get -o Dpkg::Options::=--force-confold upgrade

Backups and restore

A backup runs daily and keeps the last 14 in /var/backups/apipbx. Change the schedule, how many are kept, and add an SFTP copy to another machine in the panel (Backups) or through the API. To restore, install apipbx on the new machine, stop apipbxd and Asterisk, and run apipbxd restore -file <dump> -yes as the apipbx user with /etc/apipbx/apipbx.env loaded, then start them again. Restore is deliberately a command-line operation. After a restore on a machine with a different address, set the new -external-ip (step 2) and run apipbxd apply.

Getting help

Write to info@apipbx.uk with what you ran, what you expected and what happened. The output of sudo journalctl -u apipbxd -u asterisk --since "10 minutes ago" helps.