centos或ubuntu部署OpenSips

参考

Centos7安装opensips超详细教程
centos7 部署opensips信令服务器
【死磕opensips】sip协议解析
开源SIP Kamailio OpenSIPS的四种均衡负载算法详解和SBC呼叫路由
基于SIP协议的性能测试——奇林软件kylinPET
OpenSIPS实战(一):OpenSIPS使用简介
Sip服务器搭建全过程(Linphone拨号)

关于via和Record-Route的理论

SIP系列讲座-关于VIA和Record-Route header
SIP route与 record_route /SIP路由机制解析

关于rport机制

国标视频sip协议的rport机制

概述

竞争对手Kamailio、Asterisk
https://github.com/kamailio/kamailio/blob/master/src/core/utils/srjson.h

安装依赖

centos

# 如果已经存在不需要安装
yum install gcc gcc-c++
# 如果已经存在不需要安装
yum install make
yum install flex bison ncurses ncurses-devel
yum install -y gcc make bison openssl-devel pkgconfig mysql-devel libcurl-devel pcre-devel zlib-devel
yum install -y redis

ubuntu

# apt update
# apt install gcc -y
# apt install g++ -y
# apt install build-essential -y
//mysql必须为5.7版本,服务端已经装好docker,这里就不装了,若要安装,则安装下,要去配置文件中注释监听地址 # 127.0.0.1
// #apt-get install mysql-server 
# apt install mysql-client
# apt install libmysqlclient-dev -y
# apt install pkg-config
# apt install libssl-dev

# apt-get install perl libdbi-perl libdbd-mysql-perl libdbd-pg-perl libfrontier-rpc-perl libterm-readline-gnu-perl libberkeleydb-perl

# apt-get install bison flex libncurses5 libncurses5-dev

下载

访问官网https://opensips.org/pub/opensips/latest/找最新版本
下载https://opensips.org/pub/opensips/latest/opensips-3.3.5.tar.gz

cd /usr/local/opensips
wget https://opensips.org/pub/opensips/2.4.11/opensips-2.4.11.tar.gz
# 解压
tar -zxvf opensips-2.4.11.tar.gz
# 切换目录
cd  opensips-2.4.11

选择安装模块(db_mysql)

1、在opensips源码根目录执行make menuconfig,出现配置界面
2、箭头移动到“Configure Compile Options”,按右方向键→进入“Configure Excluded Modules”
3、移动到“db_mysql”项,按空格键选中,选中出现“*”号

数据库版本5.7

mysql8.0会提示mysql授权语句错误

配置安装目录

再按左方向键←回到上一页,移动到“Configure Install Prefix”,再按并按右方向键→,输入opensips安装目录为/usr/local/opensips/opensips2,回车保存。
默认位置:/usr/local/share/opensips/
默认启动项:/usr/local/sbin/下面
默认配置项:/usr/local/etc/opensips/

如果配置了Install Prefix,比如/usr/local/opensips/opensips2 则
位置:/usr/local/opensips/opensips2
启动项:/usr/local/opensips/opensips2/sbin/下面
认配置项:/usr/local/opensips/opensips2/etc/opensips/

保存配置项

再按左方向键←回到上一页,移动到“Save Changes ”,并按右方向键→,执行保存操作。

安装

回到上一页选择“Compile And Install OpenSIPS”,Enter执行编译。

配置日志

OpenSIPS实战(二):日志文件配置
OpenSIPS可以设置写入的设施,设施配置对应的log文件路径。默认配置的是log_facility=LOG_LOCAL0。如下配置:

vim  /etc/rsyslog.conf
# 增加一行:
local0.*   /opt/opensips.log
# 一定要创建并授权
cd /opt
touch opensips.log
sudo chmod -R 777 /opt/opensips.log
# 然后重启rsyslog服务:
/etc/init.d/rsyslog restart #(如果删除了日志文件,也要再次执行这个命令才行)

