Install Freepbx/Asterisk from source (For advances users)

A while back I was fascinated by VOIP technology. I did know it existed but didn’t really know its power and how much one can achieve with the applications we have available. So, As am open source enthusiast I decided to go for the very popular Freepbx/Asterisk setup.

In this tutorial I will guide you through setting up your own PBX system. For this setup you need not have a VOIP service, as that will be the last part in finishing your phone system. I will cover that briefly in the last and final steps as those steps may differ based on your VOIP provider. This tutorial also assumes you have adequate understanding of computers including but not limited to virtualization, iptables, configuring webservers etc. This tutorial has been inspired by FreePBX OpenSource Project. Fell free to use this in a production environment. This is what I have running as my VOIP server and same is supported by Sangoma.

As much as I like using the latest greatest softwares and OS, unfortunately due to many reasons we are using Debian 8.8. This setup works perfect for me (and officially tested. Let me know in the comments section if you have more suggesitons). You can find the Debian ISO here. Let me know if the link does not work, I’ll update it.

For users trying this for the first time, I would recommend using virtualbox and taking snapshots so that you can easily restore back to a previous state if something does not work right.

Before beginning, Please make sure:

  • You have a working system with virtualbox installed
  • ISO is downloaded
  • You have internet access

At this point you should have Debian 8.8 installed on your virtualbox, if not go ahead and come back when you are done. I did not install the desktop environment, but I only selected “ssh server” and “standard system utilities” rest all will be installed later.
I created a user for myself as well while setting up that user and gave it a password.
Please login as root and run the following to update/upgrade the system.

sed -i 's/ main.*$/ main contrib non-free/' /etc/apt/sources.list
apt-get update && apt-get upgrade -y 
reboot
apt-get update && apt-get upgrade -y
apt-get install -y build-essential linux-headers-`uname -r` openssh-server apache2-mpm-itk apache2 libpam0g-dev libssl-dev mysql-server mysql-client MongoDB 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 unixodbc-dev uuid uuid-dev libasound2-dev libogg-dev libvorbis-dev libicu-dev libcurl4-openssl-dev libical-dev libneon27-dev libsrtp0-dev libspandsp-dev sudo libmyodbc subversion libtool-bin python-dev
curl -sL https://deb.nodesource.com/setup_8.x | bash -
apt-get install -y nodejs

At this point I had the following:
Mysql root password
Go ahead and copy your ssh keys to root user. Will make your job easier and reboot the system and continue with installing as below.

cat >> /root/.my.cnf << EOF
[client]
user=root
password=ROOTPASSWORD
host=localhost

EOF
mysql -e "create database asteriskcdrdb;"
mysql -e "create database asterisk;"
mysql -e "grant all privileges on asterisk.* to 'asteriskuser'@'localhost' identified by 'asteriskuserpassword';"
mysql -e "grant all privileges on asteriskcdrdb.* to 'asteriskuser'@'localhost' identified by 'asteriskuserpassword';"
reboot
pear install Console_Getopt
cd /usr/src
wget http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/dahdi-linux-complete-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/libpri/libpri-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-16-current.tar.gz
wget http://www.nic.funet.fi/index/unix/mail/imap/ftp.cac.washington.edu/mail/imap-2007e.tar.Z
tar zxf imap-2007e.tar.Z
cd imap-2007e
make lnp EXTRACFLAGS="-fPIC -I/usr/include/openssl" IP6=4
cd /usr/src
tar xvfz dahdi-linux-complete-current.tar.gz
cd dahdi-linux-complete-3.0.0+3.0.0
make all
make install
make install-config
cd /usr/src
tar xvfz libpri-current.tar.gz
cd libpri-1.6.0
make
make install
cd /usr/src
tar xvfz asterisk-16-current.tar.gz
cd asterisk-16.3.0
contrib/scripts/get_mp3_source.sh
contrib/scripts/install_prereq install <== punch in your country code Mine is (01)
./configure --with-imap=/usr/src/imap-2007e/ --with-pjproject-bundled --with-jansson-bundled
make menuselect

This is where the magic happens. This menu will enable us to select modules to compile. Below are the modules I need enabled and comile as I need. I found that below options should be enough for a small to medium sized PBX system.

  • Add-Ons => enable chan_mobile, format_mp3, res_config_mysql, and all the deprecated ones
  • Applications => enable app_macro
  • Channel Drivers => disable chan_mgcp, chan_oss, chan_phone, chan_skinny, chan_unistim
  • Resource Modules => disable res_calendar_ews res_calendar_exchange res_odbc res_config_odbc
  • Voicemail Build Options => enable IMAP_STORAGE
  • Core Sound Packages => enable CORE-SOUNDS-EN-WAV, CORE-SOUNDS-EN-GSM
  • Music On Hold File Packages => enable MOH-OPSOUND-GSM, MOH-OPSOUND-WAV
  • Extras Sound Packages => enable EXTRA-SOUNDS-EN-WAV, EXTRA-SOUNDS-EN-GSM

The next section is where we compile everything.

make
make install
make config
ldconfig
make progdocs <== optional for installing documentation
update-rc.d -f asterisk remove

if the above commands ran successfully, you did everything right. Proceed below with finishing installation and configuring the system.

useradd -m asterisk
chown asterisk. /var/run/asterisk
chown -R asterisk. /etc/asterisk
chown -R asterisk. /var/{lib,log,spool}/asterisk
chown -R asterisk. /usr/lib/asterisk
sed -i 's/\(^upload_max_filesize = \).*/\120M/' /etc/php5/apache2/php.ini
mv /etc/apache2/apache2.conf /etc/apache2/apache2_orig
cat >> /etc/apache2/apache2.conf << EOF
Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
	Options FollowSymLinks
	AllowOverride None
	Require all denied
