TECHNICAL GUIDE

Use this guide as an implementation reference. Validate commands, policies and version-specific behaviour in a lab before applying changes to production systems.

1. Architecture and prerequisites

Asterisk is an open-source communications framework. A typical PBX includes SIP signalling (PJSIP), RTP media streams, dial plan logic, voicemail and optional trunks to an ITSP or PSTN gateway. This guide uses Asterisk 22 LTS as an example and assumes a clean Ubuntu 24.04 server with a static address. The exact latest patch release should be downloaded from the official Asterisk release page.

For production, place the PBX behind a properly configured firewall, plan NAT and public IP behaviour, and never expose anonymous SIP access. Voice quality depends on latency, jitter, packet loss and network capacity.

2. Prepare Ubuntu and dependencies

sudo apt update && sudo apt full-upgrade -y\nsudo apt install -y build-essential wget curl git subversion \\\n  libxml2-dev libncurses5-dev uuid-dev libjansson-dev \\\n  libsqlite3-dev libssl-dev libedit-dev pkg-config \\\n  libcurl4-openssl-dev libspeexdsp-dev

Install only the libraries needed by the modules you plan to build. Review the Asterisk dependency and codec documentation for optional features.

3. Download and compile Asterisk

Download the Asterisk 22 source archive from the official downloads page and verify the published checksum or signature. The commands below illustrate the build workflow; replace the archive name with the current 22.x patch release.

cd /usr/src\nsudo wget https://downloads.asterisk.org/pub/telephony/asterisk/asterisk-22-current.tar.gz\nsudo tar -xzf asterisk-22-current.tar.gz\ncd asterisk-22.*\nsudo contrib/scripts/install_prereq install\nsudo ./configure\nsudo make menuselect

In menuselect, select the channel drivers, codecs, applications and formats you actually need. Then compile and install:

sudo make -j$(nproc)\nsudo make install\nsudo make samples\nsudo make config\nsudo ldconfig

Sample configuration files are for reference only. Review them and remove insecure demo settings before starting a production PBX.

4. Create a dedicated Asterisk service account

sudo groupadd --system asterisk\nsudo useradd --system --gid asterisk --home-dir /var/lib/asterisk \\\n  --no-create-home --shell /usr/sbin/nologin asterisk\nsudo chown -R asterisk:asterisk /var/lib/asterisk /var/log/asterisk /var/spool/asterisk\nsudo chown -R asterisk:asterisk /var/run/asterisk\nsudo nano /etc/asterisk/asterisk.conf

Set the run user and group in the Asterisk configuration, then review ownership of configuration files. Do not run Asterisk as root in normal operation.

5. Configure PJSIP extensions

For a small lab, create two extensions such as 1001 and 1002 in /etc/asterisk/pjsip.conf. Use long random secrets and restrict registrations to trusted subnets where possible.

[transport-udp]\ntype=transport\nprotocol=udp\nbind=0.0.0.0:5060\n\n[1001]\ntype=endpoint\ncontext=internal\n disallow=all\nallow=ulaw,alaw\nauth=1001-auth\naors=1001-aor\n\n[1001-auth]\ntype=auth\nauth_type=userpass\nusername=1001\npassword=REPLACE_WITH_LONG_RANDOM_SECRET\n\n[1001-aor]\ntype=aor\nmax_contacts=1\n\n[1002]\ntype=endpoint\ncontext=internal\ndisallow=all\nallow=ulaw,alaw\nauth=1002-auth\naors=1002-aor\n\n[1002-auth]\ntype=auth\nauth_type=userpass\nusername=1002\npassword=REPLACE_WITH_ANOTHER_LONG_RANDOM_SECRET\n\n[1002-aor]\ntype=aor\nmax_contacts=1

Repeat the auth/AOR/endpoint pattern for additional phones. Ensure each endpoint has a unique identity and secret. Do not publish sample secrets.

6. Create a basic dial plan

In /etc/asterisk/extensions.conf, define an internal context. This example lets extension 1001 call 1002 and vice versa:

[internal]\nexten => 1001,1,Dial(PJSIP/1001,20)\n same => n,Voicemail(1001@default,u)\n same => n,Hangup()\nexten => 1002,1,Dial(PJSIP/1002,20)\n same => n,Voicemail(1002@default,u)\n same => n,Hangup()

Configure voicemail mailboxes in voicemail.conf if voicemail is required. Do not create a public inbound context that permits arbitrary dialling.

7. Configure RTP, NAT and firewall

Asterisk commonly uses UDP 5060 for SIP signalling and a configurable RTP range, often UDP 10000–20000. Confirm the range in rtp.conf. Permit these ports only from the trusted phone/VPN/provider networks. For remote phones, configure PJSIP external signalling/media addresses and local network ranges correctly; incorrect NAT settings cause one-way or no audio.

sudo ufw allow from 10.20.0.0/16 to any port 5060 proto udp\nsudo ufw allow from 10.20.0.0/16 to any port 10000:20000 proto udp

Replace the sample subnet with your real voice network. Do not use unrestricted allow 5060/udp on an internet-facing PBX. Consider a VPN for remote extensions and fail2ban or equivalent intrusion controls.

8. Start and test Asterisk

sudo systemctl enable --now asterisk\nsudo systemctl status asterisk\nsudo asterisk -rvvv

Useful CLI checks:

pjsip show endpoints\npjsip show contacts\ndialplan show internal\ncore show channels\nrtp show settings

Register two softphones to the server using extensions 1001 and 1002, then test registration, two-way audio, DTMF, voicemail and call teardown. Check firewall counters and logs when calls connect without audio.

9. TLS, monitoring and operational security

For production, configure TLS/SRTP where supported by all endpoints, use strong unique credentials, restrict management access, keep Asterisk patched, and monitor failed registrations and unusual call volume. Configure log rotation, time synchronisation, backups of /etc/asterisk and voicemail, and test restore procedures. Apply rate limits and spending controls at the SIP trunk provider.

FreePBX is a separate web-based PBX administration platform with its own supported installation matrix. Do not install a random FreePBX script on top of an existing Asterisk build without checking the exact FreePBX release and OS support.

Official references

Conclusion

A secure PBX needs correct SIP identities, a tightly scoped firewall, working RTP/NAT configuration and ongoing monitoring. Test calls from both sides of the network and verify two-way audio before connecting a production SIP trunk.

Found this useful?