~
~
:wq

Saturday 13 March 2010

Installing Asterisk and FreePBX on a vmware instance of Ubuntu 10.04 (Lucid) alpha3

spanish version - all english posts

I needed to test some PBX configurations but as I don't have a PBX at hand to use I thought that it would be interesting to test, at last, Asterisk. At the same time, it would be nice to test Ubuntu 10.04 just one month and a half before its release.

For the test I've created an instance of vmware-server 2.0 where I've installed a basic Ubuntu 10.04 Lucid alpha3 with up to date updates and static IP.

For the installation of Asterisk and its GUI FreePBX I've followed the script pointed out at Ubuntu's wiki which works in Ubuntu 9.10; Hence all credits should go to the script authors.

That said and after a quick look to the script I've decided to not execute it blindly. I've seen some oddities in it. For example, there is a "chown asterisk:asterisk /var/run"!. So I've preferred to make this step by step howto using the script as a basis.

Basically, the steps in this howto are the same in that script. However there are many changes in the syntax (because I prefer my own syntax). I've avoided some steps which I disliked. But I've resigned myself to commit other steps which maybe should be reorganized or even be rewritten. Maybe If I had more time for it I would have rewrote the script, but the job is just test a few things on a PBX and after all everything works which is what really matters.

We start with a basic and up to date instance of Lucid Alpha 3 on vmware server.

Steps:

Install mysql (You should enter the password for the mysql root user; for example 1234):

aptitude update
aptitude install -y mysql-server

Install all other dependencies we will need later:

aptitude install -y build-essential linux-headers-`uname -r` openssh-server bison flex apache2 php5 php5-curl php5-cli php5-mysql php-pear php-db php5-gd curl sox libncurses5-dev libssl-dev libmysqlclient15-dev mpg123 libxml2-dev

Download all the asterisk source packages that we are going to compile:

cd /usr/src/
xargs wget << SOURCES
http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/releases/dahdi-linux-complete-2.2.1+2.2.1.tar.gz
http://downloads.asterisk.org/pub/telephony/libpri/releases/libpri-1.4.10.2.tar.gz
http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-1.6.2.6.tar.gz
http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-addons-1.6.2.0.tar.gz
SOURCES

Once we have all the sources we will compile them. Reading the relevant configuration, at least read the README file, it is always a good idea.

Compile and install dahdi; problably unnecessary for our vmware instance, but it wont hurt:

tar xvf dahdi-linux-complete-2.2.1+2.2.1.tar.gz
cd dahdi-linux-complete-2.2.1+2.2.1
make all && make install && make config

libpri compilation and install:

cd ..
tar xvf libpri-1.4.10.2.tar.gz
cd libpri-1.4.10.2
make && make install

Now do the same with asterisk:

cd ..
tar xvf asterisk-1.6.2.6.tar.gz
cd asterisk-1.6.2.6
./configure
make && make install

Without forgetting to install the sample configurations:

make samples

Untar, compile and install the addons for asterisk:

cd ..
tar xvf asterisk-addons-1.6.2.0.tar.gz
cd asterisk-addons-1.6.2.0
./configure
make && make install

As before lets install the sample files:

make samples

Finally install the extra sounds for our new PBX:

cd /var/lib/astersik/sounds
wget -O - http://downloads.asterisk.org/pub/telephony/sounds/asterisk-extra-sounds-en-gsm-current.tar.gz | tar xvfz -

Now we start doing some adjustments to make our installation work. We create the user "asterisk" and add the apache user to the "asterisk" group (not sure if this is needed):

adduser asterisk --disabled-password --no-create-home --gecos "asterisk PBX user"
adduser www-data asterisk

Change the default user and group for apache to asterisk in apache2.conf (this is also a step that doesn't convince me much, but as it is just a test, lest follow the directives of the original script):

cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf_orig
sed -i 's/^\(User\|Group\).*/\1 asterisk/' /etc/apache2/apache2.conf

In the original script it is also proposed to modify the sha-bang of the /usr/sbin/safe_asterisk script from sh to bash:

sed -i '1 {s/\<sh\>/bash/}' /usr/sbin/safe_asterisk

Now create the script that will manage the asterisk service. Here I haven't made any changes on the original script, I've just added the basic information (init info) that should carry every init script:

cat > /etc/init.d/asterisk <<-END_STARTUP
#!/bin/bash
### BEGIN INIT INFO
# Provides:          asterisk
# Required-Start:    \$network \$syslog
# Required-Stop:     \$network \$syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Asterisk daemon.
# Description:       This script handles start/stop states of asterisk.
### END INIT INFO

set -e
set -a
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Asterisk"
NAME=amportal
DAEMON=/usr/sbin/\$NAME

test -x \$DAEMON || exit 0

d_start() {
    amportal start
}

d_stop() {
    amportal stop
}

d_reload() {
    amportal restart
}

case "\$1" in

start)
    echo -n "Starting \$DESC: \$NAME"
    d_start
    echo "."
