Friday, 1 June 2012

Setup And Run Asterisk and FreeBPX on A Raspberry PI

Finally recieved the Raspberry PI on Friday (25th May 2012) after a two and half year wait! My plan, which I had all that time to think about, discuss with others and have ready is still not concrete but initially, I have order 3 devices to play with.

As a Asterisk Engineer, one of the first things I wanted to tackle was to install the latest Asterisk and FreePBX on it. I build Asterisk based PBX systems for a living, so this shouldn't be that difficult. I responded on http://www.raspberry-asterisk.org/ to say I was going to attempt this and report back but so far the Admin has not got back to me on how I can add there so I have written this up here.



This guide is for complete newbies in getting the Raspberry PI to get set-up and run as a IP PBX using Asterisk and FreePBX
 

I intended to install Asterisk and FreePBX 2.9 (this is a matter of choice as I am accustomed to the layout before 2.10). I purchased a 8GB SD card instead of 2GB, which is sufficient for just Asterisk 1.8 but not enough for FreePBX. Here's the install process....

Install Squeeze - Get The PI Ready
Anyone with a Raspberry PI in their hands should first go though the following process and set up Debian Squeeze. For my setup, I had an 8GB SD Card. I downloaded Debian Squeeze for the PI from here and the Win32DiskImager from here, Stuck my SD card in to my windows laptop and fired up Win32 Disk Imager. Next, Unzipped the debian file and browsed to it in Win32 Disk Imager, select the correct drive letter in the drop down and let the imaging start. This is by far the quickest and dirtiest way to get Squeeze on to the SD.

After completion, I launched a Live CD of GParted, which is an amazing tool to partition disks. I increased the swap partition to 300Mb and moved it to the end of the drive. I then rezised the rootfs partition to used all the unallocated space on the drive.

Alternatively, you can follow the information on this great guide to go through and set up the drive properly and learn how to do the GParted stuff I mentioned above. 

Internet Access on the Raspberry PI

If you connect the PI to your local network which has a ADSL/CABLE router or mode, then it should have DHCP on. DHCP will provide it an IP address and this will be displayed on-screen when the PI boots up before the log in screen. If it doesnt, you need to get an IP and add this in manually. To do this, log in to the PI using the default username: pi and password: Raspberry and type the following command:

sudo vi /etc/network/interfaces


Using VI : Edit the file using by pressing 'a' to insert text. Once editied, press escape to come out of editing mode.

The IP parameters should be as follows with your specific IP address information:

iface eth0 inet static
address 192.168.1.100
gateway 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255


NOTE: Dont forget to comment/remove line stating:  

iface eth0 inet dhcp

Additionally you will need to set up DNS in order to get out on the internet. This setting should be done in the resolve.conf file as follows:

sudo vim /etc/resolv.conf

This should say something like:

nameserver 8.8.8.8
nameserver 192.168.1.1

to test network connectivity, at the command line, type in

ping <your favourite serach engine.com>

Press CTRL+C to stop pinging.



Update Asterisk Install Pre-requisites

To start with, update the system and install the required componants by copying the command below:

sudo apt-get update

sudo apt-get -y install make gcc g++ libxml2 libxml2-dev ssh libncurses5 libncursesw5 libncurses5-dev libncursesw5-dev linux-libc-dev sqlite libnewt-dev libusb-dev zlib1g-dev libmysqlclient15-dev libsqlite0 libsqlite0-dev bison openssl libssl-dev libeditline0 libeditline-dev libedit-dev mc sox libedit2 libedit-dev curl libcurl4-gnutls-dev apache2 libapache2-mod-php5 php-pear openssh-server build-essential openssh-client zlib1g zlib1g-dev libtiff4 libtiff4-dev libnet-telnet-perl mime-construct libipc-signal-perl libmime-types-perl libproc-waitstat-perl mpg123 libiksemel-dev php5 php5-cli mysql-server php5-mysql php-db libapache2-mod-php5 php5-gd php5-curl mysql-client vim

Dowload and Extract Asterisk

To Download and Extract asterisk, enter the commands as below:

