Asterisk setup and config tutorial

Asterisk PBX (private branch exchange) is a fully featured phone system. From what I’ve read, it’s used by companies in all shapes and sizes, and can be made to do some pretty amazing things.

My current set up has 3 different incoming UK numbers (for three different companies) hitting my Asterisk. Each number is handled differently. They forward calls to mobiles, take voicemails, record all calls, email call alerts, forward to SIP Phones or a combination of these options. My aim is to show you how to configure asterisk to do all of the above and more.

This guide is covers installation of Asterisk 1.8 on a CentOS 5 VPS in 2012. The intention is for this guide to serve as a reminder to me and a tutorial for you. I had an installation running for about 6 months until RAID crashed on the VPS and I lost the whole asterisk. Starting from scratch, I realised I had forgotten where to start and how I’d configured the system. The new system is better now. I digress.

What you’ll need

  • A server, preferably Linux. As mentioned I’m using a VPS with CentOS 5.
  • A SIP based VOIP Phone. This can be software (I recommend X-Lite (free) for windows and mac – the site is geared towards selling, if you can’t find the files please comment and I’ll update with direct links.) You may also use a Hardware SIP Phone.
  • A VOIP account – I use RXHost in the UK to provide my UK inbound number and handle the outbound calls (I added credit to my account). They also provide US and other international numbers too.  A trial account is free but will take about 24 hours to set up because they call you to confirm your identity and help you get set up.

We’re not covering physically attaching asterisk to “real” PSTN phone lines, in favour of using “virtual” sip trunks with real phone numbers.

Installing Asterisk

I don’t intend to re-invent the wheel here. You’re going to need root access to your server and be comfortable at command line. There were two resources I found online that got me installed: Installing asterisk 1.8 on CentOS 5 (opens in new window) and Installing asterisk 1.6 on CentOS 5.2.

I followed the Asterisk 1.8 and CentOS 5 guide and it seemed to do the trick. If you get stuck please just leave a comment and I’ll do my best to help.

 Starting Asterisk

Now, I hope you have Asterisk all installed and didn’t end up stuck or with too many errors. Presuming everything went to plan, you can now type

asterisk

to start asterisk. Then the following to “connect” to the asterisk server.

asterisk -r

After running the above command you’ll see (or something similar):

Connected to Asterisk 1.8.15.0 currently running on pbx (pid = 1799)
pbx*CLI>

Congratulations! You have a running PBX.

Preparing to make and receive calls

Now since Asterisk is going to be routing your calls over the internet you’re going to want to make sure you have a firewall set up and open up the ports required for it to work. You need to make sure you only open as many ports as you need to make sure you are safe. Getting hacked could be hostly, not just because you’ll have to clean up, but because a hacker could take advantage of any outbound calling lines you have and run up the bill.

Also, not having the right ports open has been reported to cause lack of sound.

Follow this guide (opens new window) on setting up your IP Tables, and we’ll carry on once you’re back.

CentOs IPTables Set Up and CentOS Start IPTables  (for reference and background reading)

Receiving Calls with Asterisk

The fun begins now. There are two ways we can place our first call with asterisk, using your SIP Phone, or not. I’m going to presume you’ve set up Asterisk but do not have a SIP Phone handy (just like me the first time I set up Asterisk).

Accepting calls from PSTN (real phones) on Asterisk

You set up a RX Host account right? We’re going to be using it now to place a call from a real phone to our RX Host number. The RX Host number will be answered by Asterisk and play us a message or some music.

If it’s prohibitively expensive for you to call a UK or international number (as provided by RX Host) then I will be covering set up using SIP Phones further down. So you can scroll down, or google some SIP providers that provide free numbers in your local area 😉

On your server you need to change into the asterisk directory:

cd /etc/asterisk

If you type the command ‘ls’ (without quotes) now, you should see a lot of .conf files. These are the files that we edit in order to let asterisk know how we want our calls handled, set up new extensions, voicemail boxes etc.

The first file we want to edit is called sip.conf.

