OpenSIPS is an Open Source carrier-grade SIP proxy/server used for SIP signaling and can handle all types of SIP operations. It’s being used by a lot of telco providers, ITSP and carriers because of its reliability and performance.
So, If you guys are looking for handling thousands of concurrent calls and SIP sessions then OpenSIPS is your way out. There are tons of different scenarios where openSIPS can play well, in your infrastructure. Some of them are –
- OpenSIPS as SIP Edge Proxy
- OpenSIPS as Mid Registrar or Main Registrar Server
- OpenSIPS as Inbound and Outbound Gateway
- OpenSIPS as Load Balancer
And a lot more.
Note: OpenSIPS only handles SIP Signaling not the media. For media handling you can use RTPPROXY or RTPENGINE.
Being a modular, powerful and flexible SIP server, OpenSIPS can be installed on all major Linux distros. In this tutorial, we gonna be using Debian 9 as our base operating system. So these are the steps which we need to follow, to get our first OpenSIPS instance up and running –
- Installing all the dependencies needed.
- Clone the latest OpenSIPS repo.
- Compile it
- Create configuration files
- Configuring openSIPS service
- Setup OpenSIPS-CLI
- Creating openSIPS database
- Starting OpenSIPS 3 Server
So before we start installing dependencies, we need to first understand how we will set up our OpenSIPS instance. Well, a basic OpenSIPS server doesn’t need much libraries or dependencies but we gonna be using OpenSIPS control panel later along with MYSQL support that’s why we will install some other dependencies so that we can easily compile some extra OpenSIPS modules.
Step 1: Installing all the dependencies
Let’s update all the package lists we have.
Now let’s install all the dependencies, along with MySQL (MariaDB) server.
apt install git gcc bison flex make openssl perl libdbi-perl libdbd-mysql-perl libdbd-pg-perl libfrontier-rpc-perl libterm-readline-gnu-perl libberkeleydb-perl mysql-server ssh libxml2 libxml2-dev libxmlrpc-core-c3-dev libpcre3 libpcre3-dev subversion libncurses5-dev git ngrep libssl-dev net-tools
Once we have all these packages installed, we now need to install devel packages for the MYSQL server.
For MariaDB –
apt install libmariadbclient-dev-compat sudo
For MYSQL Server –
apt install libmysqlclient-dev
Now we need to install dependencies for the micro httpd server which OpenSIPS gonna use for listening to JSON RPC connections.
apt install libmicrohttpd-dev pkg-config libjsoncpp-dev libjson-c-dev
ln -s /usr/include/jsoncpp/json/ /usr/include/json
dpkg -i libjson*
All the dependencies should be installed now and we are ready to clone openSIPS 3 source code.
Note: Don't forget to set msyql root username and password using - mysql_secure_installation
Step 2: Clone the OpenSIPS repo
We will be cloning the latest OpenSIPS 3 repo under /usr/src
cd /usr/src ; git clone https://github.com/OpenSIPS/opensips.git -b 3.0 opensips-3.0
Step 3: Compile the source code
To compile the OpenSIPS modules or source code first, go inside the opensips-3.0 directory.
Now type –
This will open the OpenSIPS Main Configuration Menu. From this menu, we can compile OpenSIPS and can generate our configuration scripts. So before we compile we need to include some extra module which we need for MYSQL and JSON support.
Now let’s select some extra modules which we need. So go to Configure Compile Options -> Configure Excluded Module
We have to select 4 modules – db_mysql , dialplan , json , httpd
Note: If you guys want to select or compile any other module, please make sure to install its dependencies.
Now go back using Left Arrow Key and Save Changes. And once we have our modules selected for compilation, then simply select Compile and Install OpenSIPS
After a successful compilation, you will ask to press any key to return to the main menu.
Step 4: Generating the OpenSIPS configuration script.
OpenSIPS configuration script is the brain of the OpenSIPS server, it controls all the routing, SIP traffic handling, registrations and pretty much all SIP operations. So it’s very important for us to generate it from the configuration menu. After compiling all the modules, go to Generate OpenSIPS Script.
We can generate 3 types of scripts from the configuration menu –
1. Residential Script – Provides User Registration
2. Trunking Script – Suitable for providing trunking service. It doesn’t support registrations.
3. Load-Balancer Script – Used for load balancing incoming and outgoing calls.
Note: These config files gonna have basic configuration. You always have to edit those files to make it work according to your scenario.
For this setup, we gonna use Trunking Script.
Now go to Configure Script
Select the features which you want to add in your script.
After selecting all the options needed, Save the Script and Select Generate Trunking Script.
And that’s all. Now go back and Saves All Changes.
Step 5: Configuring OpenSIPS service
It’s very important to understand the OpenSIPS file structure and important directories before start working on it. These are some main directories which we will be working on in this entire setup.
- /usr/local/etc/opensips/ # OpenSIPS default configuration file directory
- /usr/local/lib64/opensips/modules/ # OpenSIPS Modules Directory
- /etc/init.d/ # OpenSIPS service file directory
- /etc/default/opensips # OpenSIPS service config file
We can find opensips.init and opensips.default file under /usr/src/opensips-3.0/packaging/debian/ directory . So we need to copy those files under /etc/init.d/ and /etc/default/ directory.
cp /usr/src/opensips-3.0/packaging/debian/opensips.init /etc/init.d/opensips
cp /usr/src/opensips-3.0/packaging/debian/opensips.default /etc/default/opensips
chmod 755 /etc/init.d/opensips update-rc.d opensips defaults 99 mkdir -p /var/run/opensips
Now we have everything set up and ready to start our first OpenSIPS service, but before we do that we need to copy our config file in /usr/local/etc/opensips/ directory which we created from OpenSIPS configuration menu. That configuration file is located under /usr/src/opensips-3.0/etc/
Now we will copy that trunking config file under – /usr/local/etc/opensips/
mv /usr/src/opensips-3.0/etc/opensips_trunking* /usr/local/etc/opensips/opensips_trunk.cfg
Now you can see that we have opensips_trunk.cfg under /usr/local/etc/opensips directory and we gonna use the same file for our OpenSIPS server, so we have to give the path of that in opensips init file.
Note: We also have to make sure that we have correct OpenSIPS binary path in opensips init file.
We have to add script path and daemon path to DAEMON and CFGFILE variable.
Step 6: Setup OpenSIPS-CLI
Starting from OpenSIPS 3, we will be using OpenSIPS-CLI tool to manage OpenSIPS instances. Earlier we use to work with opensipsctl script but that has been removed from OpenSIPS version 3. We will be using this application to do all kind of different operations like sending MI commands, creating database and a lot more.
Let’s clone the opensips-cli repo under /usr/src/
cd /usr/src && git clone https://github.com/OpenSIPS/opensips-cli.git
Now let’s install all the dependencies we need for this handy little application.
sudo apt install python3 python3-pip python3-dev gcc default-libmysqlclient-dev
sudo pip3 install mysqlclient sqlalchemy sqlalchemy-utils pyOpenSSL
Once we have all dependencies installed, it’s time to install OpenSIPS-CLI.
sudo python3 setup.py install clean
After a successful install, we will be able to execute opensips-cli command.
Note: When opensips-cli starts, it always look for opensips-cli.cfg file under /etc/ directory. If file is not present, it will start with default settings.
Now we will create opensips-cli.cfg file in /etc/ directory.
[opensips-1] log_level: WARNING prompt_name: opensips-cli prompt_intro: Welcome to OpenSIPS at SECUREVOIP prompt_emptyline_repeat_cmd: False history_file: ~/.opensips-cli.history history_file_size: 1000 output_type: pretty-print communication_type: fifo fifo_file: /tmp/opensips_fifo database_path: /usr/src/opensips-3.0/scripts/ database_url: mysql://root:password@localhost database_name: opensips
We have to add the above block in opensips-cli.cfg file. You can customize all the settings in the config file. Syntax and meaning of each variable is as follow –
[opensips-1] # Instance name
log_level # Log verbosity
prompt_name # Shell name
prompt_intro # Shell welcome message
history_file # Where to store opensips-cli history
output_type # output options, you can use json too
communication_type # Communication type – fifo , json
database_path # Database scripts path
database_url # URL of your MYSQL database
database_name # Database to be used
Now let’s start opensips-cli with our new config file.
Syntax - opensips-cli -i [instance_name] -f [config file]
Step 7: Creating OpenSIPS database
Finally, it’s time to create an OpenSIPS database. We will be using MYSQL backend if you guys wish to use another backed like PostgreSQL you may have to install some dependencies.
First open, opensips-cli and follow these steps –
- Start OpenSIPS-CLI
- Execute – database create
- Enter your MYSQL database URL
Now let’s verify the opensips database and tables by login into mysql.
Note - It's recommended to create a separate mysql user to acess opensips database.
Step 8: Starting OpenSIPS 3 Server
Now it’s time to finally start our OpenSIPS server, but before we do that we need to configure some parameters in opensips_trunk.cfg file.
- We need to update the MySQL URL of all the modules in the script with our MySQL username and password.
- We need to make sure that we have the correct module path entered in the config script.
- Update the PATH variable in /etc/init.d/opensips file
Once we have done that, the last step is to set the RUN_OPENSIPS variable to yes.
Note: Make sure the user opensips exists in your OS, or you can add it by - useradd -r opensips
After updating config in default file we need to do systemctl daemon-reload.
So now we are ready to start our opensips service, first, let’s make sure that we don’t have any bad configuration in our OpenSIPS config file.
opensips -C -f /usr/local/etc/opensips/opensips_trunk.cfg
Now let’s start OpenSIPS service –
systemctl start opensips systemctl enable opensips
If you have followed all the steps and installed all the dependencies, you will see opensips server running and listening on port 5060.
Note: By defualt, OpenSIPS use /var/log/syslog file for logging. So if you get any erros while starting the opensips service please refer that log file for troubleshooting.
We can verify that the OpenSIPS server is running or not in different ways.
1. By verifying opensips_fifo file under /tmp directory.
2. By executing MI commands via opensips-cli
You have successfully installed your first OpenSIPS server and I wish you all the best for your VoIP journey.
Thank you for reading this blog, if you guys have any questions or you got stuck anywhere please feel free to mention in the comment section below.