cd /usr/src 
sudo wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-1.8-current.tar.gz 
sudo tar xvfz asterisk-1.8-current.tar.gz 
cd asterisk-1.8*

Set Up Correct Compilation For ARM Platform
Using your favorite editor, make the change below. I prefer VIM, an enhanced version of the popular VI command line editor:

sudo vim makeopts.in

Search for the word "proc=" in the file and change this to read "proc=arm". For those new to VIM, move to the end of this line using the arrow keys and press 'a', then remove all the chractors before the = sign using back space and type in 'arm'. Finally press escape to come out of editing mode. Then press SHIFT+Z+Z to save and exit the file.

Compile and Install Asterisk
Using the following commands:

sudo ./configure
sudo make
sudo make install
sudo make samples
sudo make config


Finally, restart the PI by typing the fillowing command:

sudo init 6

Congratulations - Asterisk installed is now installed



To test the install, log in to the PI and type the following command:

sudo asterisk -r



If this all works, great...lets move on to installing FreePBX.

Create And Setup Asterisk Users And Groups Permissions

sudo useradd -c "Asterisk PBX" -d /var/lib/asterisk asterisksudo chown -R asterisk:asterisk /var/run/asterisksudo chown -R asterisk:asterisk /var/log/asterisksudo chown -R asterisk:asterisk /var/lib/php/session/


Install Pear and DB
FreePBX uses the php pear repository and specifically requires the database repositor. Enter the commands below to install them:

sudo apt-get install php-pear
sudo pear install db


Download And Extract FreePBX

To Download and Extract FreePBX, enter the commands as below:

cd /usr/src/
sudo wget http://mirror.freepbx.org/freepbx-2.9.0.tar.gz
tar zxvf freepbx*



Start The MySQL Server And Import In The FreePBX Data
Lets start the database server and import in the tables FreePBX will require using th following commands:

sudo service mysql start
cd /usr/src/freepbx*

sudo mysqladmin create asterisk
sudo mysqladmin create asteriskcdrdb
sudo mysql asterisk < SQL/newinstall.sql
sudo mysql asteriskcdrdb < SQL/cdr_mysql_table.sql
sudo mysql





Lets add the mysql user and give it the correct permissions under the MySQL prompt:

GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO asteriskuser@localhost IDENTIFIED BY '******';
GRANT ALL PRIVILEGES ON asterisk.* TO asteriskuser@localhost IDENTIFIED BY '******';

flush privileges;

exit
NOTE where I have put '******', replace with your own password. Please keep a log of this as it will be required later. The password should secure (aplhanumeric with symbols) and in single speech marks as seen above

Now lets secure MySQL with a root password of your choice (See note above).

sudo mysqladmin -u root password '******'


Restart Asterisk With Correct Permissions

cd /use/src/freepbx*
sudo service asterisk stop
sudo ./start_asterisk start


Set The Correct Timezone for PHP
Open the PHP Configuration file:

sudo vim /etc/php5/apache2/php.ini

Under Module Settings, change the timezone accordingly:

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;