vim sip.conf

Now I’m not presuming you know vim, because I didn’t when I first installed asterisk so here are some basic commands that will help.

  • Arrow buttons (up, down, left right) for moving the cursor.
  • Shift + up or down (scrolls up our down an entire ‘page’)
  • i – type the letter i when you’re ready to start inserting text or editing. Press esc to stop editing.
  • :q – quit without saving. (make sure you push esc if you were editing)
  • :w – write aka save
  • :wq – save then quit

The sip.conf file will have a lot of commented out text example text in it. It’s recommended you read through it to see what is possible (I haven’t). Somewhere after you see

[general]
context=default
allowoverlap=no

You want to insert the following to allow Asterisk to log in to your RX Host account.

[rxhost]
type=friend
host=rxhost.co.uk
fromdomain=rxhost.co.uk
fromuser=1234567 enter your numerical username here
callbackextension=1234567 numerical username again
defaultuser=1234567 numerical username again
context=rxhost_in
outboundproxy=rxhost.co.uk
qualify=yes
secret=password
insecure=invite
allow=g729
allow=gsm
allow=ulaw
dtmfmode=rfc2833

The fields are where you need to input data from your RXHost account. The numerical username is referred to as your SIP ID on RXHost website. Once you are logged in you need to click settings then overview to find your SIP password (NB: it is not the same password you use to log in to your RXHost user account).

Don’t forget to type :wq to save and close the file. Asterisk should now have enough information to log in to your RXHost account. (You can check asterisk registers successfully and I’ll show you how in a moment.)

A key line to pay attention to is the line that reads context=rxhost_in. This tells asterisk where to look next for instructions on how to deal with the call.

The next file we need to edit is the extensions.conf file.

vim extensions.conf

Again, this file will have some sample text in it. Find some space in the file and make sure you start inserting BEFORE a line that has text in square brackets. For example you would insert your text immediately before [demo] or whatever the line may read.

The text in square brackets defines a context (more on these later) and we’re going to define out own context now. Any guesses what it will be called? Did you say rxhost_in? Well, then you’d be right. Here’s what we have to insert:

[rxhost_in]
exten => 1234567,1,Playback(vm-goodbye)
exten => 1234567,n,Hangup()

Make sure you replace 1234567 with your RXHost SIP ID (aka username from the sip.conf file). That’s all that needs doing. Don’t forget to save and quit (:wq).

The sip.conf file tells asterisk to look at the context [rxhost_in] for details on how to handle the call. In this context, asterisk simply plays a file called vm-goodbye and then hangs up. You can’t dial your number just yet, but we’re nearly there.

Connect back to asterisk CLI (command line interface).

asterisk -r

You may need to type asterisk on its own first if it isn’t running. Now we need to let asterisk know we’ve updated some files and that it should reload them. Run these commands, one after the other:

sip reload

Reloads the sip.conf file

dialplan reload

Reloads the extensions.conf file

reload

Not strictly necessary but done for good measure. (You may see some errors and warnings from this one, but you can probably ignore them).

Let’s check if Asterisk is connected to rxhost properly.

sip show registry

If you see an entry for rxhost and the word Registered under state, you’re good to go. If not, you need to double check your sip.conf file and make sure your password etc are correct. Don’t forget to come back to the Asterisk CLI and reload the file before showing the registry.

Make your test call using your normal phone and dial your RXHOST number. 

You should hear “goodbye” and then be hung up on. Et voila, you have a working PBX!

You can also use Asterisk to make calls via your RXHost number, to either other RXHost or SIP users or to normal phones, although you’ll need to pay RXHost for credit. You can also set up with multiple providers to use the ones who give you the cheapest rate depending on where you’re calling.

Oh and for more Asterisk CLI commands you can type help or look here.

Making calls with Asterisk

Whether you set up Asterisk with RXHost or not, you can still have some fun. The real power of asterisk is providing an office like phone system where each user can have their own extension and voicemail. Calls can be passed to an auto attendendant (menu system e.g. push one for sales, two for billing) or and IVR (interactive voice response) which is more like when you call your bank and are asked to enter details like your date of birth or customer number and the system checks your details against a database before reading your balance. This is all possible with Asterisk.

