网上很多搭建opensips的文章,在参考多篇文章后,自己动手搭建opensips,搭建成功。
下面把自己的搭建细节记录如下:
一、安装依赖包
在编译opensips前,先安装相应的依赖包,否则在编译时会频繁的报错。
为避免安装依赖包时,找不到相应的源,请先更新ubuntu的软件包:
$ sudo apt-get update
1、安装部分依赖包
$ sudo apt-get install build-essential bison flex libncurses5 libncurses5-dev perl libdbi-perl libdbd-mysql-perl libdbd-pg-perl libfrontier-rpc-perl libterm-readline-gnu-perl libberkeleydb-perl
2、安装mysql
在编译opensips前一定要先安装好mysql,否则编译好opensip后,创建数据库时会报错。
$ sudo apt-get install mysql-server mysql-client
$ sudo apt-get install libmysqlclient-dev
二、编译安装opensips
进入官网下载相应的tar包,并解压,链接:http://download.opensips.org/
1、编译前设置opensips
在opensips-2.3.2目录中运行下面命令,开始配置opensips
$ make menuconfig
在出现的配置界面中通过上下按键移动光标,按回车键依次选择:Configure Compile Options --> Configure Excluded Modules
出现下面的界面,按空格键,选择:db_mysql
按q返回上一级界面。并选择:Configure Install Prefix 进入,配置opensips编译成功后的安装目录
设置成功后,选择:Save Changes, 并按Enter键保存设置
按q返回上一级界面,选择:Exit & Save All Changes,保存所有的设置,并退出该设置界面。
2、编译opensips
继续运行:
$ make menuconfig
进入,menuconfig配置界面,选择:Compile And Install OpenSIPS,按回车键开始编译安装。
三、配置opensips
进入先前设置的Configure Install Prefix目录(默认是/usr/local/opensips)下的sbin目录。如:/usr/local/opensips/sbin/
1、生成配置脚本
运行命令:
$ ./osipsconfig
选择:Generate OpenSIPS Script --> Residential Script --> Configure Residential Script,进入
按空格键,选择:
[*] ENABLE_TCP
[*] USE_DBACC
[*] USE_DIALOG
[*] USE_NAT
按q退出返回上一级界面,选择:Save Residential Script,保存设置。
再选择:Generate Residential Script,生成脚本。
脚本生成成功,放在下面的目录:
/usr/local/opensips/etc/opensips/opensips_residential_xxx.cfg
2、配置opensips.cfg脚本
进入/usr/local/opensips/etc/opensips目录,可以看到刚才生成的脚本文件
运行下面命令,把刚生成的脚本文件覆盖原来的脚本文件:opensips.cfg
$ sudo mv opensips_residential_xxx.cfg opensips.cfg
编辑脚本文件:opensips.cfg
$sudo vi opensips.cfg
修改下面的代码:
/* 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 revers DNS on IPs */
auto_aliases=no
listen=tcp:127.0.0.1:5060 # CUSTOMIZE ME
listen=udp:127.0.0.1:5060 # CUSTOMIZE ME
####### Modules Section ########
#set module path
mpath="/usr/opensips//lib64/opensips/modules/"
修改红色字体标注的listen的ip为服务器的IP地址,tcp和udp都要修改,端口号为需要监听的socket端口号,保存退出。
3、配置opensipsctlrc脚本
编辑脚本文件:opensipsctlrc(脚本文件还是在/usr/local/opensips/etc/opensips目录)
$ sudo vi opensipsctlrc
修改SIP_DOMAIN为服务器的ip地址:
## your SIP domain
SIP_DOMAIN=xxx.xxx.xxx.xxx
去除下面代码的注释:
DBENGINE=MYSQL
DBHOST=localhost
DBNAME=opensips
DB_PATH="/usr/local/etc/opensips/dbtext"
DBRWUSER=opensips
DBRWPW="opensipsrw"
DBROOTUSER="root"
USERCOL="username"
保存退出
四、新建opensips数据库
进入/usr/opensips/sbin目录
运行命令:
$ sudo ./ opensipsdbctl create
在创建数据库的过程中,会提示需要设置数据库的密码。
五、创建用户
创建opensips的用户,用于拨打电话使用。
$ sudo opensipsctl add 12345 12345
前面的12345为用户名,后面的12345为密码
六、安装并配置mediaproxy(根据需要安装和配置,不安装opensips也能正常运行)
MediaProxy是一个媒体中继,配合opensips完成rtp ,rtcp媒体流的转发。如果端到端无法完成NAT的穿越,使用mediaproxy完成媒体数据的转发是个不错的选择。
MediaProxy官网:http://mediaproxy.ag-projects.com/
官方安装指导是基于Debian系统,可以参考:http://mediaproxy.ag-projects.com/installation-guide/
1、导入key
$sudo wget http://download.ag-projects.com/agp-debian-gpg.key
$sudo apt-key add agp-debian-gpg.key
2、添加下载源
$sudo vi /etc/apt/sources.list
在最后位置添加,并保存退出。
deb http://ag-projects.com/ubuntu trusty main
deb-src http://ag-projects.com/ubuntu trusty main
注意:在Ubuntu12.04中的源如下,但是这个源在14.04中并不能使用:
deb http://ag-projects.com/ubuntu precise main
deb-src http://ag-projects.com/ubuntu precise main
3、安装mediaproxy
先更新源
$apt-get update
安装
$sudo apt-get install mediaproxy-dispatcher mediaproxy-relay mediaproxy-web-sessions
如果有启用tls,需要拷贝key,
进入/etc/mediaproxy/tls目录
$cd /etc/mediaproxy/tls/
$sudo cp /usr/share/doc/mediaproxy-common/tls/* ./
4、配置mediaproxy
$sudo vi /etc/mediaproxy/config.ini
修改如下项目,IP地址为服务器的IP,假设IP为:10.10.10.10:
[Relay]
dispatchers = 10.10.10.10
passport = None
relay_ip = 10.10.10.10
port_range = 50000:60000
log_level = DEBUG
stream_timeout = 90
on_hold_timeout = 7200
reconnect_delay = 10
traffic_sampling_period = 15
[Dispatcher]
socket_path = dispatcher.sock
listen = 10.10.10.10
listen_management = 10.10.10.10
management_use_tls = yes
passport = None
management_passport = None
log_level = DEBUG
relay_timeout = 5
[TLS]
certs_path = tls
verify_interval = 300
[Database]
[Radius]
[OpenSIPS]
socket_path = /var/run/opensips/socket
max_connections = 10
5、启动mediaproxy
$sudo service mediaproxy-dispatcher start
$sudo service mediaproxy-relay start
如果启动过程中有报如下错误:
fatal error: IP forwarding is not available or not enabled (check /proc/sys/net/ipv4/ip_forward)
请修改如下文件,
$sudo vi /etc/sysctl.conf
修改下面的值为1,如果没有该行,就新增加一行:
net.ipv4.ip_foward=1
保存退出,并执行下面命令,更改配置:
$sudo sysctl -p /etc/sysctl.conf
6、在opensip.cfg配置文件中添加加载mediaproxy模块
注意:MediaProxy模块的加载顺序必须在dialog模块之后
#### MediaProxy module
loadmodule "mediaproxy.so"
modparam("mediaproxy", "disable", 0)
modparam("mediaproxy", "mediaproxy_socket", "/var/run/mediaproxy/dispatcher.sock")
modparam("mediaproxy", "mediaproxy_timeout", 1000)
modparam("mediaproxy", "signaling_ip_avp", "$avp(nat_ip)")
modparam("mediaproxy", "media_relay_avp", "$avp(media_relay)")
modparam("mediaproxy", "ice_candidate", "low-priority")
7、在opensip.cfg配置文件中添加对mediaproxy的调用
蓝色加粗字体为需要增加的代码,编辑器的关系,可能看不到加粗代码,查找注释为:#added for mediaproxy
#
# 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:
# http://www.opensips.org/Resources/DocsCookbooks
# for a explanation of possible statements, functions and parameters.
#
####### Global Parameters #########
log_level=4
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 revers DNS on IPs */
auto_aliases=no
listen=udp:10.10.10.10:5060 # CUSTOMIZE ME
listen=tcp:10.10.10.10:5060 # CUSTOMIZE ME
####### Modules Section ########
#set module path
mpath="/usr/opensips//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", 0)
#### REGISTRAR module
loadmodule "registrar.so"
modparam("registrar", "tcp_persistent_flag", "TCP_PERSISTENT")
modparam("registrar", "received_avp", "$avp(received_nh)")
/* 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:opensipsrw@localhost/opensips") # CUSTOMIZE ME
#### DIALOG 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:opensipsrw@localhost/opensips") # CUSTOMIZE ME
#### NAT modules
loadmodule "nathelper.so"
modparam("nathelper", "natping_interval", 10)
modparam("nathelper", "ping_nated_only", 1)
modparam("nathelper", "sipping_bflag", "SIP_PING_FLAG")
modparam("nathelper", "sipping_from", "sip:[email protected]") #CUSTOMIZE ME
modparam("nathelper", "received_avp", "$avp(received_nh)")
loadmodule "rtpproxy.so"
modparam("rtpproxy", "rtpproxy_sock", "udp:localhost:12221") # CUSTOMIZE ME
#### MediaProxy module
loadmodule "mediaproxy.so"
modparam("mediaproxy", "disable", 0)
modparam("mediaproxy", "mediaproxy_socket", "/var/run/mediaproxy/dispatcher.sock")
modparam("mediaproxy", "mediaproxy_timeout", 1000)
modparam("mediaproxy", "signaling_ip_avp", "$avp(nat_ip)")
modparam("mediaproxy", "media_relay_avp", "$avp(media_relay)")
modparam("mediaproxy", "ice_candidate", "low-priority")
loadmodule "proto_udp.so"
loadmodule "proto_tcp.so"
####### Routing Logic ########
# main request routing logic
route{
force_rport();
if (nat_uac_test("23")) {
if (is_method("REGISTER")) {
fix_nated_register();
setbflag(NAT);
} else {
fix_nated_contact();
setflag(NAT);
}
}
if (!mf_process_maxfwd_header("10")) {
sl_send_reply("483","Too Many Hops");
exit;
}
if (has_totag()) {
# sequential request withing a dialog should
# take the path determined by record-routing
if (loose_route()) {
# validate the sequential request against dialog
if ( $DLG_status!=NULL && !validate_dialog() ) {
xlog("In-Dialog $rm from $si (callid=$ci) is not valid according to dialog\n");
## exit;
}
if (is_method("BYE")) {
# do accounting even if the transaction fails
do_accounting("db","failed");
} else if (is_method("INVITE")) {
# even if in most of the cases is useless, do RR for
# re-INVITEs alos, as some buggy clients do change route set
# during the dialog.
record_route();
}
if (check_route_param("nat=yes"))
setflag(NAT);
# route it out to whatever destination was set by loose_route()
# in $du (destination URI).
route(relay);
} else {
if ( is_method("ACK") ) {
if ( t_check_trans() ) {
# non loose-route, but stateful ACK; must be an ACK after
# a 487 or e.g. 404 from upstream server
t_relay();
exit;
} else {
# ACK without matching transaction ->
# ignore and discard
exit;
}
}
sl_send_reply("404","Not here");
}
exit;
} else {
#added for mediaproxy
if (is_method("INVITE") && !has_totag()) {
engage_media_proxy();
}
}
# CANCEL processing
if (is_method("CANCEL"))
{
if (t_check_trans())
t_relay();
exit;
}
t_check_trans();
if ( !(is_method("REGISTER") ) ) {
if (from_uri==myself)
{
} else {
# if caller is not local, then called number must be local
if (!uri==myself) {
send_reply("403","Rely forbidden");
exit;
}
}
}
# preloaded route checking
if (loose_route()) {
xlog("L_ERR",
"Attempt to route with preloaded Route's [$fu/$tu/$ru/$ci]");
if (!is_method("ACK"))
sl_send_reply("403","Preload Route denied");
exit;
}
# record routing
if (!is_method("REGISTER|MESSAGE"))
record_route();
# account only INVITEs
if (is_method("INVITE")) {
# create dialog with timeout
if ( !create_dialog("B") ) {
send_reply("500","Internal Server Error");
exit;
}
do_accounting("db");
}
if (!uri==myself) {
append_hf("P-hint: outbound\r\n");
route(relay);
}
# requests for my domain
if (is_method("PUBLISH|SUBSCRIBE"))
{
sl_send_reply("503", "Service Unavailable");
exit;
}
if (is_method("REGISTER"))
{
if ( proto==TCP || 0 ) setflag(TCP_PERSISTENT);
if (isflagset(NAT)) {
setbflag(SIP_PING_FLAG);
}
if (!save("location"))
sl_reply_error();
exit;
}
if ($rU==NULL) {
# request with no Username in RURI
sl_send_reply("484","Address Incomplete");
exit;
}
# do lookup with method filtering
if (!lookup("location","m")) {
t_newtran();
t_reply("404", "Not Found");
exit;
}
if (isbflagset(NAT)) setflag(NAT);
# when routing via usrloc, log the missed calls also
do_accounting("db","missed");
route(relay);
}
route[relay] {
# for INVITEs enable some additional helper routes
if (is_method("INVITE")) {
if (isflagset(NAT)) {
rtpproxy_offer("ro");
}
t_on_branch("per_branch_ops");
t_on_reply("handle_nat");
t_on_failure("missed_call");
}
if (isflagset(NAT)) {
add_rr_param(";nat=yes");
}
if (!t_relay()) {
send_reply("500","Internal Error");
};
exit;
}
branch_route[per_branch_ops] {
xlog("new branch at $ru\n");
}
onreply_route[handle_nat] {
if (nat_uac_test("1"))
fix_nated_contact();
if ( isflagset(NAT) )
rtpproxy_answer("ro");
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;
##}
}
local_route {
if (is_method("BYE") && $DLG_dir=="UPSTREAM") {
acc_db_request("200 Dialog Timeout", "acc");
}
}
七、启动opensips
运行下面命令:
$ sudo opensipsctl start