Skip to content

📞 FreePBX & Asterisk PBX Configuration Guide

This guide details the configuration of Asterisk PBX (18 / 20 / 21 / 22) and FreePBX for seamless integration with py-phone-caller.

Tip

Looking for Complete Native /etc/asterisk/*.conf Examples (No FreePBX GUI)?
See Asterisk PBX Configuration & Examples (Native Linux) for complete, copy-pasteable configurations (http.conf, ari_py_phone_caller.conf, pjsip_py_phone_caller.conf with SIP trunks & local softphone extension 1000, extensions_py_phone_caller.conf with DTMF 4 acknowledgment callbacks, modules.conf, SELinux rules, and matching settings.toml / .secrets.toml).

Automated Native Asterisk 22 Provisioning (Ansible):
If you are deploying Asterisk directly on Linux (Debian 13 via AllStarLink, Ubuntu 26.04 LTS official packages, or Rocky Linux 10 / AlmaLinux 10 via Tucny 22.9.0-1.el10), use the automated asterisk_py_phone_caller Ansible role documented in deploy/docs/02-ansible-deployment.md.

Ready-to-Use Local SIP Extension 1000 (Optional): Set asterisk_sip_exten_1000_enabled: true in your Ansible inventory to automatically provision a ready-to-register local PJSIP extension (1000) with a cryptographically random password logged during execution (and preserved idempotently across reruns). It uses max_contacts=1 and qualify_frequency=0 for reliable mobile/VPN/air-gapped softphone registration (e.g., Linphone) and supports both ARI voice calls and RFC 3428 SIP MESSAGE text alerts from caller_sms.


📑 Table of Contents

  1. Architectural Role of Asterisk & FreePBX
  2. Prerequisites & Network Ports
  3. Configuring Outbound SIP / PJSIP Trunks (Visual)
  4. Creating SIP / PJSIP Extensions for Testing
  5. Creating the Custom Extension & Stasis Dialplan
  6. Configuring Asterisk REST Interface (ARI) & WebSockets
  7. RTP, Codecs & NAT Network Settings
  8. Testing & Connectivity Verification

1. Architectural Role of Asterisk & FreePBX

py-phone-caller uses the Asterisk REST Interface (ARI) to dynamically originate outbound phone calls, control audio playback, deliver optional RFC 3428 SIP MESSAGE notifications (caller_sms asterisk_sip carrier), and capture real-time dual-tone multi-frequency (DTMF) keypad responses from on-call personnel.

+-----------------------+                         +---------------------------+
|    asterisk_caller    |---(1. HTTP ARI POST)--->| Asterisk PBX / FreePBX    |
| (Outbound Controller) |                         | (ARI Port 8088 / SIP 5060)|
+-----------------------+                         +---------------------------+
                                                                |
+-----------------------+                         (2. Places Outbound Call)
|  asterisk_ws_monitor  |<--(3. WS Stasis Events)-+             |
| (Audio/Playback Loop) |                                       v
+-----------------------+                         +---------------------------+
                                                  | PSTN / SIP Trunk / Mobile |
                                                  +---------------------------+

2. Prerequisites & Network Ports

Ensure the following network communication is allowed between the py-phone-caller host/containers and the Asterisk PBX:

Port / Protocol Direction Purpose Default Value
8088 / TCP App ➔ PBX Asterisk HTTP ARI & WebSocket Events (/ari/events) 8088 (or 8089 for TLS)
5060 / UDP, TCP PBX ➔ Gateway SIP Trunk Signaling (PJSIP or chan_sip) 5060
10000–20000 / UDP Bidirectional RTP Audio Media Streams 10000:20000

3. Configuring Outbound SIP / PJSIP Trunks (Visual)

To route emergency calls out to landlines or mobile phones, configure a SIP or PJSIP trunk in the FreePBX GUI:

Step 3.1: Navigate to Trunks

  1. In the FreePBX top navigation menu, click Connectivity.
  2. Select Trunks.

Trunk Step 1

Step 3.2: Add a New Trunk

  1. Click the + Add Trunk button.
  2. Select + Add SIP (chan_pjsip) Trunk (or chan_sip for legacy gateways).

Trunk Step 2

Step 3.3: Configure General Trunk Settings

  1. Set the Trunk Name (e.g. sip-provider).
  2. Set the Outbound CallerID to your organization's valid emergency caller ID.

Trunk Step 3

Step 3.4: Configure SIP Credentials & Server Details

  1. Switch to the pjsip Settings (or sip Settings) tab.
  2. Enter your VoIP provider's SIP Server / Outbound Proxy, Username, and Secret / Password.
  3. Set Context to from-trunk and Authentication to Outbound.

Trunk Step 4 Trunk Step 5

Step 3.5: Apply Config

Click Submit and then click the red Apply Config button at the top right of the FreePBX header.

Trunk Step 6


4. Creating SIP / PJSIP Extensions for Testing

For lab environments and softphone verification (e.g. Linphone, Zoiper, MicroSIP):

Step 4.1: Navigate to Extensions

Click Applications ➔ Extensions ➔ + Add Extension ➔ + Add New PJSIP Extension.

Extension Step 1

Step 4.2: Configure Extension Credentials

  1. Set User Extension (e.g. 2001).
  2. Set Display Name (e.g. On-Call Test Phone).
  3. Set Secret (PJSIP password).

Extension Step 2 Extension Step 3 Extension Step 4

Click Submit and Apply Config.


5. Creating the Custom Extension & Stasis Dialplan

Asterisk needs a dialplan context that routes answered calls into the Stasis application managed by asterisk_ws_monitor.

Step 5.1: Configure extensions_custom.conf

Log into your Asterisk/FreePBX server via SSH and edit /etc/asterisk/extensions_custom.conf (replacing <PY_PHONE_CALLER_HOST> with the IP or hostname of your py-phone-caller caller_register service):

[py-phone-caller-custom]
exten => s,1,NoOp(Starting py-phone-caller Stasis Application)
 same => n,Answer()
 same => n,Playback(greeting-message)
 same => n,Stasis(py-phone-caller)
 same => n,Set(RES=${CURL(http://<PY_PHONE_CALLER_HOST>:8083/heard?asterisk_chan=${CHANNEL(uniqueid)})})
 same => n,Playback(press-4-for-acknowledgement)
 same => n,Playback(beep)
 same => n,Read(get,"silence/1",,,,2)
 same => n,GotoIf($["${get}" = "4"]?ack:done)
 same => n(ack),Set(RES=${CURL(http://<PY_PHONE_CALLER_HOST>:8083/ack?asterisk_chan=${CHANNEL(uniqueid)})})
 same => n(done),Playback(vm-goodbye)
 same => n,Hangup()

Reload Asterisk dialplan:

asterisk -rx "dialplan reload"

Step 5.2: Register Custom Extension in FreePBX (Optional GUI Binding)

  1. Go to Applications ➔ Custom Extensions.
  2. Click + Add Custom Extension.
  3. Set Custom Extension to 999 and Target to py-phone-caller-custom,s,1.

Custom Extension 1 Custom Extension 2 Custom Extension 3


6. Configuring Asterisk REST Interface (ARI) & WebSockets

Enable the HTTP server and create dedicated credentials for py-phone-caller:

Step 6.1: Configure /etc/asterisk/http.conf

Ensure Asterisk's built-in HTTP server is active:

[general]
enabled=yes
bindaddr=0.0.0.0
bindport=8088
prefix=

Step 6.2: Configure /etc/asterisk/ari.conf

Configure the ARI user matching your settings.toml or environment variables:

[general]
enabled = yes
pretty = yes
allowed_origins = *

[py-phone-caller]
type = user
read_only = no
password = <PASSWORD>
password_format = plain

Step 6.3: GUI Alternative via FreePBX Asterisk REST Interface Users

  1. Go to Admin ➔ Asterisk REST Interface Users.
  2. Add user py-phone-caller, set Read Only to No, and configure your password.

ARI User Step 1 ARI User Step 2 ARI User Step 3 ARI User Step 4

Reload Asterisk ARI modules:

asterisk -rx "module reload res_http_server.so"
asterisk -rx "module reload res_ari.so"

7. RTP, Codecs & NAT Network Settings

For clean audio playback without one-way audio issues:

  1. Audio Codec: Ensure alaw and ulaw (G.711) are enabled in your SIP trunk and extensions. generate_audio generates 8000 Hz 16-bit PCM WAV audio matching G.711 native sampling.
  2. NAT Settings (/etc/asterisk/pjsip.conf or SIP Settings GUI):
    • If Asterisk is behind NAT, configure external_media_address and external_signaling_address.
    • Set local_net=192.168.0.0/16 (or your local subnet CIDR).

8. Testing & Connectivity Verification

From the py-phone-caller host, verify ARI connectivity with curl:

# 1. Test HTTP ARI API
curl -v -u py-phone-caller:<PASSWORD> \
  http://<asterisk-ip>:8088/ari/asterisk/info

# Expected output: HTTP 200 OK with Asterisk version information JSON
# 2. Test Stasis WebSocket Connection
uv run python -c "
import asyncio, websockets
async def test():
    uri = 'ws://py-phone-caller:<PASSWORD>@<asterisk-ip>:8088/ari/events?app=py-phone-caller'
    async with websockets.connect(uri) as ws:
        print('Connected successfully to Asterisk Stasis WebSocket!')
asyncio.run(test())
"

Once verified, configure asterisk_host, asterisk_user, and asterisk_pass in src/config/settings.toml or your deployment .env file!