ubuntu下安裝配置opensips

opensips源碼能夠從官網下載。html

編譯安裝

$ tar -xzvf opensips-2.2.2.tar.gz
$ cd opensips-2.2.2/
$ sudo make menuconfig

menuconfig中選擇--->Configure Compile Options---> Configure Excluded Modules
,方向鍵向下滾動,按空格選中[*] db_mysql。按q鍵返回上一級,選擇---> Configure Install Prefix,輸入/usr/local/opensips_proxy後按回車(表示安裝在/usr/local/opensips_proxy目錄下)。 選擇---> Save Changes保存修改,按q返回,選擇---> Compile And Install OpenSIPS,回車安裝。python

安裝完成後會將配置文件放在/usr/local/opensips_proxy/etc/opensips目錄下,運行文件在/usr/local/opensips_proxy/sbin目錄下。若是出現依賴錯誤,先經過apt-get安裝依賴。源代碼安裝軟件要注意查看READMEINSTALL等文件,這些文件裏有很重要的說明和安裝信息,裏面有安裝Opensips所須要的依賴包。mysql

opensips安裝以後的文件目錄:linux

sbin/中的可執行命令:opensipsopensipsctlopensipsdbctlopensipsunixosipsconfigosipsconsole
etc/opensips/中的配置文件:opensips.cfgopensipsctlrcosipsconsolerc
lib/opensips/中的庫文件:modulesopensipsctl兩個目錄,modules爲當前opensips所支持的模塊。web

opensips配置

opensips的配置文件都在/etc/opensips/中,分別爲opensips.cfgopensipsctlrcosipsconsolercsql

opensips.cfg

opensips.cfg文件主要用於opensips啓動的配置,全部應用功能的配置都在這個文件中說明。該配置文件主要由三個部分組成:數據庫

a、全局變量,例如:ubuntu

listen=udp:127.0.0.1:5060
disable_tcp=yes
disable_tls=yes

b、設置加載模塊相應參數,如:session

loadmodule 「db_mysql.so」
loadmodule 「auth.so」
loadmodule 「auth_db.so」
modparam(「auth」, 「calculate_ha1」, yes)
modparam(「auth_db」, 「password_column」, 「password」)

c、路由策略和功能應用,如:app

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」);
}

opensipsctlrc

opensipsctlrc文件中包含了數據庫配置的信息。

進入/usr/local/opensips_proxy/etc/opensips目錄,運行osipsconfig命令,依次選擇---> Generate OpenSIPS Script —> Residential Script —> Configure Residential Script,選中以下幾項:

[*] ENABLE_TCP
[*] USE_ALIASES
[*] USE_AUTH
[*] USE_DBACC
[*] USE_DBUSRLOC
[*] USE_DIALOG
[*] USE_NAT

q返回,選擇---> Generate Residential Script回車,生成新的配置文件。按q(三次)退出命令,將新生成的opensips_residential_*.cfg文件重命名爲opensips.cfg,並編輯。

總共改動了3處:

#
# $Id$
#
# OpenSIPS residential configuration script
# by OpenSIPS Solutions <team@opensips-solutions.com>
#
# 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 #########

debug=3
log_stderror=no
log_facility=LOG_LOCAL0

fork=yes
children=4

/* uncomment the following lines to enable debugging */
#debug=6
#fork=no
#log_stderror=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

改動第1處
xxx.xxx.xxx.xxx.xxx是你本身的IP地址,不能夠寫爲127.0.0.1

listen=udp:xxx.xxx.xxx.xxx:5060   # CUSTOMIZE ME
listen=tcp:xxx.xxx.xxx.xxx:5060   # CUSTOMIZE ME

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

#set module path
mpath="/usr/local/opensips_proxy//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)
modparam("uri", "db_url",
"mysql://opensips:opensipsrw@localhost/opensips") # CUSTOMIZE ME

#### 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:opensipsrw@localhost/opensips") # CUSTOMIZE ME

#### 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", "failed_transaction_flag", "ACC_FAILED")
/* account triggers (flags) */
modparam("acc", "db_flag", "ACC_DO")
modparam("acc", "db_missed_flag", "ACC_MISSED")
modparam("acc", "db_url",
"mysql://opensips:opensipsrw@localhost/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:opensipsrw@localhost/opensips") # CUSTOMIZE ME
modparam("auth_db", "load_credentials", "")