需要注意的是只有opensips.cfg配置debug_mode=no及log_stderror=no时日志才会写文件。

配置数据库

 cd /usr/local/etc/opensips/ # 或者自定义目录/usr/local/opensips/opensips2/etc/opensips
 vim opensipsctlrc

修改如下:
在这里插入图片描述
创建opensips数据库

//进入文件夹
# cd /usr/local/sbin
//创建数据库,这时候如果mysql版本为8.0以上的话会列出一系列编码选择,得mysql5.7版本不会出现这问题
opensipsdbctl create # 或者 sudo ./opensipsdbctl create
//表创建完成后(提示的两个问题都选n)

执行完成之后发现有很多表
在这里插入图片描述

配置文件 opensips.cfg

#
# OpenSIPS residential configuration script
#     by OpenSIPS Solutions <[email protected]>
#
# This script was generated via "make menuconfig", from
#   the "Residential" scenario.
# You can enable / disable more features / functionalities by
#   re-generating the scenario with different options.#
#
# Please refer to the Core CookBook at:
#      https://opensips.org/Resources/DocsCookbooks
# for a explanation of possible statements, functions and parameters.
#


####### Global Parameters #########

log_level=3
log_stderror=no
log_facility=LOG_LOCAL0

children=4

/* uncomment the following lines to enable debugging */
#debug_mode=yes

/* uncomment the next line to enable the auto temporary blacklisting of 
   not available destinations (default disabled) */
#disable_dns_blacklist=no

/* uncomment the next line to enable IPv6 lookup after IPv4 dns 
   lookup failures (default disabled) */
#dns_try_ipv6=yes

/* comment the next line to enable the auto discovery of local aliases
   based on reverse DNS on IPs */
auto_aliases=no


#listen=udp:127.0.0.1:5060   # CUSTOMIZE ME
#listen=tcp:127.0.0.1:5060   # CUSTOMIZE ME
listen=udp:192.168.0.44:5060   # CUSTOMIZE ME
listen=tcp:192.168.0.44:5060   # CUSTOMIZE ME
#listen=udp:0.0.0.0:5060   # CUSTOMIZE ME
#listen=tcp:0.0.0.0:5060   # CUSTOMIZE ME


####### Modules Section ########

#set module path
mpath="/usr/local/opensips/opensips2/lib64/opensips/modules/"
#mpath="/usr/local//lib64/opensips/modules/"

#### SIGNALING module
loadmodule "signaling.so"

#### StateLess module
loadmodule "sl.so"

#### Transaction Module
loadmodule "tm.so"
modparam("tm", "fr_timeout", 5)
modparam("tm", "fr_inv_timeout", 30)
modparam("tm", "restart_fr_on_each_reply", 0)
modparam("tm", "onreply_avp_mode", 1)

#### Record Route Module
loadmodule "rr.so"
/* do not append from tag to the RR (no need for this script) */
modparam("rr", "append_fromtag", 0)

#### MAX ForWarD module
loadmodule "maxfwd.so"

#### SIP MSG OPerationS module
loadmodule "sipmsgops.so"

#### FIFO Management Interface
loadmodule "mi_fifo.so"
modparam("mi_fifo", "fifo_name", "/tmp/opensips_fifo")
modparam("mi_fifo", "fifo_mode", 0666)

#### URI module
loadmodule "uri.so"
modparam("uri", "use_uri_table", 0)

#### MYSQL module
loadmodule "db_mysql.so"

#### USeR LOCation module
loadmodule "usrloc.so"
modparam("usrloc", "nat_bflag", "NAT")
modparam("usrloc", "db_mode",   2)
modparam("usrloc", "db_url","mysql://opensips:[email protected]:3316/opensips") # CUSTOMIZE ME


#### REGISTRAR module
loadmodule "registrar.so"
modparam("registrar", "tcp_persistent_flag", "TCP_PERSISTENT")
/* uncomment the next line not to allow more than 10 contacts per AOR */
#modparam("registrar", "max_contacts", 10)

