How To: FreePBX on Raspbian

FreePBX is an opensource front-end for the Asterisk VoIP solution. It can be installed on a Raspberry Pi via a prebuilt image but what if you want to use it directly on Raspbian (or other distros)? Well, it is possible with a few minor tweaks.

FreePBX

Because FreePBX and Asterisk are tightly bound it is a permissions nightmare to get the web interface to work with any other user than ‘asterisk’. If you already have Apache running for other purposes then this is obviously not ideal. For this reason we need to get second Apache process running as the ‘asterisk’ user, while at the same time allowing the main Apache process to run as is. Here’s how you do that.

To start with the instructions provided by FreePBX for version 13.0 and Debian 8.1 work just fine up to a point. You can find them at this link here.

You could just follow their instructions to the letter and they would work, however, it is highly likely you have Apache already running on Raspbian for other tasks, such as serving up web pages or other web based interfaces. If this is the case you probably don’t want to be changing the user it runs as.

This solution will also work for Debian, Ubuntu, CentOS etc but might need slightly tweaked.

In the official instructions there is a section titled ‘A few small modifications to Apache’.

STOP HERE! Don’t run this part:

A few small modifications to Apache.
sed -i 's/\(^upload_max_filesize = \).*/\120M/' /etc/php5/apache2/php.ini
cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf_orig
sed -i 's/^\(User\|Group\).*/\1 asterisk/' /etc/apache2/apache2.conf
sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
service apache2 restart

If you were to run those commands you would break your already working Apache configuration. Instead, we’re going to fire up a new Apache process.

Note: It might also be possible to work around this by not running PHP as a module but as a FastCGI executable (such as with PHP-FPM).  Feel free to give that a go instead of using this method.

We are going to copy and modify the Apache configuration directory and tweak it. Firstly, copy the configuration directory to make a new one.

cp -a /etc/apache2 /etc/apache2_freebpx

Next, clean up any site configuration you may already have in place by removing the symlinks from sites-enabled and any custom configuration files etc.

In order to change the user Apache runs as and to allow a second instance to run there are a few required changes.

In /etc/apache2_freepbx/ports.conf change the port:

Listen 4080

In /etc/apache2_freepbx/envvars change a few things:

        SUFFIX=_freepbx
export APACHE_RUN_USER=asterisk
export APACHE_RUN_GROUP=asterisk

You will notice there are multiple variables which end in ‘$SUFFIX’. These will now have _freepbx appended to them, making them unique and therefore different from our main Apache process.

In /etc/apache2_freepbx/sites-enabled/000-default change the port:

<VirtualHost *:4080>

Then run two of the suggested modifications as these are still required.

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

Finally, we need to start this new Apache process. To do this we need the following commands.

source /etc/apache2_freepbx/envvars
mkdir /var/run/apache2_freepbx/
apache2 -f /etc/apache2_freepbx/apache2.conf &

Tada! You should now have Apache running as ‘asterisk’ and listening on port 4080. You can browse to your Pi’s IP via a web browser at the address ‘http://192.168.x.x:4080’ and you should see the FreePBX interface.

In order to ensure this runs on start up you can add the last three commands we can to start Apache to the end of ‘/etc/rc.local’ – ensure they are at the bottom, before the ‘exit’.

You may wish to access FreePBX via your existing Apache server, for example if you want to use the standard ports, say 443 for SSL. To do this we can use the proxy module for Apache. To do this we need a new virtual host.

For example, create /etc/apache2/sites-available/freepbx.example.com with something like this:

<VirtualHost 192.168.xxx.xxx:80>
 ServerAdmin freepbx.example.com
 ServerName freepbx.example.com
 ErrorLog /dev/null
 CustomLog /dev/null combined
 ProxyRequests On
 ProxyPreserveHost On
 ProxyPass / "http://192.168.xxx.xxx:4080/"
 ProxyPassReverse / "http://192.168.xxx.xxx:4080/"
</Virtualhost>

Then enable the site and the proxy module with:

a2enmod proxy
a2enmod proxy_http
a2ensite freepbx.example.com
service apache2 reload

You should be able to access http://freepbx.example.com/ which is proxying to the other Apache instance on port 4080.