Simple Asterisk VoIP on a hosted server

I’ve been playing with Asterisk for a long time, mainly as a hobby and mostly just hacking things together. For the past couple of years I went the easy route and used Asterisk@home (now Trixbox), which allows out of the box install on a server and an adequate interface for setup. It worked very well, but about a month ago I got sick of the old server humming in my office. I decided to do a bare-bones Asterisk install on one of our hosted Debian servers.

Quick intro on Asterisk

If you are not familiar with Asterisk, you should be. It’s an extremely flexible and powerful VoIP PBX system. Think of it as an open-standard Skype for business. Our team at Wildbit is virtual, but we can easily have extensions and US dialing anywhere, as long as a VoIP phone is connected to the Internet. For instance, I could dial extension 102 and talk to Ivan (in St. Pete, Russia) as if he was in the office next to me, without any charge for the call. The system is almost limitless, which is why I have been so excited about it for so long. I say “almost” because the SIP protocol is not perfect on NAT.

So, if you are interested in setting up a cheap, extremely reliable phone system, i’ll explain how you can get started, including:

  • Install Asterisk on a public server with phone behind a NAT router
  • Configure two internal phone extensions
  • Configure and setup voicemail for each extension
  • Setup outgoing/incoming trunk for regular phone calls (with Telasip)
  • Bring it all together with an IP phone

Installing Asterisk

Asterisk installation is pretty straight forward. Instead of getting into the details, just check out this tutorial. Follow all of the steps except the last, which is to make samples. We’ll simplify this later.

Select a VoIP provider

Asterisk can interact with many VoIP providers. We chose Telasip for their reliability, great prices, and ability to set caller ID manually. In order to use Asterisk for regular phone calls, you will need a VoIP trunk such as Telasip. Any of the plans listed on their site will work well.

Configuration files

To make things easy, I created a simple set of config files that you can upload. We’ll use these config files to explain the basic settings in Asterisk.

Download the files and upload them to the /etc/asterisk/ directory.

There are several files in this packages, but only three of them matter for customization.

  • sip.conf: Defines extensions and general asterisk settings
  • extensions.conf: Defines dial routes and plans for extensions
  • voicemail.conf: Defines voicemail settings and mailboxes

Setup your trunk and extensions

The first step is to setup your extensions and your Telasip trunk. Open up the sip.conf file. You should see some general settings at the top.

[general]
bindport = 5060
bindaddr = 0.0.0.0
disallow=all
allow=ulaw
allow=alaw
allow=h263
allow=h263p
videosupport=yes
externip=0.0.0.0
externhost=domain.com
context = from-trunk
sendrpid=yes
register=user:pass@gw4.telasip.com

Most of the items in the file can stay the same, but you will want to change externip to your public IP, externhost to your domain for that IP (optional), and user:pass to the username and password for your Telasip account. If you are curious what each option does, I inserted comments to explain each option. The section just defines your Asterisk server settings and registers it with Telasip.

Next you will see some extensions that look like:

[101]
username=101
type=friend
secret=1111
record_out=Adhoc
record_in=Adhoc
port=5060
host=dynamic
nat=yes
mailbox=101@default
dtmfmode=rfc2833
context=from-internal
canreinvite=no
callerid=Name <101>

The code above is copied for each extension, where [101] is the number of the extension. All you have to change here is the secret (your password in digits) and callerid. If you want to add more extensions just copy the code and change 101 to 103, 104, etc. The line context=from-internal is very important here, because it defines where in the extension.conf file to route calls. In the sample file, I included two extensions: 101 and 102.

Next you need to setup the Telasip trunk, which is basically an extension, but for routing incoming and outgoing calls.

[telasip-gw]
username=username
type=peer
secret=password
insecure=very
host=gw4.telasip.com
fromuser=username
dtmfmode=rfc2833
context=from-trunk

Everything here can stay the same, except for username and secret (your Telasip password). Again, the line context=from-trunk is very important, because it defines where to look in the extensions.conf file for incoming and outgoing calls.

That’s it for setting up extensions. Now you need to setup some mailboxes for each extension.

Configuring voicemail

Open up the voicemail.conf file. The file is self explanatory, but the important part is at the bottom where we define each extension's mailbox.

[default]
101 => 1234,Your Name,email@domain.com
102 => 1234,Another Name,email@domain.com

These two small lines define the mailboxes for the two extensions. The format has the following structure:

extension => password,full name,email for notifications

If you decide to add more extensions later, just create a new line and change the information.

Configuring dial plans and calling

Now that we have extensions, a trunk, and voicemail we need to tell Asterisk what to do when someone makes a call or dials a number. This is where the madness begins, because the options are endless.

Open up the extensions.conf file and skim through it. We have two main sections: from-internal and from-trunk. Remember these from above? From-internal defines what happens when calls are dialed internally. From-trunk defines what happens when a call comes in from Telasip.

First, let me explain some of the logic behind the command, because they are all similar. All of them look something like this:

