Freepbx on Ubuntu (Ubuntu v12, Asterisk v11, Freepbx v2.11)

Sections: 

Ubuntu

This guide covers installing Asterisk and FreePBX from source on Ubuntu.  Although Ubuntu includes Asterisk deb packages they are not used in this guide.

Tested on:

Ubuntu Server v12.04 LTS 32 or 64bit
Asterisk v11.5
FreePBX v2.11

Let's get started

If installing from scratch via ISO download Ubuntu Server ISO then proceed with the default install.  After install skip down to Create root password. Otherwise, it is assumed you already have a server with a base Ubuntu server installation before you begin. We want to be running in console text mode not GUI graphics mode. If you already have a GUI installed you will want to disable it at boot time. You will still be able to start it manually using startx.

sudo nano /etc/default/grub

Change GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash” to GRUB_CMDLINE_LINUX_DEFAULT=”” If it is not already set to that.

Update Grub:

sudo update-grub

Create root password

$sudo passwd root
[sudo] password for defaultuser:[Type the defaultuser password you set up during install]
Enter new UNIX password: [Type the root password you want]
Retyp new UNIX password: [Retype the root password you chosen before]
passwd: password updated successfully
$su
Password:[Type the root password]
#

From now on all commands assume you are logged in as root (#).  Otherwise just keep using sudo before every command.

Install Asterisk/FreePBX required packages, other useful packages, and their dependencies

apt-get update && apt-get upgrade -y
apt-get install -y build-essential openssh-server apache2 mysql-server mysql-client bison flex php5 php5-curl php5-cli php5-mysql php-pear php5-gd curl sox libncurses5-dev libssl-dev libmysqlclient-dev mpg123 libxml2-dev libnewt-dev sqlite3 libsqlite3-dev pkg-config automake libtool autoconf git subversion uuid uuid-dev libiksemel-dev tftpd postfix mailutils nano ntp chkconfig libspandsp-dev libcurl4-gnutls-dev unixodbc unixodbc-dev libmyodbc e2fsprogs

It will keep asking to set MySQL root password so set that now.  If unsure how to configure postfix then configure for internet site.

Fix for chkconfig /sbin/insserv: No such file or directoryerror.

ln -s /usr/lib/insserv/insserv /sbin/insserv

Install pear DB

Don't worry about the warning message.

pear install db-1.7.14
reboot

TFTP

If you plan to use hardware SIP phones you will probably want to enable the tftp server. Create tftp configuration file.

nano /etc/xinetd.d/tftp
service tftp
{
protocol        = udp
port            = 69
socket_type     = dgram
wait            = yes
user            = nobody
server          = /usr/sbin/in.tftpd
server_args     = /tftpboot
disable         = no
}

Now make the directory and restart the daemon to start tftp.

mkdir /tftpboot
chmod 777 /tftpboot
service xinetd restart

Set Timezone

check timezone

date

If it needs to be changed then do the following

dpkg-reconfigure tzdata

and follow instructions

Download and untar source files.

Get and install DAHDI (optional)

Not needed if you are not installing a telco card and not using meetme conferencing and paging.  Asterisk 11 uses confbridge by default for conferencing and paging.  If unsure then install anyways.

cd /usr/src
wget http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/dahdi-linux-complete-current.tar.gz
tar zxvf dahdi-linux-complete*
cd /usr/src/dahdi-linux-complete*/
make && make install && make config

Get FreePBX

Check if this is the latest released version.

cd /usr/src
wget http://mirror.freepbx.org/freepbx-2.11.0.42.tgz 
tar zxvf freepbx-2.11*

Get and Install Asterisk

Do NOT run make samples.  If you do it causes some problems you will have to clean up later on.  If you run make samples on an already running FreePBX system you are upgrading it will break FreePBX. You will then have to manually change back user/password in /etc/asterisk/manager.conf and probably some other things to get it working again.

cd /usr/src
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-11-current.tar.gz
tar zxvf asterisk-11-current.tar.gz
cd /usr/src/asterisk-11*/
make clean && make distclean

Preinstall 

The CFLAGS=-mtune=native setting prevents some problems that come up with virtual machines and moving them to different hardware.  It is also arguably a better more compatible way to do it than the default Asterisk uses.  Last time we checked Asterisk uses CFLAGS=-march=native by default if nothing is specified.  Here is a link to the description of these settings.

./configure && make menuselect

Select format_mp3 in "addons" if you are going to be doing anything with mp3 files.   This option is not selected by default

For backwards compatibility and fall back in case ODBC doesn't work I recommend you install the deprecated cdr_mysql as well.  This option is not selected by default.

FreePBX does not use Asterisk realtime but if you are thinking of using A2Billing then also select res_config_mysql.

Select Core Sounds and Extra Sounds.  I suggest ulaw as they sound better than gsm especially if you are using ulaw as your default codec.  I usually just check both. Then make sure to press the save button afterwards.

When you select format_mp3 above as an addon you must run a script before going any further otherwise the install will fail.  Run the following command after selecting menu options and saving but before running make.

./contrib/scripts/get_mp3_source.sh

You must also have subversion installed to run the above script and be in the root directory of the Asterisk source code.

Now compile and install Asterisk.  DO NOT run make samples even though the install script suggests you do.  It will cause conflicts with FreePBX config files.

make && make install

Create Asterisk User

adduser asterisk --disabled-password --no-create-home --home /var/lib/asterisk --shell \
/sbin/nologin --gecos "Asterisk User"

Change Apache User

Change Apache run user and group name to asterisk.

sed -i 's/\(APACHE_RUN_USER=\)\(.*\)/\1asterisk/g' /etc/apache2/envvars
sed -i 's/\(APACHE_RUN_GROUP=\)\(.*\)/\1asterisk/g' /etc/apache2/envvars
chown asterisk. /var/lock/apache2
service apache2 restart

MySQL Setup

MySQL should already be running but just to be sure.

service mysql start

Now, to configure the databases for Freepbx.  Since we have already created a mysql root password we have to enter this password after each of the following commands.

cd /usr/src/freepbx*/
mysqladmin -p create asterisk
mysqladmin -p create asteriskcdrdb
mysql -p asterisk < SQL/newinstall.sql
mysql -p asteriskcdrdb < SQL/cdr_mysql_table.sql
mysql -p

mysql> GRANT ALL PRIVILEGES ON asterisk.* TO asteriskuser@localhost IDENTIFIED BY 'amp109';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO asteriskuser@localhost IDENTIFIED BY 'amp109';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> \q

Install FreePBX

/usr/sbin/safe_asterisk
cd /usr/src/freepbx*/

Now run the FreePBX install script.  Select all defaults for now by hitting the ENTER key at each prompt.

./install_amp

If you get any warnings or errors they're usually not traumatic.

Set FreePBX to start on boot

nano /etc/rc.local

Add

sleep 1
/usr/local/sbin/amportal start

Before exit 0 

For Ubuntu, Asterisk needs to recreate the /var/run/asterisk directory each time the server boots.  Based on our testing, there needs to be a 1 second delay (ie. sleep 1) added to give the server time to do that before we try start Asterisk using the amportal start command.

Set Apache root directory to /var/www/html

If this has already been changed from the default/var/www directory then edit manually

sed -i "s_/var/www_/var/www/html_" /etc/apache2/sites-available/default
reboot

You should now be able to access FreePBX GUI at http://yourserverIP

After creating the default username/password, log into the FreePBX GUI and press the red Apply Config button at the top before doing anything else. This generates the Asterisk config files.   Asterisk will not run properly until these files are generated and the server rebooted.

reboot

CDR ODBC

This is optional if you selected the deprecated cdr_mysql module in Asterisk menu at compile time. This is the new recommended way of connecting to the CDR DB.  Eventually this will be required when cdr_mysql no longer works or is no longer included with Asterisk.

nano /etc/odbcinst.ini
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/i386-linux-gnu/odbc/libmyodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libodbcmyS.so
FileUsage = 1

If 64bit OS change the 1 line to ix86_64-linux-gnu Test that the ODBC driver is working

odbcinst -s -q

should result in [MySQL]

nano /etc/odbc.ini
[MySQL-asteriskcdrdb]
Description     = MySQL ODBC Driver
Driver          = MySQL
Socket          = /var/run/mysqld/mysqld.sock
Server          = localhost 
Database        = asteriskcdrdb 
Option          = 3

Check the following file which Asterisk uses to connect to the CDR DB

nano /etc/asterisk/res_odbc_additional.conf
[odbccdr]
enabled => yes 
dsn => MySQL-asteriskcdrdb 
username => asteriskuser 
password => amp109

Now using the above username/password, test that it can can connect to the DB via odbc

isql -v MySQL-asteriskcdrdb asteriskuser amp109

If you used a different username/password when setting up cdr DB use those and duplicate the res_odbc_addional.conf file to res_odbc_custom.conf substituting the username/password for the relevant ones. should result in

Connected!
sql-statement
help [tablename]
quit

SQL>

Type quitto exit.

Lastly create or add the following. Note connection=odbcdr matches [odbcdr] in res_odbc.conf.  calldate does not exist any more in cdr_adaptive_odbc.so.  To have the behaviour the same as standard cdr functions we add the alias start line

nano /etc/asterisk/cdr_adaptive_odbc.conf
[first]
connection=odbcdr
table=cdr
alias start => calldate

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

Misc. optional settings

Change the “upload_max_filesize” from 2M to 20M to allow larger music on hold files.

nano +891 /etc/php5/apache2/php.ini

Edit Apache web server for GUI access using a port other than 80:

nano +134 /etc/apache2/ports.conf

change "Listen 80" to "Listen 8888" or whatever port you want.  Instead of accessing FreePBX by http://xxx.xxx.xxx.xxx You now access it by http://xxx.xxx.xxx.xxx:8888 Change default Apache setting of AllowOverride None to All so that Apache obeys directives in .htaccess files which by default prevents viewing sensitive directories on Freepbx.

nano +7 /etc/apache2/sites-available/default
AllowOverride All
service apache2 restart

Setup external sip extensions if going through NAT.  You can also do this using Asterisk SIP Settings module.

nano /etc/asterisk/sip_nat.conf
nat=yes
externip= or
;externhost=yourdns.com
localnet=192.168.1.0/255.255.255.0
;change the above to whatever your local subnet is
externrefresh=10

(Ctrl-X>y>ENTER) Also, when adding the external SIP extension in FreePBX, make sure to change the nat=never default in the configuration to nat=yes for the extension that will be external.

Install FreePBX commercial module dependencies

If you want to install commercial modules you need zendguard and some additional dependencies.

cd /usr/src

Download zendguard

For 32bit  wget http://downloads.zend.com/guard/5.5.0/ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz

For 64bit  wget http://downloads.zend.com/guard/5.5.0/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz

Untar

tar -xzf ZendGuardLoader-php-5.3*

Create directory for it and copy. 

mkdir /usr/local/lib/Zend
cp ZendGuardLoader-php-5.3*/php-5.3.x/ZendGuardLoader.so /usr/local/lib/Zend/

Tell php where to find it.

nano /etc/php5/conf.d/zendguard.ini
zend_extension = /usr/local/lib/Zend/ZendGuardLoader.so
zend_loader.enable = 1

Install commercial module dependencies.

apt-get install -y incron prosody

Log Files Configuration

If you don't want to see a bunch of notices, warnings and errors each time you restart amportal from command line and you don't want your asterisk log files getting huge from constantly writing debug information do the following:

nano /etc/asterisk/logger_logfiles_custom.conf
console =>
full => notice,warning,error

Also make sure freepbx debug logging is disabled in FreePBX GUI>Settings>Advanced Settings>Developer and Customization

logrotate setup

Set up configuration to rotate log files otherwise they get too big after a short while. Create the following file.

nano /etc/logrotate.d/asterisk

Now add the following to make sure the asterisk log files are rotated weekly along with all the other log files.

/var/log/asterisk/messages /var/log/asterisk/*log /var/log/asterisk/full {
missingok
notifempty
sharedscripts
create 0640 asterisk asterisk
postrotate
/usr/sbin/asterisk -rx 'logger reload' > /dev/null 2> /dev/null
endscript
}

Do the same for freepbx

nano /etc/logrotate.d/freepbx
/var/log/asterisk/freepbx_dbug /var/log/asterisk/freepbx_debug {
missingok
notifempty
sharedscripts
create 0640 asterisk asterisk
postrotate
/usr/sbin/asterisk -rx 'logger reload' > /dev/null 2> /dev/null
endscript
}

Configure voicemail to email template

nano etc/asterisk/vm_email.inc

Change the template to what you want the voicemail emails to look like.  Check that http://ipaddress_of_Freepbx_server is correct

Root alias

Edit /etc/aliases file and add an email address to forward ‘root’ messages to your personal email address.  At the very bottom you should see a commented example.  Copy it and replace with your email address

nano /etc/aliases
root:   some_email@somedomain.com

Then run

newaliases

Test if you can send emails via root

echo testing | mail -s "test mail" root@localhost

If you get an email then you know aliases and outbound email is working.

MySQL performance tuning

This will reduce memory usage without affecting performance.

nano /etc/mysql/my.cnf
[mysqld]
.
.
.
skip-innodb

(Ctl-x > y > ENTER) From command prompt:

service mysql restart