How to Set Up an Internal Phone Network with FreePBX and Asterisk

On
FreePBX phone network

If you want to set up an internal VoIP phone network, FreePBX and Asterisk enable you to do so with minimum effort. It's pocket-friendly, easy to configure, and can be scaled to any level. Tech-savvy home users and small to medium-sized businesses can use this setup to deploy a robust and reliable VoIP phone network for internal usage. In this guide, we will deploy the VoIP phone network using both software tools on a Linux system. We'll also discuss security and troubleshooting tips related to the VoIP phone network. So, let's get started and create a secure and reliable private VoIP phone network.

FreePBX phone network
📷 Credit: DALL·E 3

The tutorial given below can be applied to other software solutions as well. All you need to do is follow the generic flow of the setup. Working knowledge of the Internet, telephony and Linux is required.

Read Also:
Recording Telegram Calls: A Complete How-To Guide

Before moving ahead, let's take a look at a simple diagram giving you an overview of the VoIP phone network we're going to set up. There can be a few changes depending on your exact requirements.

VoIP internal phone network using FreePBX and Asterisk
📷 A macro overview of the VoIP phone network

Now that we have a surface-level understanding of our VoIP phone network, let's move forward toward the actual process to implement it.

Prerequisites for the Set-Up

Here's a list of hardware and software requirements to build our private phone network.

  1. Hardware/VM Requirements:
    • A dedicated server with 4GB RAM and at least 20GB of storage.
    • Make sure your server has a multi-core CPU and Asterisk utilizes these cores effectively for concurrent calls.
    • Acquire a static IP address to ensure there are no connectivity issues. It greatly helps in avoiding call drops.
  2. Operating System:
    • Clean installation of one of the following Linux distros. CentOS 8/9, Ubuntu 22.04 LTS, or Debian 11. Avoid installing desktop environment to keep the system lean and performant. At the time of reading this, if FreePBX and Asterisk support newer versions of these operating systems, go ahead with those releases.
  3. Network:
    • The following ports are required for the server. 5060 (SIP), 10000-20000 (RTP), and 80/443 (HTTP/HTTPS for FreePBX).
    • A firewall application is required. You can use ufw on Ubuntu and firewalld on CentOS.
  4. SIP Devices:
  5. Router Configuration:
    • Make sure your router's port forwarding feature works. You may use it for creating remote extensions.
    • Also, Quality of Service (QoS) should be in an enabled state to ensure the VoIP traffic is prioritized.

Step 1: Install Asterisk and FreePBX

We'll start with installing both Asterisk and FreePBX. It's a straightforward process and may take a few minutes to complete. Let's get started!

1.1 Update and Prepare the System

An update is necessary to ensure our Linux system has the latest packages to avoid dependency conflicts during the installation process.

# For a Debian/Ubuntu system:  
sudo apt update && sudo apt upgrade -y  
sudo reboot  # Reboot if kernel updates are applicable 

# For a CentOS/RHEL system:  
sudo yum update -y  
sudo reboot

1.2 Install Required Dependencies

And now let's install the dependencies required for successfully installing both our primary applications.

# On a Debian/Ubuntu system:  
sudo apt install -y wget nano curl net-tools libnewt-dev libssl-dev libncurses5-dev libsqlite3-dev libgdbm-dev zlib1g-dev libjansson-dev libxml2-dev uuid-dev libedit-dev  

# On a CentOS/RHEL system:  
sudo yum install -y wget nano curl net-tools newt-devel openssl-devel ncurses-devel sqlite-devel gdbm-devel zlib-devel jansson-devel libxml2-devel libuuid-devel libedit-devel

In these dependencies, libssl-dev is essential for enabling secure SIP (TLS/SRTP), and libjansson-dev is required to make Asterisk's JSON API work—out of the box.

1.3 Install and Configure MariaDB

It's time to install MariaDB on your Linux machine. Let's install and configure it for FreePBX.

# On a CentOS 7 system:
sudo yum install -y mariadb-server

# On a CentOS 8 and 9 system:
sudo dnf install -y mariadb-server

# On a Ubuntu system:
sudo apt install -y mariadb-server

Now start and enable the MariaDB service.

# On a CentOS system:
sudo systemctl start mariadb
sudo systemctl enable mariadb

# On a Ubuntu system:
sudo systemctl start mysql
sudo systemctl enable mysql

Now execute the following script to set a root password and secure your MariaDB instance.

sudo mysql_secure_installation