exten => extension,priority,command or exten => s,1,Dial(SIP/101, 15)
  • Extension: This defines the extension that is dialed or matched. Such as dialing 101.
  • Number: The next is the priority, which is the order in which commands are parsed (1,2,3, etc)
  • Command: This is the action, such as dial, send to voicemail, hangup, play music, and so on.

These three simple variables provide most of the power behind Asterisk. To learn more about the settings read this entry on Voip-Info.

For our example, let’s start with the from-trunk section. This is how we handle incoming calls. You can see it looks something like this:

exten => _2155551212,1,Dial(SIP/101, 15) ;Send the call to ext 101 and rings for 15 seconds
exten => _2155551212,2,VoiceMail(101@default,su) ;Sent to voicemail with unavailable message
exten => _2155551212,3,PlayBack(vm-goodbye)
exten => _2155551212,4,HangUp()
exten => a,1,VoiceMailMain(101) ;If caller dials * then he can check the voicemail for ext 101
exten => a,2,Hangup()

The number is the extension variable would be your number with Telasip. The series of commands basically says that when an incoming call comes in on Telasip, dial extension 101. If after 15 seconds no one answers, send it to voicemail with the unavailable (su) message. The voicemail is referencing the mailbox we setup above (101@default). After that, say goodbye and hang up the connection. There is an additional extension labeled as a. This is a custom command to tell Asterisk what to do if the caller dials the * key. This is useful if you want to call your own number and check voicemail from another phone. With this section, you can now receive incoming calls.

Now, what about outgoing calls? For outgoing calls we move down to the from-internal section. This defines all calls going outside of Asterisk and any calls within asterisk. Let’s start with making outbound calls through Telasip.

exten => _1NXXNXXXXXX,1,Set(CALLERID(all)=Chris Nagele <12152030488>) ; Set the calledID. Must use sendrpid=yes in sip.conf
exten => _1NXXNXXXXXX,2,Dial(SIP/telasip-gw/${EXTEN},,r) ; Dial the number through the telasip trunk
exten => _1NXXNXXXXXX,3,congestion() ; No answer, nothing
exten => _1NXXNXXXXXX,102,busy(); Busy

You can use the extension parameter to match dialing patterns. In the example above, we are matching US seven digit dialing preceded by a 1. If you look in the file, you will also notice a pattern for 7 digit dialing and international dialing (_011.). When someone dials a number from an extension that matches the pattern, it will run these commands. The first sets the caller ID, which is incredibly useful for making outbound calls. Telasip allows you to spoof this any way you want. Next, it dials through the telasip-gw extension with the number entered. ${EXTEN} is just a variable that matches the defined extension (_1NXXNXXXXXX). That’s it. You can make outgoing calls.

Now that we can make and receive calls from the outside world, we will setup some internal dialing plans between extensions, such as 101 and 102.

Internal extensions are setup almost the same way as the from-trunk extension, except we will match an internal extension. The dial plan look like this:

exten => _XXX,1,Dial(SIP/${EXTEN}, 15) ;Dials the extension
exten => _XXX,2,Voicemail(${EXTEN},su) ;Sent to unavailable vm
exten => _XXX,102,Voicemail(${EXTEN},sb) ;If busy, plays busy vm
exten => _XXX,1,VoiceMailMain(${EXTEN}) ;Dial * to listen to vm for this extension
exten => _XXX,2,Hangup()

This does almost the same thing as the from-trunk command, but instead we are matching a three digit extension (_XXX).

Lastly, setup an easy way to access voicemail. The following lines will allow you to dial *97 from your phone to access voicemail. It will automatically detect your extension with the callerid variable.

exten => *97,1,VoicemailMain(${CALLERID(num)}) ;Matches the extension dialed from for vm
exten => *97,2,Hangup

Bring it together with an IP Phone

Now that you have edited all of the files it is time to upload them to the server where you installed Asterisk. Upload the entire package of files to the /etc/asterisk/ directory. From the command line, start asterisk with the command asterisk. If Asterisk is already running, type asterisk -r and then type reload to reload the configuration.

The best way to test your setup is with a softphone. The best softphone for Mac, Linux, and Windows is X-Lite. Go ahead and download the softphone. When it is installed it will ask you to setup the account information. The account information is based on your extensions in the sip.conf file.

Username: 101

Password: your password

Authorization: 101 (same as username)

Domain: IP or Domain of your server

When you are done. The phone should register with Asterisk and display your extension and your name. You can test it out by dialing a landline number or you can setup a second soft phone on another computer with extension 102 and dial the extensions directly. You should also be able to dial to extension 101 from a landline when dialing your Telasip number.

If all works well, you can setup your voicemail by dialing *97 and following the instructions.

What’s next?

To me, there is nothing better than a good hard phone. I have tried a bunch and nothing is better than the Aastra 480i-CT so far. There are literally hundreds of phones that you can use with Asterisk, but not all of them handle multiple extensions, transfers, and conference calling. Check out VoIP Supply for more options.

Asterisk is kind of addictive, but it has a steep learning curve as well. In my next article I am going to explain how to setup Music On Hold and an Auto Attendant for friendly answering and extensions.

If you have questions or problems, post a comment. I will respond quickly.

Comments

Our products