[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone


Uncomment date.timezone and set to the correct setting according to the link above. In my case, this was as follows:


date.timezone = Europe/London


To do this, move over the first charactor in the line, press 'a' the backspace to uncomment the line. Then move to the end of the line and type in your timezone after the '='. Press escape to come out of editing mode and save the file using the SHIFT+Z+Z combination.


Setup Apache For FreePBX and Asterisk
Apache user and group will need to be set to asterisk. To do this, run the following command to open the Apache configuration file:

sudo vim /etc/apache2/apache.conf

Find the line that states:

User www-data
Group www-data



Change these to read:

User asterisk
Group asterisk

For those new to VIM, move to the end of this line using the arrow keys and press 'a', then remove all the charactors before the user or group using back space and type in 'asterisk'. Finally press escape to come out of editing mode. Then press SHIFT+Z+Z to save and exit the file.

Additionally, change the default sites settings by:

sudo vim /etc/apache2/sites-available/default

Under tags "<Directory />" and "<Directory /var/www/>" change the following settings:



Options FollowSymLinks
AllowOverride All



For those new to VIM, follow the origional instuction on editing above using the 'a' to go in to editing mode and escape key to come out of editing mode. Remember to save the file using SHIF+Z+Z.

Finally restart apache using the following command:

sudo service apache2 restart



Install FreePBX
To install FreePBX, you will need to specify the username and password to the database we setup earlier, start by typing in the following command:

sudo ./install_amp --username=asteriskuser --password=******

For each of the prompts, just hit enter except when prompted for

Enter the path to use for your AMP web root:
 [/var/www/html]


in this case type the following:

/var/www

Then hit enter. You should see something like this :

Checking for PEAR DB..OK
Checking for PEAR Console::Getopt..OK
Checking user..OK
Checking if Asterisk is running..running with PID: 2591..OK
Checking for /etc/amportal.conf../etc/amportal.conf does not exist, copying default
Creating new /etc/amportal.conf

Enter the hostname of the 'asterisk' database:
 [localhost]

Enter a USERNAME to connect to the Asterisk Manager interface:
 [admin]
Enter a PASSWORD to connect to the Asterisk Manager interface:
 [amp111]
Enter the path to use for your AMP web root:
 [/var/www/html] /var/www

Enter the IP ADDRESS or hostname used to access the AMP web-admin:
 [xx.xx.xx.xx]
Enter a PASSWORD to perform call transfers with the Flash Operator Panel:
 [passw0rd]
Use simple Extensions [extensions] admin or separate Devices and Users [deviceanduser]?
 [extensions]
Enter directory in which to store AMP executable scripts:
 [/var/lib/asterisk/bin]
Created /var/lib/asterisk/bin
Enter directory in which to store super-user scripts:
 [/usr/local/sbin]


One this is complete, lets set the file permissions for FreePBX to be accessable to apache users:

sudo chmod 777 /var/www/* -R

Finally Restart all services
Although in a lot of the cases, service restart is not required, I have had issues in previous versions of FreePBX, therefore as a final step, restart all the service to ensure they start with the correct permissions:

sudo amportal stop
sudo service apache2 stop
sudo service mysql stop

sudo service apache2 start
sudo service mysql start
sudo amportal start

Congratulations - Free PBX is now installed.

 To test the install, open your favourite browser on another PC and browse to the address of your PI


35 comments:

  1. This is really brilliant! Can you also provide some info about what is the system's perfomance and limitations? What is a hypothetic senario - setup? Can it be used for a really small SOHO? These are the best news for asterisk enthousiasts!! Keep on!

    ReplyDelete
  2. Hi Athanasios1,

    Thank you for your comment. Tackling perfermance was not a goal for me (this is a whole new blog post later) at this stage however, in order to do anything useful with a Raspberry PI will require a fair bit of patience.

    On opening Midori browser and bringing up FreePBX dashboard, the CPU can be seeing to hit 100% (memory at 72%) for a number of seconds. Doing anything in FreePBX takes an age, however, browsing to it on a remote machine with no desktop loaded on the PI is much better. We have managed 3 successful simultanious SIP calls with no degradation in call quality. I cant see why this cant push out 10 calls although this, we have not tested as yet.

    Using the PI as a SOHO PBX, i wouldn't see any problems with. I would however recommend you setup you extensions and dialplan, then shut down apache2 and mysql to ensure maximum CPU and memory is available for the Asterisk process.

    ReplyDelete
    Replies
    1. Hey thanks for your answer!I just installed asterisknow in a VM machine with 256MB ram in order to start playing with it and successfully paired a softphone..RAM usage with that doing nothing is 83%.
      This is a really good trick..I will try to shutdown these two processes..
      waiting for further info from your testings! Yay!!!

      Delete
  3. Ok

    So first off thanks for taking the trouble to do this but I am experiencing some problems following in your footsteps. I should add that I am truly a complete newbie so please bear with me.

    I can set the SD card up (I chose a 32 gig card) and get the Pi running including the Gparted bit. My network uses DHCP and that works, my problems begin with the "Update Asterisk Install Pre-requisites" section. I assume that this command is actually two commands as the Pi won't let me enter the command as shown however if I split it at the second sudo it will run both bits seperately. Is this right?

    When I run the second part I get a problem flagged with apache saying it failed. Is this to be expected at this point?

    Finally for now as this is as far as I get before admitting defeat, when I enter the command "sudo vim makeopts.in" (without the quotes) there isn't an entry that has "PROC=" with or without the quotes! Where am I going wrong?

    I have started from scratch twice now and have the same problems each time.

    Can you assist?

    ReplyDelete
    Replies
    1. Hi,

      Appologies for the late reply, been on a 'unwired' vacation!

      Firstly, can you let me know which version of Asterisk you are installing.

      Secondly, Sorry, the commands under "Update Asterisk Install Pre-requisites" are for some reason, joined together. These should be in two parts.

      sudo apt-get update

      Then...

      apt-get -y install make gcc.....etc etc...

      Can you expain "second part" and also copy and paste your errors here or on pastebin so I can take a look, thanks.

      Don't worry too much about the "PROC=" if its there, use it, if not your on Asterisk 1.8.1+ and as far as I can see, it's no longer required. I have compiled Asterisk 1.8.13.0 with the above process with no issues.

      If you would like me to login and take a look, ping me an email at thepie at my google username dot com with ssh details.

      Delete
  4. Hi,
    first of all, thanks for this great implementation
    second, excuse my english.

    I´m planning a project to use my raspberry as a house phone system.
    By the way, i never collect skills in asterisk/freepbx before and never heard about it since two days ago, but i love it to tinkering in linux and electronics. :)
    So i took the latest image from www.raspberry-asterisk.org, config my static ip, etc... read a lot about asterisk and freepbx,... and added first sip-devices (android-smartphone and pc) over freepbx. I tested it with voicemail etc. and it works very well.

    But by doing the next step, i get grey hairs.
    The theme is "console dial". I find out, that it is possible to use linux alsa sounddevices as a softphone that can control with asterisk console by typing e.g."CLI>dial xxx" or "CLI>console dial xxx"
    I need this function to couple the mic/speaker from raspberry to the speaker/mic from an exsisting door-phone-installation. (Also the gpio should used by shell script to control the electric door opener. I will do this later, because i need first to proof the "console dail" concept and also control gpio is not so difficult.)

    So what have i done:

    ------------------------

    getting the asterisk-console:

    -install many admin-modules at freepbx, also the module "Asterisk CLI"

    -connect via shell and execute "asterisk -c" (there are failure-text at start, but i get a console "CLI>" I think the errors are because of starting as user "pi" and not as linux-user "asterisk", so asterisk runs parallel at the same time!?)

    ------------------------

    soundcard (usb):

    -install a (alsa) usb soundcard

    -type "cat /proc/asound/cards" give me only the installed usb device:
    0 [Device ]: USB-Audio - USB PnP Sound Device
    C-Media Electronics Inc. USB PnP Sound Device at usb-bcm2708_usb-1.2, full spee

    -i can record in shell: "rec ~/test.wav"

    -i can play and hear it: "play ~/test.wav"

    ----------------------

    asterisk config:

    -edit the /etc/asterisk/modules.conf and change "chan_alsa.so" to "load"
    (chan_oss.so is by default noload)

    -edit the /etc/asterisk/alsa.conf in many different configurations
    e.g. like this how-to http://www.voip-info.org/wiki/view/Asterisk+CLI+dial
    (autoanswer=yes, context=local, extension=s, mohinterpret=default, input_device=hw:0,2, output_device=hw:0,1 etc...)

    ----------------------


    But nothing worked!

    When i type "dail 2000" or "console dial 2000" at CLI> to call e.g. my pc using, i get always the same error on bash and on asterisk-cli-module:


    "No such command 'dial 2000' (type 'core show help dial 2000' for other possible commands)"

    I found out, that the dial command only available if the alsa or oss conection to asterisk is existing and working.
    So, how can i proof this or what is wrong in my config?
    Is there a way to configure console dial by using only freepbx?

    It would be nice if someone can give me a hint.

    Thanks

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. I have tried following the instructions, with the following alterations:

      1. using only sudo apt-get -y install make ssh openssh-server openssh-client instead of the full list, then logging in via ssh (putty) and copy-pasting the entire string

      2. using sudo vim makeopts.in, there I cannot find any 'proc' so I am skipping that step

      3. on this step:

      sudo useradd -c "Asterisk PBX" -d /var/lib/asterisk asterisksudo chown -R asterisk:asterisk /var/run/asterisksudo chown -R asterisk:asterisk /var/log/asterisksudo chown -R asterisk:asterisk /var/lib/php/session/

      changing it to

      sudo useradd -c "Asterisk PBX" -d /var/lib/asterisk asterisk
      sudo chown -R asterisk:asterisk /var/run/asterisk
      sudo chown -R asterisk:asterisk /var/log/asterisk
      ***sudo chown -R asterisk:asterisk /var/lib/php/session/***
      this folder did not exist, so I used sudo mkdir to create it, and then ran the command

      4. adding sudo to this command: tar zxvf freepbx*

      5. my Apache also gave failure errors until I edited
      sudo vim /etc/apache2/apache.conf
      sudo vim /etc/apache2/apache2.conf
      as the first one does not exist

      I am still working on this, but I have come to wish for a way of making a backup of the sd card via the sd card reader while within windows, is there any program that will do that (without requiring a restart)? I have tried Acronis and to my dismay it required a restart still

      I have connected via web browser to the raspberry pi, but what is the default username/password for the freePBX administration?

      P.S. - sorry if these are supposed to be obvious, but because I was copy-pasting via ssh each one of these typo/errors gave me problems that I needed to figure out

      also, I would suggest linking to here
      http://php.net/manual/en/timezones.america.php
      for the php timezone setup step

      Delete
  6. His Cristian,

    Thank you for the above comments. Some errors when submitting pasted commands in blogger concatinates the commmand set. I will rectify as per your comments. The default username in FreePBX: admin password: admin

    moishtech.

    ReplyDelete
  7. Hi,

    thats the result when i use the last command of your instruction:


    sudo amportal start

    Please wait...

    SETTING FILE PERMISSIONS
    chown: missing operand after `asterisk:asterisk'
    Try `chown --help' for more information.
    chmod: missing operand after `g+w'
    Try `chmod --help' for more information.
    chown: missing operand after `asterisk:asterisk'
    Try `chown --help' for more information.
    chmod: missing operand after `g+w'
    Try `chmod --help' for more information.
    Permissions OK

    STARTING ASTERISK
    Asterisk Started


    In the browser I can open the Main Site, but when I click at the "FreePBX Administration"-Link ....the result was a HTTP-ERROR 500

    Thanks for help and sorry for my bad English ;-)

    Ronny

    ReplyDelete
    Replies
    1. I have a similar problem in that from a remote machine I usually get the 500 error from the three links. occasionally it will query for credentials. However, as soon as I provide the credentials it will return the 500. local to the pi 127.0.0.1 the link asks for credentials. then posts a blank page.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. This comment has been removed by the author.

      Delete
    5. This comment has been removed by the author.

      Delete
    6. I'm tracking a similar issue here:

      http://www.freepbx.org/forum/freepbx/installation/need-assistance-setting-up-on-raspberry-pi-blank-config-php-page

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. HI,

    I followed all the instructions above, but at the end /admin sections returns the following error:


    FATAL ERROR

    Unknown SQL engine: []

    Trace Back

    /var/www/admin/common/db_connect.php:57 die_freepbx()
    [0]: Unknown SQL engine: []

    /var/www/admin/bootstrap.php:75 require_once()
    [0]: /var/www/admin/common/db_connect.php

    /var/www/admin/config.php:61 require()
    [0]: /var/www/admin/bootstrap.php



    Any ideas where things might have gotten wrong?

    ReplyDelete
  10. sorry if this repeats but after posting to above my post seems to have disappeared.

    I have a similar problem to previous in that from a remote machine while logging into the config webpage I usually get the 500 error from the three links. occasionally it will query for credentials. However, as soon as I provide the credentials it will return the 500 error page. local to RPi (127.0.0.1) the link asks for credentials. then posts a blank page, any idea from here?

    ReplyDelete
  11. Hello moishtech,

    I attempted your install and mostly things went very smoothly. I was curious though about amportal. I don't see how that is configured and when I got to one of the last steps: 'sudo amportal stop', there was no amportal tool installed. I see now that there is an 'amportal.conf' file and 'amp_conf/' directory but there's no binary to run here... Any clues?

    Thanks,

    Daniel

    ReplyDelete
  12. I was able to install and configure this on my pi and have 2devices on my home wifi network. a tablet and a mobile. On these i installed the csipsimple softphone. I created extensions in freepbx and am able to establish a call between the tablet and mobile and visaversa.
    The problem I am facing is the call disconnects after 7 to 15 seconds. Any ideas on how to troubleshoot.

    ReplyDelete
  13. I sorted my problem.Changed the codec and all is good.

    ReplyDelete
  14. Hi,
    Following you install, all good until "sudo make config"

    I get the following
    "We could not install init scripts for your operating system."

    Can anyone help?

    Steve...

    ReplyDelete
  15. Hi,

    All went perfect untill the command

    sudo mysqladmin create asterisk

    I get an error message

    root@squeezeplug:/usr/src/freepbx-2.9.0# sudo mysqladmin create asterisk
    mysqladmin: connect to server at 'localhost' failed
    error: 'Access denied for user 'root'@'localhost' (using password: NO)'

    I checked this message on the internet, but I found no clue. What can I do.

    Thanks in advance

    ReplyDelete
    Replies
    1. Hi Meindert,

      If you would like me to log on and take a look, please mail me your IP, username and password to mohammed at coventryprinting dot com and a time when you are available to discuss it Skype. (dont forget your Skype ID)

      Thanks,
      M

      Delete
  16. Hi,

    First of all, thanks for sharing you knowledge here. It's a very good tutorial to start with Asterisk on Raspberry.

    I installed it right, but only Asterisk because I don't need FreePBX. Then I have tried to install a module called VXI, that it's a VXML interpreter, to play with my Raspi but I cannot run the module. The installation was fine, I didn't see any error in the process but Asterisk cannot load the module. It says:

    WARNING[9236] loader.c: Error loading module 'app_vxml.so': /usr/lib/asterisk/modules/app_vxml.so: cannot open shared object file: No such file or directory.
    WARNING[9236] loader.c: Module 'app_vxml.so' could not be loaded.

    I have checked that app_vxml.so is where it's supposed to be. Could you help me please? I'm lost...

    You can find VXI here: http://www.i6net.com/downloads/vxi-packages/

    Thanks in advance.

    ReplyDelete
  17. Hello, I followed you steps but when I run the 'sudo make install' I end up with having "chan_agent.c39:222 fatal error: asterisk,h: No such file compilation terminated"
    I tried the steps couple of times with different asterisk versions.But always the same. Appriciate your help since I am new to Asterisk :)
    Thanks in Advance!

    ReplyDelete
    Replies
    1. Hi Rukshan,

      If you would like me to log on and take a look, please mail me your IP, username and password to mohammed at coventryprinting dot com and a time when you are available to discuss it Skype. (dont forget your Skype ID)

      Thanks,
      M

      Delete
  18. You might want to change the article title - it says "FreeBPX" ;)

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. Dear sirs,

    I would like to congratulate you for your presentation on Asterisk.

    I am currently working on a project which relates Raspberry and Asterisk and I have a question for you, in case you could help me: I would like to add a collaboration functionality such as instant messaging to this solution. Do you think it is possible? Do you have any recommendations on this matter? Otherwise, do you know anybody who does?

    Thanks in advance for your kind help.

    ReplyDelete
  21. Hello, Thanks for the tutorial!

    excellent initiative. Congratulations!

    I'm having problems when run newinstall.sql

    raspberryverga usr/src/freepbx-2.9.0/SQL root @ :/ # mysql-u asterisk-p <newinstall.sql
    Enter password:

    ERROR 1046 (3D000) at line 20: No database selected


    I do not know what can be.

    Can you give me a light?

    Thank you!

    ReplyDelete