Hack 99. Turn Your Linux Box into a PBX
Install and test the Asterisk open source telephony server on your Linux PC.
Asterisk is open source software to turn a Linux computer into a Voice over IP Private Branch Exchange (PBX). As a PBX it can be the backbone server to provide telephone service to hundreds of office telephones or a call router and voicemail server for an especially geeky individuallike you. As VoIP is the future of telephony, playing around with an Asterisk server is a good way to get your toes wet in this exciting new field, and this hack shows you how to get it installed.
Some RPM packages are available to simplify Asterisk's installation, but manual compilation is relatively easy. So I'm going to show you how to download, compile, and install Asterisk the "old-fashioned" way. The development branch you'll download from is stable, though once you get comfortable with Asterisk you'll want to jump out on the bleeding edge and try the developer releases, too. Each release tends to introduce something new and worthwhile, even if it's not in the stable branch yet.
The easiest place to download the Asterisk software is the CVS repository at Digium, the company responsible for Asterisk and some of the hardware components that work with it. To access the CVS repository, you'll need to be logged into your Linux computer at a shell prompt as root. Type these commands to run the CVS check-out routine and download the source code:
# cd /usr/src # export CVSROOT=:pserver:firstname.lastname@example.org:/usr/cvsroot # cvs login # cvs checkout zaptel libpri asterisk
Alternatively, you can specify a particular version of Asterisk:
# cvs checkout -r v1-2 zaptel libpri asterisk
When prompted, use anoncvs as a password. If you don't use /usr/src as the local location for compiling programs, substitute the appropriate path. The CVS client you're running here will create the /usr/src/asterisk directory that contains all the Asterisk source code. Once the download completes, you are ready to begin compiling.
Asterisk consists of several software components for Linux. Not all of these packages are required, as some of them are drivers for Digium's interface cards. If you aren't planning to use Digium's cards, you'll need to build only the last of the three, asterisk:
If you're wondering about these technical terms, don't worry. As you experiment with Asterisk and learn more about VoIP, they'll become very familiar. For now, just compile and install all three of them.
Since you ran the CVS download, the source code for each Asterisk software component is sitting in its respective directory in /usr/src. Let's compile each software component by issuing the following commands. Again, you need to compile zaptel and libpri only if you're planning on using legacy or Digium interface cards. Many of the examples in this book use legacy devices, so it's probably a good idea to compile them all right now. Here is the sequence of commands:
# cd zaptel # make clean ; make install # cd ../libpri # make clean ; make install # cd ../asterisk # make clean ; make install
It should take 20 minutes at most to complete the whole build on an average PC. Once built, Asterisk is ready to use. But you can't race this Ferrari without a training lap on the test track, and you can't really use Asterisk until you understand the basics of configuring it. So it's time for driving school. To get started, run this command in the Asterisk source directory:
# make samples
This creates a basic sample set of Asterisk configuration files and places them in /etc/asterisk. You might want to peruse these filesespecially extensions.conf and sip.conf, where you'll likely be spending a lot of time.
If you've used an RPM package or some other precompiled Asterisk distribution (or if you've obtained a Linux distribution with Asterisk already installed), you can still obtain the source distribution files from Digium's CVS repository and issue only the make samples command. This will give you the sample configuration files without actually rebuilding Asterisk on your PC.
5.12.1. Start and Stop the Asterisk Server
The Asterisk program has two modes of operation: server mode and client mode. The server is the instance of Asterisk that stays running all the time, handling calls, recording voicemails, greeting callers while users are away, and so on. The client is the instance of Asterisk that allows you to monitor and manipulate the server while it runs. The mode the program uses depends on how Asterisk is invoked at the command prompt or within a shell script.
To launch Asterisk in server mode, execute this command:
# asterisk -vvv &
The more v's, the more verbose Asterisk's console output will be.
To connect Asterisk in client mode on the local machine already running in server mode, execute this command:
Once the Asterisk client is connected to the Asterisk server, you can use Asterisk's command-line interface to issue queries and commands about the telephony server. These include listing calls in progress, listing used and unused channels, and stopping the Asterisk server.
You can shut down the server using one of several Asterisk CLI commands:
The restart commands stop and then restart the Asterisk server process, which can be helpful in situations where the server's configuration has changed significantly and needs to be restarted. The stop commands just shut down the Asterisk server process. You'll have to execute the Asterisk program in server mode to get it running again.
The now and when convenient arguments tell Asterisk how quickly to shut down or restart. If you want to interrupt the current calls and tasks in progress on the server, now is appropriate. If you want Asterisk to wait until all the calls and tasks are finished and there is no call activity at all, when convenient is appropriate. Generally, especially if you're planning to have any callers besides yourself on the system, get in the habit of using when convenient.
5.12.2. Linux-Specific Start and Stop Scripts
Depending on your particular flavor of Linux, be it Fedora, Debian, SuSE, or something else, you'll find your system's normal startup scripts in a place that's unique to each flavor. Fortunately, Asterisk's makefile has an option that lets you automatically generate start and stop scripts that are specific to your flavor of Linux. In your Asterisk src directory, just issue the command make config, and the scripts will be installed. These scripts start and stop not only Asterisk, but also the Zaptel drivers, if you've compiled them.
As it stands at this point, your Asterisk server won't be especially useful. You'll be able to explore the Asterisk command prompt with asteriskr, but the truly fun stuff, like hooking up phone lines and phones, is still to come. To try out Asterisk's cool demonstration routineslike interactive voice response (IVR) and an Internet-based VoIP callyou've got to configure a phone of one sort or another to access the Asterisk server. I explain how to do this in my book VoIP Hacks (O'Reilly), but if you want to get started quickly and don't have a VoIP hardphone (a physical phone), then you should try connecting to your server with the X-Lite softphone, available at http://www.counterpath.com, or any one of several other softphones. Unfortunately, because Skype uses a proprietary protocol, you won't be able to connect to your Asterisk server using this popular cross-platform VoIP program.