Digital garden

Asterisk setup

How i installed asterisk from scratch, including every step and command i ran.

  • updated 2026-10-06
  • #voip

To install asterisk, i’ve used debian since my machine to run it is raspberry pi, but we could use ubuntu as well, however if you plan to install freepbx, it seems debian is required, that was the reason why i choose debian in the first place, to make sure the same steps can be used by freepbx.

How to install

First you have to download asterisk:

wget https://downloads.asterisk.org/pub/telephony/asterisk/asterisk-23.5.0.tar.gz

After downloading the tar.gz file you will have to extract it, make sure you have necessary packages installed to do so:

sudo apt install build-essential tar
tar -zxvf asterisk-*.tar.gz

After that cd into the folder you just made by extracting the tar file, and start building by using these commands: (This will install all the required packages that asterisk needs)

cd [extracted_folder]
./contrib/scripts/install_prereq install

Now run:

./configure --with-jansson-bundled
make menuselect
make
## Make sure you read the output, based on selected things to install you might need to run other commands
make install

At this point installation should be completed, but you can run these to have a headstart in terms of configs:

make samples
mkdir /etc/asterisk/samples
mv /etc/asterisk/*.* /etc/asterisk/samples
make basic-pbx
make config

Now make sure the asterisk is enabled on the boot:

systemctl enable asterisk
systemctl start asterisk

Get into the console and enjoy:

/usr/sbin/asterisk -r

That’s it, you successfully setup asterisk!

Console commands

When in the asterisk console, you can reload pjsip by doing one of the following commands:

# to reloead all extensions
core reload
# to reload pjsip
module reload res_pjsip

Restart for changes

If you want to restart completely just run the following command:

core restart now

This will restart asterisk completely, it’s better since core reload won’t restart some services.

Change verbosity

You can debug and change verbosity in two ways, one is when you enter the console:

asterisk -rvvv

The other solution is when we are inside the console:

> core set verbose 3

Enable AGI Debug

To see what things are happening in the AGI script, you can enable the debug:

agi set debug on

Definitions

Since VOIP networks have their own definitions, we are going to add them here to better understand the rest of the document:

  • channels: Channels are connections between asterisk and other devices, like an IP Phone.
    • Note: Channels are not calls, it’s a connection.
    • Channels can work with different protocols, like SIP, PJSIP or IAX or anything else.
    • For a phone call to be established, asterisk acts as a gateway to connect the channels together.
    • A channel has lots of information inside, like caller id and etc..
  • dial plan: It’s the call routing table of asterisk.
    • rules: It defines who call use what functions, like dial or who can call who etc…
    • The configuration is stored under /etc/asterisk/extension.conf

Configs

All asterisk configuration files are stored in /etc/asteris/ folder, the most important ones are:

  • estensions.conf These are extensions and what features can be worked with.

Dial Plan

To write a dial plan in extension.conf we use a simple format:

; [context name]
; exten = [name or id], [priority=1], [function to call]

Here is a sample:

[context_1]
exten = 100, 1, wait(2)
 same = n, noo()

To make the changes take effect you should reload the dial plan, to do it connect to the asterisk shell:

asterisk -r
> dialplan reload

Include

You can include other contexts into a parent one using include keyword, the fun fact is that you can make it conditional to only include it if certain conditions are met:

[ctx_1]
exten test, 1, wait(2)
include = ctx_2
; include = ctx_2, 8:00-16:00, [days=*], [months=*], [idk=*]

[ctx_2]
exten test, 1, noo()

You can also include by file: (imagine you have a context saved in ctx_1.conf)

[ctx_2]
#include conf_1.conf

Available Applications

Functions used in Dial plans are called applications:

Playback

To play a sound, it’s blocking

playback(sound_1&sound_2&...)

Wait

To block for a certain amount of time (in seconds):

await([seconds_to_wait])

Background

It’s similar to playback, but it accepts user input (DTMF) meanwhile:

background([name_of_the_sound_file])

ARI

The best API to control dial plan is ARI, we have AGI as well but it’s not even close as good as what ARI can do, to enable modules make sure you have loaded all of them:

require => res_http_websocket.so ; HTTP WebSocket Support, needed for res_ari_events
require => res_ari.so ; Asterisk RESTful Interface
require => res_ari_applications
require => res_ari_asterisk.so
require => res_ari_bridges
require => res_ari_channels.so
require => res_ari_device_states.so ; RESTful API module - Device state resources
require => res_ari_endpoints.so
require => res_ari_events.so
require => res_ari_model.so ; ARI Model validators
require => res_ari_playbacks.so
require => res_ari_recordings.so
require => res_ari_sounds
require => res_stasis.so ; Stasis application support
require => app_stasis.so ; Stasis dialplan application
require => res_stasis_answer.so
require => res_stasis_device_state.so ; Stasis application device state support
require => res_stasis_playback.so ; Stasis application playback support
require => res_stasis_recording.so ; Stasis application recording support
require => res_stasis_snoop.so ; Stasis application snoop support

Or if you want to persist it, simply add this to modules.conf:

; this is important
autoload = yes

; ARI
load = res_ari.so
load = res_ari_applications.so
load = res_ari_asterisk.so
load = res_ari_bridges.so
load = res_ari_channels.so
load = res_ari_device_states.so
load = res_ari_endpoints.so
load = res_ari_events.so
load = res_ari_model.so
load = res_ari_playbacks.so
load = res_ari_recordings.so
load = res_ari_sounds.so

To make sure all of them are loaded:

> module show like ari

Asiatech mikrotik setup

Here are the firewall rules i used in microtik for asiatech trunk:

add action=src-nat chain=srcnat dst-address=185.98.113.0/24 src-address=192.168.88.50 to-addresses=85.198.30.130

add action=dst-nat chain=dstnat dst-port=5060 protocol=udp src-address=185.98.113.196 to-addresses=192.168.88.50 to-ports=5060

add action=dst-nat chain=dstnat dst-port=10000-50000 protocol=udp src-address=185.98.113.208/29 to-addresses=192.168.88.50  to-ports=10000-50000

add action=dst-nat chain=dstnat dst-port=10000-50000 protocol=udp src-address=185.98.113.216/29 to-addresses=192.168.88.50  to-ports=10000-50000

add action=dst-nat chain=dstnat dst-port=10000-50000 protocol=udp src-address=185.98.113.120/29 to-addresses=192.168.88.50 to-ports=10000-50000

How to upgrade asterisk

Well if you want to upgrade asterisk, the steps are the same as installation, you need to fetch the latest version file and follow the steps, but you should stop short when you reach make config:

# all the steps of installation
# do not run make basic-pbx (because i haven't tested that one)
# backup existing configration files
mkdir ~/asterisk-config-backup
cp -r /etc/asterisk/* ~/asterisk-config-backup
# now do the rest of the installation...
make config

The installation is pretty straightforward, the only thing you should be worried about is the configuration files.

How to convert files to be used in IVR

First install sox:

brew install sox

then you can convert your mp3 files using sox to appropriate wav files like this:

sox [input.mp3] -c 1 -r 8000 -b 16 [output.wav]

Note:

  • -c means mono channel
  • -r is 8k
  • -b is 16-bits

Troubleshoot

If uallaw issue, run this in asterisk console:

module load codec_alaw.so

To monitor stuff in microtik:

/tool sniffer quick port=5060

Read more about it here (opens in a new tab).

Disconnection after ~50 sec

I had this wired issue from the day one that after almost 50 seconds, call would be dropped, after searching a lot i found out that OPTION is not being answered by asterisk when provider is sending them however this is not the case with chan_sip, only pjsip has this issue, i was forced to downgrade to v20 to use chan_sip, today i gave it another try after almost 8 months.

After a lot of try i couldn’t find the issue but accidentally i found a post (opens in a new tab) on redit that had the exact problem as mine, the issue is a missing module, just add the following line to /etc/asterisk/modules.conf:

// Resources
load = res_pjsip_dlg_options.so
pezhvak@web:~$
↑↓move↵openescclose

keys

←→↑↓
Move between tiles
↵
Open the focused tile. Articles: start reading
QE
Home: highlights / recently updated. Gallery: filter by medium. Articles: previous / next entry
JK
Next / previous section of an article
F
Gallery work: full size
A
Home: all entries of the focused tile's section
esc
Back
1…5
Jump to a section
/
Search everything
R
Random note from the garden
M
UI sounds on / off
H
Go home