#### ALIAS module
loadmodule "alias_db.so"
modparam("alias_db", "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:pinger@127.0.0.1") #CUSTOMIZE ME
modparam("nathelper", "received_avp", "$avp(received_nh)")

改動第2處
端口號22222能夠隨便修改,可是必須和後面安裝的rtpproxy的端口號相同

loadmodule "rtpproxy.so"
modparam("rtpproxy", "rtpproxy_sock", "udp:localhost:22222") # CUSTOMIZE ME

改動第3處
增長MediaProxy模塊,增長下邊這一段

#### 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")) {
setflag(ACC_DO); # do accounting ...
setflag(ACC_FAILED); # ... even if the transaction fails
} 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;
}

# CANCEL processing
if (is_method("CANCEL"))
{
if (t_check_trans())
t_relay();
exit;
}

t_check_trans();

if ( !(is_method("REGISTER")  ) ) {

if (from_uri==myself)

{

# authenticate if from local subscriber
# authenticate all initial non-REGISTER request that pretend to be
# generated by local subscriber (domain from FROM URI is local)
if (!proxy_authorize("", "subscriber")) {
proxy_challenge("", "0");
exit;
}
if (!db_check_from()) {
sl_send_reply("403","Forbidden auth ID");
exit;
}

consume_credentials();
# caller authenticated

} 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;
}

setflag(ACC_DO); # do accounting
}

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"))
{
# authenticate the REGISTER requests
if (!www_authorize("", "subscriber"))
{
www_challenge("", "0");
exit;
}

if (!db_check_to())
{
sl_send_reply("403","Forbidden auth ID");
exit;
}

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;
}

# apply DB based aliases
alias_db_lookup("dbaliases");

# do lookup with method filtering
if (!lookup("location","m")) {
if (!db_does_uri_exist()) {
send_reply("420","Bad Extension");
exit;
}

t_newtran();
t_reply("404", "Not Found");
exit;
}

if (isbflagset(NAT)) setflag(NAT);

# when routing via usrloc, log the missed calls also
setflag(ACC_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");

}
}

修改opensipsctlrc文件,去掉全部DB相關的註釋。

# $Id$
#
# The OpenSIPS configuration file for the control tools.
#
# Here you can set variables used in the opensipsctl and opensipsdbctl setup
# scripts. Per default all variables here are commented out, the control tools
# will use their internal default values.

## your SIP domain
SIP_DOMAIN=xxx.xxx.xxx.xxx  #你本身的IP地址

## chrooted directory
# $CHROOT_DIR="/path/to/chrooted/directory"

## database type: MYSQL, PGSQL, ORACLE, DB_BERKELEY, or DBTEXT,
## by default none is loaded
# If you want to setup a database with opensipsdbctl, you must at least specify
# this parameter.
DBENGINE=MYSQL

## database host
DBHOST=localhost

## database name (for ORACLE this is TNS name)
DBNAME=opensips

# database path used by dbtext or db_berkeley
# DB_PATH="/usr/local/etc/opensips/dbtext"

## database read/write user
DBRWUSER=opensips

## password for database read/write user
DBRWPW="opensipsrw"

## database super user (for ORACLE this is 'scheme-creator' user)
DBROOTUSER="root"

# user name column
# USERCOL="username"

opensips新建數據庫,增長域名及用戶:

cd /usr/local/opensips_proxy/sbin/
sudo ./opensipsdbctl create
sudo ./opensipsctl domain add xdty.org #若是沒有域名可不用添加
sudo ./opensipsctl add 10000 123456
sudo ./opensipsctl add 10001 123456

安裝配置rtpproxy

$ sudo apt-get install rtpproxy
$ sudo vi /etc/default/rtpproxy

修改成以下內容

# Defaults for rtpproxy

# The control socket.
#CONTROL_SOCK="unix:/var/run/rtpproxy/rtpproxy.sock"
# To listen on an UDP socket, uncomment this line:
CONTROL_SOCK=udp:127.0.0.1:22222