#### ACCounting module
loadmodule "acc.so"
/* what special events should be accounted ? */
modparam("acc", "early_media", 0)
modparam("acc", "report_cancels", 0)
/* by default we do not adjust the direct of the sequential requests.
   if you enable this parameter, be sure the enable "append_fromtag"
   in "rr" module */
modparam("acc", "detect_direction", 0)
modparam("acc", "db_url","mysql://opensips:[email protected]:3316/opensips") # CUSTOMIZE ME

#### AUTHentication modules
loadmodule "auth.so"
loadmodule "auth_db.so"
modparam("auth_db", "calculate_ha1", yes)
modparam("auth_db", "password_column", "password")
modparam("auth_db|uri", "db_url","mysql://opensips:[email protected]:3316/opensips") # CUSTOMIZE ME
modparam("auth_db", "load_credentials", "")

#### DOMAIN module
loadmodule "domain.so"
modparam("domain", "db_url",	"mysql://opensips:[email protected]:3316/opensips") # CUSTOMIZE ME
modparam("domain", "db_mode", 1)   # Use caching
modparam("auth_db|usrloc|uri", "use_domain", 1)

loadmodule "proto_udp.so"
loadmodule "proto_tcp.so" 


#### dispatcher module
loadmodule "dialog.so"
modparam("dialog", "dlg_match_mode", 1)
modparam("dialog", "default_timeout", 21600)  # 6 hours timeout
modparam("dialog", "db_mode", 2)
modparam("dialog", "db_url", "mysql://opensips:[email protected]:3316/opensips") 

#### dispatcher module
loadmodule "dispatcher.so"
modparam("dispatcher", "db_url", "mysql://opensips:[email protected]:3316/opensips")
modparam("dispatcher", "ds_ping_method", "OPTIONS")
modparam("dispatcher", "ds_ping_interval", 5)
modparam("dispatcher", "ds_probing_threshhold", 2)
modparam("dispatcher", "ds_probing_mode", 1)

#### load_balancer module
loadmodule "load_balancer.so"
modparam("load_balancer", "db_url", "mysql://opensips:[email protected]:3316/opensips")
modparam("load_balancer", "probing_method", "OPTIONS")
modparam("load_balancer", "probing_interval", 30)



####### Routing Logic ########

# main request routing logic
route{
    
    
		xlog("new request in \n");
        if (!mf_process_maxfwd_header("10")) {
    
    
                sl_send_reply("483","Too Many Hops");
                exit;
        }
        if (!has_totag()) {
    
    
                record_route();
        }
        else {
    
    
                loose_route();
                t_relay();
                exit;
        }
        if (is_method("CANCEL")) {
    
    
                if ( t_check_trans() )
                        t_relay();
                exit;
        }
        if (is_method("INVITE")) {
    
    
                if (!load_balance("1","pstn","1")) {
    
    
                        send_reply("503","Service Unavailable");
                        exit;
                }
        }
        else if (is_method("1REGISTER")) {
    
    

#        		if (!load_balance("1","pstn","1")) {
    
    
#				  xlog("REGISTER at 1\n");
#                      exit; 
#               }
			xlog("REGISTER at 1\n");
#			rewritehostport("192.168.0.44:15692");
#			t_relay();

			seturi("sip:192.168.0.44:15692");
			append_hf("X-My-Custom-Header: 192.168.0.44:15692/route2\r\n");
   			forward();
			
#                if (!ds_select_dst("1", "4")) {
    
    
#                        send_reply("503","Service Unavailable");
#                        exit;
#                }
        }
        else if (is_method("REGISTER")) {
    
    
			xlog("REGISTER at 2\n");
#			t_on_reply("handle_invite_reply");
#		    lb_add("destination.group", "sip:192.168.0.44:15692");
#		    lb_add("destination.group", "sip:192.168.0.44:15692");
#		    load_balance(1, "RR");
			seturi("sip:192.168.0.44:15692");
		append_hf("Record-Route: <sip:$si;lr>\r\n");
		append_hf("Via: SIP/2.0/UDP 192.168.0.102:5066;branch=z9hG4bK8ac5a;rport\r\n");
#		append_hf("Via: SIP/2.0/UDP 192.168.0.44:5060;branch=z9hG4bK8ac5a;rport;received=192.168.0.102\r\n");
#		append_hf("Via: SIP/2.0/UDP 192.168.0.102:5066;branch=z9hG4bK8ac5a;rport;received=192.168.0.102\r\n");
#    		t_relay();
   			forward();

        }
        else {
    
    
                send_reply("405","Method Not Allowed");
                exit;
        }
        if (!t_relay()) {
    
    
                sl_reply_error();
        }
}