</Directory>
<Directory /usr/share>
	AllowOverride None
	Require all granted
</Directory>
<Directory /var/www/>
	Options Indexes FollowSymLinks
	AllowOverride None
	Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
	Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

EOF

cat >> /etc/odbcinst.ini << EOF
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/x86_64-linux-gnu/odbc/libmyodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcmyS.so
FileUsage = 1
  
EOF

cat >> /etc/odbc.ini << EOF
[MySQL-asteriskcdrdb]
Description=MySQL connection to 'asteriskcdrdb' database
driver=MySQL
server=localhost
database=asteriskcdrdb
Port=3306
Socket=/var/run/mysqld/mysqld.sock
option=3
  
EOF
cat >> /etc/apache2/sites-available/pbx.mgodiya.com.conf << EOF
<VirtualHost 192.168.44.239:80 >
        ServerName              pbx.mgodiya.com
        ServerAdmin             mohit@mgodiya.com
#        RewriteEngine           on
#        RewriteCond             %{HTTPS} !=on
#        RewriteRule             ^(.*) https://%{SERVER_NAME}$1 [R,L]
        ErrorLog                /var/log/apache2/pbx.mgodiya.com/error.log
        LogLevel                error
        CustomLog               /var/log/apache2/pbx.mgodiya.com/access.log combined
	DocumentRoot            /var/www/pbx.mgodiya.com
        AssignUserID            asterisk        asterisk
        ServerSignature         Off
</VirtualHost>
#<VirtualHost 192.168.44.239:443>
#        SetEnvIf                User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
#        SSLEngine               on
#        SSLCertificateFile      /var/CA/mgodiya.com/fullchain.pem
#        SSLCertificateKeyFile   /var/CA/mgodiya.com/privkey.pem
#        SSLProtocol             -All +SSLv3 +TLSv1
#        ServerName              pbx.mgodiya.com
#        ServerAdmin		mohit@mgodiya.com
#        DocumentRoot            /var/www/pbx.mgodiya.com
#        AssignUserID            asterisk        asterisk
#        MaxClientsVHost         1000
#        NiceValue               10
#        <Directory /var/www/pbx.mgodiya.com>
#                Options         Indexes FollowSymLinks MultiViews
#                Order           allow,deny
#                AllowOverride   All
#                Allow           from all
#        </Directory>
#        ErrorLog                /var/log/apache2/pbx.mgodiya.com/error.log
#        CustomLog               /var/log/apache2/pbx.mgodiya.com/access.log combined
#</VirtualHost>

EOF
mkdir -p /var/log/apache2/pbx.mgodiya.com
mkdir /var/www/pbx.mgodiya.com
chown asterisk:asterisk /var/www/pbx.mgodiya.com
a2ensite pbx.mgodiya.com
a2enmod rewrite
a2enmod ssl
a2enmod mpm_itk
/etc/init.d/apache2 restart
cd /usr/src
wget http://mirror.freepbx.org/modules/packages/freepbx/freepbx-14.0-latest.tgz
tar vxfz freepbx-14.0-latest.tgz
cd freepbx
touch /etc/asterisk/{modules,ari,statsd}.conf
./start_asterisk start
./install
Database engine [mysql]: 
Database name [asterisk]: 
CDR Database name [asteriskcdrdb]: 
Database username [root]: asteriskuser
Database password: asteriskuserpassword
File owner user [asterisk]: 
File owner group [asterisk]: 
Filesystem location from which FreePBX files will be served [/var/www/html]: /var/www/pbx.mgodiya.com
Filesystem location from which Asterisk configuration files will be served [/etc/asterisk]: 
Filesystem location for Asterisk modules [/usr/lib/asterisk/modules]: 
Filesystem location for Asterisk lib files [/var/lib/asterisk]: 
Filesystem location for Asterisk agi files [/var/lib/asterisk/agi-bin]: 
Location of the Asterisk spool directory [/var/spool/asterisk]: 
Location of the Asterisk run directory [/var/run/asterisk]: 
Location of the Asterisk log files [/var/log/asterisk]: 
Location of the FreePBX command line scripts [/var/lib/asterisk/bin]: 
Location of the FreePBX (root) command line scripts [/usr/sbin]: 
Location of the Apache cgi-bin executables [/var/www/cgi-bin]: /var/www/pbx.mgodiya.com/cgi-bin
Directory for FreePBX html5 playback files [/var/lib/asterisk/playback]:

after the setup is complete, restart apache, and you should be able to see FreePBX setup page and complete your first time setup.

You might have noticed I have commented out sections, feel free to uncomment and adjust accordingly. Dont forget to change the IP in apache config to adjust to your subnet.

The next section is a continuation to this tutorial. It will be some asterisk configuration, tidying up and installation of OSSEndpoint which is no longer maintained by FreePBX any more as it is now a commercially sourced project.
After logging in, I installed the following modules form module admin:

Certificate Manager, Phonebook, Speed Dial Functions, Text To Speech Engines, Text To Speech, User Management, User Control Panel, IVR, Paging and Intercom, Parking Lot, Calendar, Call Event Logging, Queue Priorities, Queues, Ring Groups, Caller ID, Wake Up Calls, SMS, Time Conditions, Voicemail Blasting, WebRTC Phone

In the next part(s) I will show you how to create extensions, configure Voicemail. Please let me know in the comments if there is anything you would like to see in this blog.

Please feel free to leave a comment, I’m always looking for suggestions/improvements.

Leave a Reply

Your email address will not be published. Required fields are marked *