29 Aug, 2008
Installing Asterisk on Ubuntu Server 8.04 (Hardy Heron) + Configuring a Grandstream GXP 2000 behind a network (i.e. NAT’ed) to receive incoming and make outgoing calls via SIP (phone->PBX, inbound->PBX) and IAX2 (PBX->outbound)
Posted by: Henri Cook In: Asterisk| General Ramblings| Technical Support| Ubuntu
Wow, possibly the largest title ever. This is something that i’ve done fairly recently, I wanted a phone for my business that wasn’t a mobile and my options appear to be costly forwarding to my mobile (when i’ll probably be near my PC anyway) or getting calls routed to a home phone line (which would be no good when i’m out and about).
My solution is using a VPS (Virtual Private Server) running Ubuntu 8.04 (Hardy Heron) as a Private Branch Exchange (PBX) using Asterisk – the amazing open source PBX.
Before I guide you through the (shockingly simple) setup, a word on where you can find support when stuck:
Asterisk and Ubuntu both run channels on irc.freenode.net – #asterisk and #ubuntu respectively – the people in these channels are friendly and immensely helpful. Make sure you’re read the manual first.
There is an Asterisk book available online or from Amazon which is an amazing read, especially if you’re completely new to VoIP technology like me. I ordered a paper copy, worth it.
Ubuntu is something I can’t really describe the setup of here, if you don’t know how to configure a basic linux server system you probably shouldn’t be attempting things described here. Read on if you’re just curious!
The things required for such a system are:
- A VPS with a public-facing IP of its own (NAT’ed servers are not covered in this guide)
- An inbound and outbound SIP/IAX2 trunk provider. For this project I used Gradwell.com on a recommendation from a friend. I purchased one inbound SIP trunk for £3/mo and one outbound IAX2 trunk for £25 one off setup free (you can use one outbound trunk for multiple calls).
- A Grandstream GXP 2000 IP Hardphone, the cheapest I found it was at soho66.co.uk
————————————————–
Installing * (Asterisk) on Ubuntu 8.04
This couldn’t be more simple, just run:
apt-get install asterisk asterisk-mp3 asterisk-sounds-main asterisk-sounds-extra
… and the magic that is apt will take care of the rest.
You’ve just installed asterisk and some mp3 reading tools for Asterisk which you might need later as well as some pre recorded sound files in many encodings that come with the Asterisk (*) source package.
Configuring Asteriskb
All of the configuration files we need are in /etc/asterisk – namely sip.conf – where SIP clients are configured and routed to extension contexts and extension.conf where those contexts are configured.
My final sip.conf looks like this:
[general]
context=default
allowoverlap=no
bindport=5060
bindaddr=0.0.0.0
srvlookup=yes
disallow=all
allow=ulaw
; Phone 0
[Ph0]
type=friend
host=dynamic
secret=phonesecretpassword
context=outbound-myconn
nat=yes
qualify=yes
canreinvite=no; SIP Provider
[gradwell]
type=peer
host=voip.gradwell.net
user=gradwell
fromuser=gradwell
secret=gradwellsecretpassword
canreinvite=no
A context is fairly obviously denoted by [contextlabel] and everything under a tag is part of that context until a new tag is encountered.
Gradwell is configured as a connection type (peer) that can receive incoming calls via SIP – using the username and secret (passwod) that you configured when signing up at Gradwell.com
My Grandstream is called ‘Ph0′ (you can call it whatever you like) and the ‘friend’ type means that it can both receive incoming calls and make outgoing calls via SIP (effectively a combination of ‘peer’ and ‘user’ where user can make outgoing calls and peer can receive them only)
From these configuration options for Ph0 then any incoming connections with the appropriate username and secret are routed to extension context ‘outbound-myconn’ which is setup in extensions.conf. The options at the end say that this client is NAT’ed and to take the appropriate measures. These three lines (nat, qualify, reinvite) are all you need to enable NAT support for a client (phone). Look the directives up for more information on what they do.
When you configure your Grandstream (use the web interface) you will specify the username and secret specified here (for Ph0) – they all call them different things so use your street smarts (password == secret, SIP Auth Name == username etc.)
Lets look at extensions.conf:
[general]
static=yes
writeprotect=no
autofallthrough=yes ; important
clearglobalvars=no
[inbound-myconn]
exten => 08458621487,1,Ringing
exten => 08458621487,n,Goto(hc,s,1)[hc]
; Desk Phone
exten => s,1,Dial(SIP/Ph0, 12)[outbound-myconn]
ignorepat => 9
exten => _9.,1,Set(CALLERID(all)=08458621487)
exten => _9.,n,Dial(IAX2/myusername:secretpass@iax-lb.gradwell.net/${EXTEN:1})
exten => _9.,n,Congestion()
exten => _9.,n,HangUp()
This is a very simple set of extensions and there is a *lot* more you can do, i’m sharing my configuration with you but read the book if you want to do things that are different.
NB: The phone number shown here is not my own, it is random – please do not call it.
Lets deal with inbound-myconn first which automatically matches any attempts to ‘route in’ a number – 08458621487 – the routing was configured when you signup up with Gradwell – you have the option of something like sipuser:password@yourserver/routenumber (e.g. gradwell:secretpassword@myserver/08458621487) or you can choose just the extension or something custom.
It’s a list of ‘priorities’ or an order of commands if you will, priority 1 must always be there but you can add magic auto-incrementing priorities by using ‘n’ after that. The inbound-myconn context then first matches the 08458621487 pattern, then as a matter of top priority starts ringing (sends the ringing sound to the user). Next in the list (n effectively gets replacted with 2 (last priority + 1)) sends the call to the ’s’ pattern, priority 1 in context ‘hc’
This context simply dials the SIP client Ph0 as a matter of top priority. The 12 specified here says that it will stop ringing after 12 seconds of no answer – this option can be removed for default or tailored to your needs. Eventually I want to implement a voice mailbox when the phone rings for >12 seconds. After this ‘ring-out’ there are no further instructions so the connection is terminated (hung up).
If the hardphone has been configured proprely and made all the appropriate connections (which wasn’t hard for me, just set the usernames and secrets for Ph0 in ‘account 1′ and the same server address for incoming/outbound and you’re on the way (do this after you’ve configured the server).
Making Phone Calls
Lets look at the outbound-myconn context. The first line tells the context to ignore a ‘9′ as a pattern in its own right. e.g. ‘Press 9 for an outside line’ – you can do things with internal extensions according to your needs but I haven’t touched those yet.
Again we see a priority list, exten => <pattern>, <priority>, <action>
the pattern is ‘_9.’ – i.e. 9 followed by anything in regex-like syntax, you could use pure integer matching here for precision
Top priority, set a caller ID – might as well use my 0845 number so people can call me back properly!
Next is contact the IAX2 outbound trunk at gradwell user:secret@gradwell/numbertodial – here we use ${EXTEN:1} – the one means start from the 2nd character (stripping out the 9 you used to get an outside line)
The Congestion() function plays a fast busy tone, in case the Dial() is unsuccessful, then hangs up after the default timeout. If the connection is successful obviously you control the hangup yourself and the process is terminated.
Debug
After you’ve done this make sure Asterisk is stopped (/etc/init.d/Asterisk stop) and then start it in debug mode – from a root shell:
asterisk -vvvvcg
then type
’set verbose 10′
’set debug 3′
and try to place some calls to yourself, register your hardphone, and make calls – you should see loads of debug information here.
If for any reason your GXP isn’t registered you can debug SIP communication here by typing ’sip debug’ (spam warning).
That’s it! You’ve now got a working PBX on a very basic level – there is a *LOT* more you can do, get the book, read it – it’s worth it!
Further Work
I mentioned that I wanted a solution that was better than a simple (and expensive) redirect to landline, eventually I plan to:
- Have both my hardphone and my mobile ring simultaneously when my 0845 is called (using Asterisk of course)
- Voicemail is neither location picks up
- Perhaps an automated prompt, but they bug me