;;

stop)
    echo -n "Stopping \$DESC: \$NAME"
    d_stop
    echo "."
;;

restart|force-reload)
    echo -n "Restarting \$DESC: \$NAME"
    d_stop
    sleep 10
    d_start
    echo "."
;;

*)

    echo "Usage: \$SCRIPTNAME {start|stop|restart|force-reload}" >&2
    exit 3
;;

esac

exit 0
END_STARTUP

make appropriate modifications to the asterisk init script to make it available at booting:

chmod 755 /etc/init.d/asterisk
update-rc.d asterisk defaults 90 10

We are almost done. Now we are going to install FreePBX, the graphical interface that we will install to manage Asterisk (now here comes the chaos; IMHO the following steps reorganized would be better):

cd /usr/src/
wget -O - http://mirror.freepbx.org/freepbx-2.7.0.tar.gz | tar xvfz -
cd freepbx-2.7.0/

Copy amportal.conf configuration file to /etc/:

cp amportal.conf /etc/

Create the databases. Remember that we had used "1234" as the password for our mysql root user. Also we define a password for the asterisk database, eg 4321:

export MYSQL_ROOT_PW=1234
export ASTERISK_DB_PW=4321
mysqladmin -u root -p${MYSQL_ROOT_PW} create asterisk
mysqladmin -u root -p${MYSQL_ROOT_PW} create asteriskcdrdb
mysql -u root -p${MYSQL_ROOT_PW} asterisk < SQL/newinstall.sql
mysql -u root -p${MYSQL_ROOT_PW} asteriskcdrdb < SQL/cdr_mysql_table.sql
mysql -u root -p${MYSQL_ROOT_PW} <<-END_PRIVS
GRANT ALL PRIVILEGES ON asterisk.* TO asteriskuser@localhost IDENTIFIED BY "${ASTERISK_DB_PW}";
GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO asteriskuser@localhost IDENTIFIED BY "${ASTERISK_DB_PW}";
flush privileges;
END_PRIVS

And slightly modify the settings in /etc/amportal.conf (in the original script this is done before installing freepbx, so we do it too):

sed -i "s/# \(AMPDBUSER=.*\)/\1/" /etc/amportal.conf
sed -i "s/# \(AMPDBPASS=\).*/\1${ASTERISK_DB_PW}/" /etc/amportal.conf
sed -i "s@\(AMPWEBROOT=\).*@\1/var/www/@"  /etc/amportal.conf
sed -i "s@\(FOPWEBROOT=\).*@\1/var/www/panel@" /etc/amportal.conf
sed -i "s@\(FOPWEBADDRESS=\).*@PUTIPADDRESS@" /etc/amportal.conf

Adjust some PHP.ini settings related to the use of memory (in the original script there are some changes that are not necessary for lucid):

sed -i 's/\(^upload_max_filesize = \).*/\120M/' /etc/php5/apache2/php.ini

Change the permissions of a series of directories:

chown asterisk. /var/run/asterisk
chown -R asterisk. /etc/asterisk
chown -R asterisk. /var/{lib,log,spool}/asterisk
chown -R asterisk. /var/www/

We enable the asterisk configuration as it is indicated in /etc/asterisk/asterisk.conf by removing the trailing characters in the first line:

sed -i '1 s/\(\[directories\]\).*/\1/' /etc/asterisk/asterisk.conf

At last! lets install freepbx:

./start_asterisk start
./install_amp

Restart apache2 and dahdi:

/etc/init.d/apache2 restart
/etci/init.d/dahdi restart

Finally (it seems necessary):

ln -s /var/lib/asterisk/moh /var/lib/asterisk/mohmp3
amportal start

That's all; we can connect to the management interface of or new virtual ippbx at http://ip/admin/

A reboot shows that everything works!