Finally, log in to the mariaDB shell and create a database for the FreePBX application.

sudo mysql -u root -p

CREATE DATABASE yourdbname;
GRANT ALL PRIVILEGES ON yourdbname.* TO 'freepbxuser'@'localhost' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
EXIT;

Replace yourdbname with the preferred database name you want to use for your FreePBX data.

1.4 Download and Install Asterisk

Follow the process given below to install Asterisk on your Linux server.

cd /usr/src/  
sudo wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-20-current.tar.gz # Replace '20' with the current LTS version available
sudo tar -xvzf asterisk-20-current.tar.gz  
cd asterisk-20.*  

# Configure with required modules:  
./configure --with-pjproject-bundled --with-jansson-bundled  
make menuselect  # Optional: Customize modules (e.g., to enable codec like G.729)  
make  
sudo make install  
sudo make samples  
sudo make config  
sudo ldconfig

You can use the make menuselect command to enable MP3 support for voicemail recordings. Or, to use PJPROJECT library for WebRTC.

In case you are recompiling due to some errors, use the make clean command before restarting the installation process.

1.5 Install FreePBX

To install FreePBX, fire the following commands.

cd /usr/src/  
sudo wget https://mirror.freepbx.org/modules/packages/freepbx/freepbx-16.0-latest.tgz # Replace the version (16) number with the release you want to use
sudo tar -xvzf freepbx-16.0-latest.tgz  
cd freepbx  
sudo ./install -n --dbhost=localhost --dbname=yourdbname --dbuser=freepbxuser --dbpass=your_password

1.6 Configure Apache

I'm presuming that the Apache web server is already installed on your Linux system. Let's configure it quickly to host the FreePBX web interface.

First of all, check and ensure that Apache service is running.

# On a CentOS system:
sudo systemctl start httpd
sudo systemctl enable httpd

# On a Ubuntu system:
sudo systemctl start apache2
sudo systemctl enable apache2

Thereafter, make sure to set the correct file permissions for FreePBX.

sudo chown -R asterisk:asterisk /var/www/html
sudo chmod -R 755 /var/www/html

Open the Apache configuration file to change the override setting.

sudo nano /etc/httpd/conf/httpd.conf  # On a CentOS system
sudo nano /etc/apache2/apache2.conf   # On a Ubuntu system

In the file, change AllowOverride None to AllowOverride All for the /var/www/html directory.

Finally, restart the Apache server.

sudo systemctl restart httpd       # On a CentOS system
sudo systemctl restart apache2     # On a Ubuntu system

Troubleshooting Tips:

  • If you ever experience errors, inspect the /var/log/asterisk/ and /var/log/httpd/ log files.
  • Often missing PHP extensions play foul. Check and install the missing PHP extensions, if any.
  • If you are using CentOS Linux, ensure SELinux is disabled. Use the following command to disable it.

    sudo setenforce 0

Installation of FreePBX is complete at this point. Let's configure it!

Step 2: Configure FreePBX

It's time to configure FreePBX, to build our internal phone network. Follow the steps given below.

2.1 Access the FreePBX Web Interface

Open the URL http://<server-ip>/admin. The IP address of your Linux server should be used here.

The setup will prompt you to complete the set up that includes creation of admin account and configuration of basic setings. Use a strong password for the admin account.

After that, ensure you have enabled HTTPS through the SettingsAdvanced SettingsWeb Server option.

2.2 Start FreePBX Services

In the next step, start and enable the FreePBX services:

sudo fwconsole start
sudo fwconsole reload

# To ensure FreePBX services start on boot:
sudo systemctl enable asterisk

2.3 Secure Your FreePBX Installation

Security is of prime importance when it comes to installing and configuring such critical applications. To secure your FreePBX server, do the following.

Read Also:
Linux Hardening Guide: How to Protect Your System from Hackers

  1. Configure your firewall to restrict access to 80, 443, and 5060 ports.
  2. Use Let's Encrypt to install an SSL certificate on your server.

2.4 Configure SIP Trunks (VoIP Provider Integration)

In the next step, we'll configure SIP trunks to integrate the VoIP service provider details into our FreePBX instance. Here's how to do it.

  1. First of all, go to the ConnectivityTrunksAdd SIP Trunk option.
  2. Now, enter your VoIP provider details. Here's a generic example of the same.
    • Trunk Name: VoIP_Provider_Name
    • Outgoing Settings:
      • PEER Details:
        host=voip-provider-host-name.com  
        type=peer  
        username=your_username  
        secret=your_password  
        context=from-trunk  
        insecure=port,invite
    • Incoming Settings:
      • USER Context: your_DID_number
      • USER Details: type=user
  3. Codec Priorities: Set to ulaw, alaw, GSM under the SettingsAsterisk SIP Settings option.