Let’s start with adding some extensions. I’m hoping you’ve read the part above, so you’ll understand the editing of sip.conf and extensions.conf.

On your server, if you haven’t already:

cd /etc/asterisk
vim sip.conf

Where you’ve entered  (or would have entered) the RXHost entry directly below this you want to enter the following:

[phone1]
type=friend
defaultuser=phone1
nat=yes
host=dynamic
secret=abcdefg
regexten=1003
regcontext=phone1
context=rxhost_out
insecure=invite
canreinvite=yes
allow=all
[phone2]
type=friend
defaultuser=phone2
nat=yes
host=dynamic
secret=abcde
regexten=3001
regcontext=phone2
context=rxhost_out
insecure=invite
canreinvite=yes
allow=all

Make sure you change the secret to something secure. Save the file and quit.(:wq).

You can also see that the context on both phones we’ve registered here is set to rxhost_out. This means that when either of these phones are connected to asterisk, it will look in extensions.conf at [rxhost_out] to determine how to hand the digits dialled. So let’s add something to extensions.conf.

vim extensions.conf

Underneath the entry we made for [rxhost_out] (or where you would have made that entry) add the following:

[rxhost_out]
;this context is assigned to phones/users so that they may call
;9 should be used to make a call out via this RXHost line.

exten => _90[1-9].,1,Set(CALLERID(num)=rxhost_number_here)
exten => _90[1-9].,n,Dial(SIP/${EXTEN:1}@rxhost,30,trg)
exten => _90[1-9].,n,Hangup

;here come general extensions for all users.

;first is to record some messages.
exten => 3003,1,Answer()
exten => 3003,n,Wait(2)
exten => 3003,n,Record(/var/lib/asterisk/sounds/recordings/recording.gsm) ; # to stop recording.
exten => 3003,n,Wait(2)
exten => 3003,n,Playback(/var/lib/asterisk/sounds/recordings/recording)
exten => 3003,n,Wait(2)
exten => 3003,n,Hangup

;3001 to dial phone 1
;tmpA(beep) performs call screening
exten => 3001,1,Dial(SIP/phone1,18,tmpA(beep)
exten => 3001,n,Hangup()

; 3002 dials phone 2
exten => 3002,1,Dial(SIP/phone2,18)
exten => 3002,n,Hangup()

The comments in the dialplan should explain what is happening clearly. If user on phone1 or phone2 dials 9 followed by a number, asterisk attempts to place the call via RXHost. If the user dials 3001 or 3002, asterisk attempts to call the relevant phone. Dialling 3003 allows you to record your voice and save a file, this will come in useful later when we record our own menu system.

Now for this to work you need to set up your hardware or soft phones. All you should need is the IP address of your server running asterisk and the username e.g. phone1 or phone2 and the corresponding password (the secret field from sip.conf).

Don’t forget to connect to the asterisk CLI and reload everything.

asterisk -r
sip reload
dialplan reload
reload
sip show users

This last command should show all registered phones. If your phone1 or phone2 is not listed then you need to double check your settings. I believe on x-lite you can set up two accounts on one device. If you’re all set up you can dial either 3001 or 3002 and see if you can call the other phone. You can also dial
910005 (9 for RXHost line) and 10005 being the RXHost echo test. Where you should be able to speak and hear yourself. Alternatively you can dial 3003 to record your voice and have it played back via the asterisk system.

To be continued

I didn’t realise it would take this long to write this guide. There’s lots more I’d like to share, so I this post will be followed up by others. If you liked it or have any comments or questions please leave them below and feel free to tweet and share this post too!

The Definitive Guide

Within the asterisk community there seems to be one book that everybody loves. I’ve had to check it out and found it VERY informative. If you have any interest in playing with asterisk a lot I recommend you pick up a copy of Asterisk: The Definitive Guide.