25 comments:

  1. Hi,

    Your walkthru works great! But I am not able to connect to the asterisk via external network.

    Would implementing OpenSips solve this problem? And how do I install opensips?

    Thanks

    ReplyDelete
  2. Hi Desmond,

    I'm not sure what do you mean.

    However if you are just trying to access your asterisk with a VoIP phone which is outside of your LAN, you'll need to open at least one UDP port in your router and NAT it to your asterisk local IP.

    Usually that port is 5060. It doesn't hurt to NAT ports 10000-20000 UDP too.

    Maybe if you still have problems you may also need to edit sip_general_custom.conf to define your external IP.

    ReplyDelete
  3. Hi hmontoliu,

    I have install on Ubuntu 10.4 LTS, and I have this error after :

    Automatically restarting Asterisk.
    Asterisk ended with exit status 1
    Asterisk died with code 1.
    cat: /var/run/asterisk/asterisk.pid: No such file or directory
    Automatically restarting Asterisk.

    I search but i found othing to repair this, can you help me.

    Thanks

    ReplyDelete
  4. Hi hmontoliu

    I tried to put the user asterisk on the root group, and asterisk can start after, did you have this error before?

    Thak

    Jsgrenon

    ReplyDelete
  5. Jean-Seb said...

    [...]
    > I tried to put the user asterisk on the root group, and asterisk can start after, did you have this error before?

    Hmmm, that should be unnecessary and a security risk IMHO.

    Did you change the owner permissions to all the dirs listed in my post?

    ReplyDelete
  6. Yes I change all owner permission. I will tried to re-installed.
    Thank

    ReplyDelete
  7. amazing and flawless. thanks for your hard work

    ReplyDelete
  8. Hi Hmontoliu,

    I have re-install and the right in folder it's ok now. But when I call with a land line, I have this in my log, and the call hang up after 5 second.

    == Using SIP RTP TOS bits 184
    == Using SIP RTP CoS mark 5

    Any idea?

    JS Grenon

    ReplyDelete
  9. Jean-Seb said...
    > I have re-install and the right in folder it's ok now. But when I call with a land line, I have this in my log, and the call hang up after 5 second.
    >
    > == Using SIP RTP TOS bits 184
    > == Using SIP RTP CoS mark 5
    >
    > Any idea?

    Hi,

    I'm in a hurry right now, but I bet that it is a NAT issue.

    you may need to "declare" your network settins in /etc/asterisk/sip_general_custom.conf

    among others you'll need to define your DNS name using "externhost" or your external IP with "externip". Also it will be a good idea to define all your local networks with "localnet".

    For example, my /etc/asterisk/sip_general_custom.conf looks like:

    [...]
    externhost=XXXX.dyndns.org
    ;externip=###.##.##.##
    ;externip=##.#.##.###
    localnet=###.###.#.#/###.###.###.#
    localnet=##.#.#.#/###.###.###.#
    localnet=###.###.#.#/###.###.###.#
    nat=yes
    externrefresh=5
    [...]

    HTH

    ReplyDelete
  10. HI,

    thank's for your time, and the information. I tried to put the information in my conf, but i have te same problem.

    I have the same NAT that my asterisk 1.4 in production.

    I tired to change the IP, and put the same iip that one in prod.

    I'ts the first time that i tried the 1.6 version, and i found nothing in the web for this error.

    I will continu to search, thank's

    Js Grenon

    ReplyDelete
  11. good stuff thanks for your time

    I did notice a small type on the second to last code block

    /etci/init.d/dahdi restart

    ReplyDelete
  12. Hi, I did step by step tutorial and everything is perfect!
    FreePBX and Asterisk is running perfectly!
    But when I make a connection between two branches of type SIP has no sound on either side! Could anyone help me?

    Excuse me for English, is that I do not know the language and used Google Translate!
    Since already many thanks!

    ReplyDelete
  13. Hi Nickael Almeida

    You are probably having an issue with NAT; search for asterisk nat settings. For example:

    http://www.asteriskguru.com/tutorials/sip_nat_oneway_or_no_audio_asterisk.html


    HTH

    ReplyDelete
  14. Thank you for your tutorial hmontoliu .

    I´m just having a small problem, I don´t know what I have done wrong, but I have to go to a shell and run asterisk since it doesn´t work at start-up. I´m a newbie on linux and I have no idea how to fix it.

    Could you help me?
    Thank again

    FranciscoRC

    ReplyDelete
  15. Francisco Conceição said...
    >
    > Thank you for your tutorial hmontoliu .
    >
    > I´m just having a small problem, I don´t know what I have done wrong, but I
    > have to go to a shell and run asterisk since it doesn´t work at start-up. I´m a
    > newbie on linux and I have no idea how to fix it.
    >
    > Could you help me? Thank again
    >
    > FranciscoRC
    >

    Hi Francisco,

    can you elaborate a bit more your question or paste the results of running the following two commands as root in a shell:

    /etc/init.d/asterisk start


    and

    bash -x /etc/init.d/asterisk start

    ReplyDelete
  16. Thanks for you reply, here is the output for the 2 commands.

    /etc/init.d/asterisk start
    ERROR: __ASTERISK_SBIN_DIR__/asterisk not found

    bash -x /etc/init.d/asterisk start
    + PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    + NAME=asterisk
    + DESC='Asterisk PBX'
    + DAEMON=__ASTERISK_SBIN_DIR__/asterisk
    + ASTVARRUNDIR=__ASTERISK_VARRUN_DIR__
    + ASTETCDIR=__ASTERISK_ETC_DIR__
    + TRUE=/bin/true
    + COLOR=yes
    + set -e
    + '[' -x __ASTERISK_SBIN_DIR__/asterisk ']'
    + echo 'ERROR: __ASTERISK_SBIN_DIR__/asterisk not found'
    ERROR: __ASTERISK_SBIN_DIR__/asterisk not found
    + exit 0

    I think this output explains better than me :)

    ReplyDelete
  17. Francisco, it looks like you have a different /etc/init.d/asterisk file. Check out my howto again.

    HTH

    ReplyDelete
  18. Hi
    Sorry during my quest I messed things up in /etc/init.d/asterisk. I change it back to your instructions but it didn´t work either.

    I finally managed to get things working after changing the DAEMON=/usr/sbin/\$NAME to DAEMON=/usr/local/sbin/\$NAME.

    I don´t know if its right or wrong, but it´s working. :)

    FranciscoRC

    ReplyDelete
  19. El 29/11/10 16:11, Francisco Conceição wrote:
    >
    > Hi
    > Sorry during my quest I messed things up in /etc/init.d/asterisk. I
    > change it back to your instructions but it didn´t work either.
    >
    > I finally managed to get things working after changing the
    > DAEMON=/usr/sbin/\$NAME to DAEMON=/usr/local/sbin/\$NAME.
    >
    > I don´t know if its right or wrong, but it´s working. :)
    >

    If it's working is because it's right,

    I'm glad to read that you managed to fix it :-)

    H.

    ReplyDelete
  20. Well thank you very much for the instructions!!
    I did want to put out for info purposes that:
    Using: Ubunto 10.4 with the asterisk and dahdi from apt-get I folled all the directions except that the /etc/init.d/asterisk script was givin me problems (var/run/asterisk.ctl) so I restore the init script that comes with the Ubuntu asterisk and now everything is running fine.
    On another subject I have the same problems as someone above:
    == Using SIP RTP TOS bits 184
    == Using SIP RTP CoS mark 5
    when doing an incoming call to the freepbx box
    and yes I do have sip externip and all configured.

    ReplyDelete
  21. Hi there

    sorry I'm new to this and just experimenting

    I followed your instructions but when i go to browser

    http://192.168.0.1/admin/

    I get "The requested URL /admin/ was not found on this server." error

    What could I have done wrong

    Many thanks...

    ReplyDelete
    Replies
    1. Hi, mrbronz

      Is that the IP of your FreePBX server?
      Have you restarted apache after configuring it?

      Delete
  22. hi, I followed your article to the point. My problem is that I am using virtualmin to play with my server. However virtualmin is creating web directories at /home/username/public_html

    I have multiple domains running on this machine, so each domain have thier own directory in the /home folder. I know Apache's default directory is /var/www/ and accorning to this article, it has correctly placed the files within that directory. now when I type in the IP of the server, I am redirected to a folder in /home/username/public_html

    I created a directory in public_html called voipphone and cp all the files from /var/www/

    However, when I go the site http://lookkawa.com/voipphone/ I get Service not available. Any suggestions??

    Thanks you once again for a great article and help.

    Cheers

    ReplyDelete
  23. modified
    # Full path to asterisk binary
    DAEMON=/usr/sbin/asterisk
    ASTVARRUNDIR=/var/run/asterisk
    ASTETCDIR=/etc/asterisk
    TRUE=/bin/true
    nano /etc/init.d/asterisk

    ReplyDelete
  24. Hello !
    When I install Dahdi 2.2.1+ 2.2.1 , a system show no hardware found although I used card X100P . I don't know how to solve this problem . Can you help me !
    Thanks a lot !

    ReplyDelete