~
~
: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!

Instalación de Asterisk y FreePBX en una instancia vmware de Ubuntu 10.04 (Lucid) alpha3

english version - all spanish posts

Necesitaba probar unas configuraciones en una centralita y como no tengo ninguna a mano que mejor que probar de una vez por todas Asterisk y aprovechar para ver que tal va Ubuntu 10.04 a falta de poco mas de un mes para su lanzamiento.

Para la prueba creo una instancia de vmware-server 2.0 donde instalo Ubuntu 10.04 Lucid alpha3 con las actualizaciones hasta la fecha y con IP estática.

Para instalar Asterisk y su GUI FreePBX uso como base el script del wiki de ubuntu para Ubuntu 9.10; así que todos los reconocimientos deben de ir para los autores de dicho script.

Dicho lo anterior y tras un vistazo rápido al script, este no me gusta mucho; por ejemplo se hace un "chown asterisk:asterisk /var/run"!!!. Así que he preferido hacer un howto paso a paso usando el script como base por lo que os anoto aquí los pasos que he seguido yo.

Básicamente, los pasos de este howto, son los mismos que el script original. Hay muchos cambios en la forma y alguno en el fondo. Evito los pasos que no me convencen y me resigno a otros que o bien reorganizaría o bien escribiría de nuevo. A lo mejor si tuviera mas tiempo reescribiría el script, pero se trata de comprobar unas cosas en una IPPBX y nada mas. Al fín y al cabo al final todo funciona que es lo que importa.

Pasos:

Comenzamos con una instalación básica y actualizada de Lucid Alpha 3 server en vmware-server.

Instalar mysql (anotamos la password que proporcionamos para el user root de mysql: por ejemplo 1234):

aptitude update
aptitude install -y mysql-server

Instalar las otras dependencias que necesitaremos mas tarde:

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

Descargar los elementos de asterisk que vamos a compilar:

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

Tras haber descargado los distintos componentes procedemos a su compilación. Siempre hay que leer la correspondiente documentación, al menos el README de turno.

Compilamos dahdi; no necesario seguramente para la instancia de vmware ya que se trata de los drivers para el kernel de hardware específico de telefonía:

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

Compilamos libpri:

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

Ahora hacemos lo propio con asterisk:

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

Sin olvidarnos de instalar las configuraciones de ejemplo:

make samples

Descomprimimos, compilamos e instalamos los addons para asterisk:

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

Al igual que antes, nos propone instalar los archivos de ejemplo, cosa que hacemos así:

make samples

Finalmente instalamos los sonidos extra para nuestra centralita:

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

Ahora pasamos a realizar las modificaciones oportunas para que funcione nuestra instalación. Creamos el usuario asterisk y añadimos al usuario de apache al grupo asterisk:

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

Cambiamos el usuario y grupo por defecto de apache por el usuario y grupo asterisk en apache2.conf (a mi esto no me convence mucho, pero como se trata de una prueba rápida sigo las indicaciones del script original):

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

También se propone en el script original que se modifique el ejecutable para el script /usr/sbin/safe_asterisk de sh a bash:

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

Creamos a continuación el script que se encargará de gestionar el servicio asterisk. Aquí no hago ningún cambio sobre el script original; únicamente añado la información básica para init que debería llevar todo script en /etc/init.d:

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

Realizamos las modificaciones oportunas para que arranque asterisk al inicar el sistema:

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

Ya casi estamos, ahora instalamos FreePBX, la interfaz gráfica que vamos a instalar para manejar Asterisk (ahora viene el caos, esto reorganizado estaría mucho mejor):

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

Copiamos el fichero de configruación amportal.conf a /etc/:

cp amportal.conf /etc/

Creamos las bases de datos; recordemos que habíamos usado "1234" como password para el usuario root de mysql. Necesitaremos definir también una contraseña para la base de datos de asterisk; por ejemplo 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

Y modificamos ligeramente la configuración de /etc/amportal.conf (en el script original se hace esto antes de instalar freepbx, así que nosotros también):

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

Ajustamos la configuración de PHP.ini en relación al uso de memoria de la siguente forma (en el script original hay cambios que no son necesarios para lucid):

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

Cambiamos los permisos de una serie de directorios:

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

Habilitamos la configuración de asterisk tal y como se indica en /etc/asterisk/asterisk.conf:

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

Y ahora instalamos por fín freepbx:

./start_asterisk start
./install_amp

Reiniciamos apache2:

/etc/init.d/apache2 restart

Y dahdi si nos hiciera falta:

/etci/init.d/dadhi restart

Por último (parece ser necesario):

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

Ya podemos conectarnos al interfaz de administración de nuestra nueva pbx virtual en http://ip/admin/

Comprobamos que al reniciar todo funciona!