route[relay] {
    
    
	# for INVITEs enable some additional helper routes
	if (is_method("INVITE")) {
    
    
		
		

		t_on_branch("per_branch_ops");
		t_on_reply("handle_nat");
		t_on_failure("missed_call");
	}

	

	if (!t_relay()) {
    
    
		send_reply("500","Internal Error");
	}
	exit;
}




branch_route[per_branch_ops] {
    
    
	xlog("new branch at $ru\n");
}


onreply_route[handle_nat] {
    
    
	
	xlog("incoming reply\n");
}


failure_route[missed_call] {
    
    
	if (t_was_cancelled()) {
    
    
		exit;
	}

	# uncomment the following lines if you want to block client 
	# redirect based on 3xx replies.
	##if (t_check_status("3[0-9][0-9]")) {
    
    
	##t_reply("404","Not found");
	##	exit;
	##}

	
}




生成特定配置文件

# 进入目录
cd /usr/local/etc/opensips
# osipsconfig
./osipsconfig

操作内容
//依次选择—> Generate OpenSIPS Script —> Residential Script —> Configure Residential Script

//选中如下几项[] ENABLE_TCP[] USE_AUTH[] USE_DBACC[] USE_DBUSRLOC[*] USE_DIALOG

//按q返回,选择 —> Generate Residential Script 回车,生成新的配置文件,文件格式为opensips_residential_xxxxx.cfg,按qqq退出命令,生成新的配置文件

备份旧文件,修改新文件

# 备份原配置文件
# mv opensips.cfg opensipsold.cfg1
# 用生成的opensips_residential_xxx.cfg替换原先的opensips.cfg:
mv opensips_residential_2021-7-21_6:35:0.cfg opensips.cfg
#  编辑新生成的配置文件,修改监听端口和ip,ip把127.0.0.1改为自己的ip,保存
vim opensips.cfg

在这里插入图片描述

注意如果自定义了安装路径,需要修改模块地址
mpath=“/usr/local/opensips/opensips2/lib64/opensips/modules/”
#mpath=“/usr/local//lib64/opensips/modules/”

修改数据库配置

在这里插入图片描述

创建用户

opensipsctl add 1000 123456  //创建账号1000 密码123456
opensipsctl add 1001 123456  //创建账号1001 密码123456
//创建账号将在表subscriber新增一条记录,也可以在数据库直接修改

启动

常用命令
cd /usr/local/sbin
sudo opensipsctl start 启动服务

sudo opensipsctl stop 停止服务

sudo opensipsctl restart 重启服务

查询在线用户

./opensipsctl online

负载均衡

https://www.cnblogs.com/abnk/p/14357930.html
https://www.renrendoc.com/paper/166036126.html
https://www.dandelioncloud.cn/article/details/1499035824284049410

常见错误

ERROR: database engine not specified, please setup one in the config script

是因为opensipsctlrc配置文件中没有设置mysql信息

性能测试kylinPET

猜你喜欢

转载自blog.csdn.net/Blueeyedboy521/article/details/130644291
今日推荐