# Additional options that are passed to the daemon.
EXTRA_OPTS=""
LISTEN_ADDR=xxx.xxx.xxx.xxx #你本身的IP地址
EXTRA_OPTS="-l ${LISTEN_ADDR}"

啓動rtpproxy

$ sudo killall rtpproxy
$ sudo /etc/init.d/rtpproxy start

若是rtpproxy啓動失敗,請檢查/etc/init.d/rtpproxy腳本DAEMON路徑是否正確,默認DAEMON=/usr/sbin/$NAME,可能要改成DAEMON=/usr/bin/$NAME

安裝配置mediaproxy

導入源密鑰,增長mediaproxy的源到/etc/apt/sources.list

$ wget http://download.ag-projects.com/agp-debian-gpg.key
$ sudo apt-key add agp-debian-gpg.key
$ sudo vi /etc/apt/sources.list

最後位置添加

deb http://ag-projects.com/ubuntu precise main
deb-src http://ag-projects.com/ubuntu precise main

安裝mediaproxy

$ sudo apt-get update
$ sudo apt-get install mediaproxy-dispatcher mediaproxy-relay mediaproxy-web-sessions

media-relay須要內核支持ipv4 forwarding,因此須要執行(以root用戶執行):

$ sudo su
$ echo 1 > /proc/sys/net/ipv4/ip_forward

注意:能夠vi /proc/sys/net/ipv4/ip_forward查看裏面是否爲1,有時候仍是爲0,若是此時爲0,繼續執行一遍echo 1 > /proc/sys/net/ipv4/ip_forward,直到裏面的內容爲1。在文件/etc/sysctl.config中打開net.ipv4.ip_forward=1,這樣即使重啓設備也能夠運行mediaproxy了。

另外,mediadispatcherrelay之間須要經過tls通訊,故須要在/etc/mediaproxy/tls/中有認證文件,進入/etc/mediaproxy/tls目錄,拷本密鑰文件,修改配置文件。

$ cd /etc/mediaproxy/tls/
$ sudo cp /usr/share/doc/mediaproxy-common/tls/* .
$ cd ..
$ sudo vi config.ini

修改成相似以下內容

[Relay]
dispatchers = xxx.xxx.xxx.xxx #你本身的地址
passport = None
relay_ip = xxx.xxxx.xxx.xxx #你本身的IP地址
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 = xxx.xxx.xxx.xxx
listen_management = xxx.xxx.xxx.xxx #你本身的IP地址
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

啓動mediaproxy服務

$ sudo media-dispatcher restart
$ sudo media-relay restart

或者:

$ sudo service mediaproxy-dispatcher restart
$ sudo service mediaproxy-relay restart

查看mediaproxy是否正常運行:

$ ps -ef | grep media
root  6592  2110  0 14:25 ?00:00:01 /usr/bin/python /usr/bin/media-dispatcher restart
root  6969  2110  0 14:35 ?00:00:04 /usr/bin/python /usr/bin/media-relay restart

則說明已經正常啓動。若是沒有正常啓動能夠在/var/log/syslog中查看緣由。

啓動服務並檢驗

opensips的日誌保存在/var/log/opensips.log,修改日誌文件配置。

$ sudo touch /var/log/opensips.log
$ sudo chmod 777 /var/log/opensips.log
$ sudo vi /etc/rsyslog.d/opensips.conf

增長以下內容

local0.* /var/log/opensips.log

重啓syslog服務,啓動opensips

$ sudo service rsyslog restart
$ sudo /usr/local/opensips_proxy/sbin/opensipsctl start

若是啓動失敗,查看/var/log/opensips.log文件查找錯誤.

服務啓動後,防火牆開啓tcpudp的端口

$ iptables -I INPUT -p tcp –dport 5060 -j ACCEPT
$ iptables -I INPUT -p udp –dport 5060 -j ACCEPT
$ iptables-save

如今能夠用SIP客戶端登陸上面建立的賬號,測試了。

參考文章

ubuntu下opensips安裝配置
基於ubuntu中使用mysql實現opensips用戶認證的解決方法
Realtime OpenSIPS - Asterisk Integration

相關文章
相關標籤/搜索