2.5 Create Extensions

You have to create all the extensions you want in your phone network. Connections and routes are configured first before you even attempt to add IP phones to the network. Here we go.

  1. First of all, go to the ApplicationsExtensionsAdd New SIP Extension option.
  2. Best Practices for Extension Creation:
    • Always use numeric extensions (e.g., 2005, 2006)
    • Use strong passwords for these extensions.
    • If a firewall is in place, enable NAT—without fail.

2.6 Configure Inbound/Outbound Routes

After creating extensions, create both inbound and outbound routes. Plan your routes as per your requirements creating a list beforehand with all the required data.

  • Inbound Route Example:
    • Route Name: Primary_DID
    • DID Number: 4573459239 (example assigned DID)
    • Destination: Extension 2005
  • Outbound Route Example:
    • Route Name: HR_Calls
    • Route Pattern: NXXNXXXXXX (should match 10-digit numbers)
    • Trunk Sequence: Select the SIP trunk

Step 3: Connect IP Phones

Now that almost everything is configured (except the advanced features), let's connect IP phones to our network. In this example, I'm adding a Zoiper softphone to give you an idea about the configuration. If you are using any other IP phone, refer to the user manual of the same.

Zoiper Configuration

  1. Start with the ZoiperSettingsAccountsAdd Account option.
  2. Manual Configuration:
    • Domain: <server-ip-address>
    • Username: 2005
    • Password: Extension's_secure_password (password created during step 2.5)
    • Transport: UDP (or TCP/TLS for encryption)
  3. Test Registration:
    • To test the IP phone registration, open FreePBX ReportsAsterisk CLI and run:
      sip show peers  # Should list your extension as "OK"

Step 4: Advanced Features & Security

If you want to add a few advanced features and looking to harden the security of your FreePBX server, you can do so after completeting the initial configuration. Let's do it!

  1. Voicemail:
    • If you want to enable voicemail service for all the created extensions, you can do so through the ApplicationsVoicemail Admin option.
    • You can also configure email notifications for voicemail delivery.
  2. IVR (Auto Attendant):
    • Creating an IVR through FreePBX and Asterisk is not hard at all. You can create a menu under ApplicationsIVR (e.g., Press 1 for HR, 2 for Services).
  3. Security Hardening:
    • Firewall: The strongest security measure is to block relevant ports on your FreePBX server (e.g., sudo ufw deny 5060 if you are using TLS).
    • Fail2Ban: Install this tool to block brute-force attacks. Use the sudo apt install fail2ban command to do so.
    • SIP TLS/SRTP: Encryption configuration can be done through the SettingsAsterisk SIP Settings option.

Step 5: Testing and Troubleshooting

In this last section, we'll take a look at some of the testing and troubleshooting tips. It'll help you identify errors and successfully install and maintain your internal phone network.

Pro Tip: Always back up your FreePBX configuration through the AdminBackup & Restore option. Do it at regular intervals—without any fail.

  1. Test Calls:
    • Internal: Dial between all extensions.
    • External: You must also test both inbound and outbound calls through your SIP trunk.
  2. Debugging:
    • Asterisk CLI: Invoke the CLI environment through asterisk -rvvvvv and use the sip set debug on command to troubleshoot SIP traffic issues.
    • Log Checking: Another place to check for problems and errors are the logs. They contain tons of information you can use to debug errors. Use the tail -f /var/log/asterisk/full command to inspect Asterisk logs.
  3. Common Issues:
    • One-Way Audio: This problem often arises due to incorrect NAT settings. To fix it, go to the SIP SettingsNAT option.
    • Registration Failures: Sometimes, your IP phones do not get registered when listing the attached devices. To fix it, double-check the password and see if any firewall rule is conflicting with your configuration.

Generally, once you get it up and running, the chances of errors when extending or enhancing your current PBX configuration are very low.

Conclusion

This guide provides a production-ready setup for FreePBX/Asterisk. By incorporating security measures, advanced routing, and scalability options, you can build a system that rivals commercial PBX solutions. Future enhancements could include WebRTC integration, call center modules, or integration with CRM tools.