Bootstrap

Kamailio-SIP代理服务器单节点的安装与部署

使用背景:存在 Freeswitch 等语音网关应用服务,期望实现负载均衡、前置处理的功能,类似于Web应用服务的Nginx的角色

市场调研,发现了几家颇受欢迎的开源产品:

  • Kamailio
  • OpenSIPS
  • Asterisk

Kamailio和OpenSIPS由于其灵活性、高性能和强大的路由能力,在开源SIP服务器领域尤其受到欢迎。

基于以上背景,从头开始学习并了解Kamailio.

Kamailio 是什么?

在这里插入图片描述

Kamailio是一个:

  • SIP Server,SIP服务器
  • SIP Proxy Server,SIP代理服务器
  • SIP Registrar Server,SIP注册服务器
  • SIP Location Server,SIP地址查询服务器
  • SIP Redirection Server,SIP重定向服务器
  • SIP Application Server,SIP应用服务器
  • SIP Loadbalance Server,SIP负载均衡服务器
  • SIP WebSocket Server,SIP WebSocket服务器
  • SIP SBC Server,SIP SBC服务器

Kamailio基于GPLv2+开源协议发布,主要处理SIP协议,当你发现你的需求要对SIP报文类似于插桩、修饰的时候,你就应该想到这类产品。

Kamailio 开源项目地址,拥有2.2K的 Star, 也是不错的成绩。

当然,了解Kamailio 这个SIP服务器的前提就是,你知道什么是SIP。
很贴心的,官方还给专门写了一篇文章,作为你了解Kamailio的背景知识。这篇文章我也有学习和整理

官方也推荐了其他的SIP知识学习网站:

  • siptutorial.net
  • tutorialspoint.com
  • voip-info.org

书籍:

  • SIP Beyond VoIP
  • Understanding the Session Initiation Protocol
  • SIP Demystified
  • Internet Communications Using SIP

认识它,从源码安装、使用开始 5.8.x

光看文章介绍,总是有点干巴巴,也有关于他的中文文档,但是看着四年多没有更新,讲的内容又很基础,我们直接就从英文文档入手吧,基础的是入门指南,想要了解更多当然要阅读CookBook-5.5.x,你还可以在这里找到其他版本的CookBook

目前2024年最新版本是在 5.8.x,那么我们就跟着入门指南实践部署一个基于Debian(uBuntu下)单机版本源码构建Kamailio(Debian和Centos也有构建好的安装方式,可以自行选择)

  • Kamailio 5.8.2
  • MySQL 5.7(8.x不行,初始化语法不兼容),提前准备好
  • git指令
  • 一个Linux服务器

官方5.8 源码安装可参考

获取源码

创建目录

mkdir -p ~/kamailio-5.8
cd ~/kamailio-5.8

git上分支从3.1-5.8/master开发版,可以自行选择,我这边检出5.8最新稳定版本

git clone --depth 1 --no-single-branch git://git.kamailio.org/kamailio kamailio
cd kamailio
git checkout -b 5.8 origin/5.8

配置项目

在kamailio文件夹下,src文件夹是我们需要关注的,后续的编译配置都在这个目录下

在src文件夹下,项目结构是这样的:

¥:~/kamailio-5.8/kamailio/src$ ls -l
total 9608
-rw-r--r--   1 kama kama   36045 Jun 14 11:19 Makefile
-rw-r--r--   1 kama kama     327 Jun 14 11:19 Makefile.cfg
-rw-r--r--   1 kama kama   61022 Jun 14 11:19 Makefile.defs
-rw-r--r--   1 kama kama     619 Jun 14 11:19 Makefile.dirs
-rw-r--r--   1 kama kama   15180 Jun 14 11:19 Makefile.groups
-rw-r--r--   1 kama kama    4453 Jun 14 11:19 Makefile.libs
-rw-r--r--   1 kama kama    7161 Jun 14 11:19 Makefile.modules
-rw-r--r--   1 kama kama    1220 Jun 14 11:19 Makefile.radius
-rw-r--r--   1 kama kama    8184 Jun 14 11:19 Makefile.rules
-rw-r--r--   1 kama kama    1513 Jun 14 11:19 Makefile.shared
-rw-r--r--   1 kama kama     756 Jun 14 11:19 Makefile.sources
-rw-r--r--   1 kama kama    1824 Jun 14 11:19 Makefile.targets
-rw-r--r--   1 kama kama    3450 Jun 14 11:19 Makefile.utils
drwxr-xr-x   9 kama kama   12288 Jun 20 13:28 core
drwxr-xr-x  13 kama kama    4096 Jun 14 11:19 lib
-rw-r--r--   1 kama kama   91492 Jun 14 11:19 main.c
drwxr-xr-x 258 kama kama   12288 Jun 14 11:19 modules

在Makefile所在的项目根目录,初始化配置文件

make cfg

编译之后就会多出一些文件,这些也是在Makefile的脚本中可以有迹可循的

-rw-r--r--   1 kama kama      16 Jun 20 13:28 librpath.lst # 指定库文件的路径
-rw-r--r--   1 kama kama    6350 Jun 20 13:28 main.d # 帮助管理编译时的依赖关系
-rw-r--r--   1 root   root    479768 Jun 20 13:28 main.o # 编译后的二进制形式的目标文件 
-rw-r--r--   1 kama kama     966 Jun 14 11:20 makecfg.lst # 包含了编译Kamailio时需要的额外配置指令或变量定义
-rw-r--r--   1 kama kama    1885 Jun 26 09:50 modules.lst # 依赖模块的初始化文件,这个如果存在就会删了重新生成
-rw-r--r--   1 root   root      3600 Jun 20 13:27 config.mak # 用来指导后续的编译步骤(通常通过执行make命令)如何基于你的配置需求来构建Kamailio

其中 modules.lst 主要负责引入和排除加载的镜像,是通常使用过程中需要修改的,比如增加数据库模块、缓存模块、http请求模块

# this file is autogenerated by make modules-cfg

# the list of sub-directories with modules
modules_dirs:=modules

# the list of module groups to compile
cfg_group_include=

# the list of extra modules to compile
include_modules= app_lua db_mysql presence presence_conference presence_dialoginfo presence_mwi presence_profile presence_reginfo presence_xml http_async_client http_client

# the list of static modules
static_modules=

# the list of modules to skip from compile list
skip_modules=

# the list of modules to exclude from compile list
exclude_modules= acc_json acc_radius app_java app_mono app_perl app_python app_python3 app_python3s app_ruby app_ruby_proc auth_ephemeral auth_radius cdp cdp_avp cnxcc cplc crypto db2_ldap db_berkeley db_cassandra db_mongodb db_mysql db_oracle db_perlvdb db_postgres db_redis db_sqlite db_unixodbc dialplan dnssec erlang evapi gcrypt geoip geoip2 gzcompress h350 ims_auth ims_charging ims_dialog ims_diameter_server ims_icscf ims_ipsec_pcscf ims_isc ims_ocs ims_qos ims_registrar_pcscf ims_registrar_scscf ims_usrloc_pcscf ims_usrloc_scscf jansson janssonrpcc json jsonrpcc jwt kafka kazoo lcr ldap log_systemd lost lwsc memcached microhttpd misc_radius mqtt nats ndb_cassandra ndb_mongodb ndb_redis nghttp2 nsq osp outbound peering phonenum pua pua_bla pua_dialoginfo pua_json pua_reginfo pua_rpc pua_usrloc pua_xmpp rabbitmq regex rls rtp_media_server ruxc sctp secsipid secsipid_proc slack snmpstats stirshaken systemdops tls tls_wolfssl tlsa topos_redis utils uuid websocket xcap_client xcap_server xhttp_pi xmlops xmlrpc xmpp $(skip_modules)

modules_all= $(filter-out modules/CVS,$(wildcard modules/*))
modules_noinc= $(filter-out $(addprefix modules/, $(exclude_modules) $(static_modules)), $(modules_all))
modules= $(filter-out $(modules_noinc), $(addprefix modules/, $(include_modules) )) $(modules_noinc)
modules_configured:=1

可以直接打开文件进行编辑,或者使用命令的方式 make include_modules="app_lua db_mysql" exclude_modules="xxxxx" cfg

以上你如果选用了对应的模块,前提是你的环境已经安装了哦,比如已经安装了MySQL(我这边安装的是MySQL5.7),已经安装了Lua(我这边安装的是Lua5.4,liblua5.4-dev)

编译并安装项目

make all编译

$:~/kamailio-5.8/kamailio/src$ sudo make all
make[2]: 'libsrdb2.so.1.0' is up to date.
make[2]: 'libsrdb1.so.1.0' is up to date.
make[2]: 'libsrdb1.so.1.0' is up to date.
make[1]: 'acc_diameter.so' is up to date.
make[2]: 'libsrdb1.so.1.0' is up to date.
make[1]: 'app_jsdt.so' is up to date.
....

make install 安装

$:~/kamailio-5.8/kamailio/src$ sudo make install

安装完成后:

所有配置文件将位于:/usr/local/etc/kamailio,核心配置文件位于:/usr/local/etc/kamailio/kamailio.cfg

可执行文件位于: /usr/local/sbin/

  • kamailio - SIP 服务器脚本
  • kamdbctl - 创建和管理数据库的脚本,比如你使用MySQL作为其存储时就需要使用到这个
  • kamctl - 管理和控制SIP服务器的脚本
  • kamcmd - CLI 可以与SIP服务器交互的命令行接口

已经安装的模块位于:/usr/local/lib/kamailio/modules/,如果引用还有报包不存在的,就需要看看这里确认,没有的话编译安装可能要重来

一些文档位于: /usr/local/share/doc/kamailio/,/usr/local/share/man/man5/,/usr/local/share/man/man8/

日志输出位于: /var/log/syslog,好像有点疑惑为什么不是和其他的一样是关于kamailio的命名,这个可以配置

配置日志输出

  1. kamailio.cfg配置文件中,找到关于日志的配置行 log_facility=LOG_LOCAL0,确定没有注释掉
  2. 对于Unbuntu 22.04,找到/etc/rsyslog.d/50-default.conf文件,对syslog的配置进行调整
...
#注释掉这一行
#*.*;auth,authpriv.none    -/var/log/syslog 
#新加
*.*;auth,authpriv.none,local0.none    -/var/log/syslog  #追加local0
#新加
if $syslogfacility-text=='local0' then -/var/log/kamailio.log #将local0 输出到 kamailio.log,路径可以自定义 /var/log/kamailio/kamailio.log
...

配置MySQL数据库存储

默认情况下,不会引入任何数据库存储,我们这边是因为需要做持久化(也是正常使用的需求),前面modules已经引入 db_mysql的模块

/usr/local/etc/kamailio/kamctlrc

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

## the SIP domain
# SIP_DOMAIN=kamailio.org

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

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

## database host
DBHOST=127.0.0.1

## database port
DBPORT=3306

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

## database path used by dbtext, db_berkeley or sqlite
# DB_PATH="/usr/local/etc/kamailio/dbtext"

## database read/write user
DBRWUSER="kamailio"

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

## database read only user
DBROUSER="kamailioro"

## password for database read only user
DBROPW="kamailioropasswprd"

## database access host (from where is kamctl used)
# DBACCESSHOST=192.168.0.1

## database host for super user (useful for specifying a local socket or virtual hostname)
# defaults to value of DBHOST when not set
# DBROOTHOST="localhost"

## database port for super user (on some DB specifying the port will force TCP connections)
# default value will depend on client DB tool
# DBROOTPORT=""

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

## password for database super user
## - important: this is insecure, targeting the use only for automatic testing
## - known to work for: mysql
# DBROOTPW="dbrootpw"

## option to ask confirmation for all database creation steps
# DBINITASK=yes

## database character set (used by MySQL when creating database)
#CHARSET="latin1"

## user name column
# USERCOL="username"


## SQL definitions
## If you change this definitions here, then you must change them
## in db/schema/entities.xml too.
## FIXME

# FOREVER="2030-05-28 21:32:15"
# DEFAULT_Q="1.0"


## Program to calculate a message-digest fingerprint
# MD5="md5sum"

## awk tool
# AWK="awk"

## gdb tool
# GDB="gdb"

## If you use a system with a grep and egrep that is not 100% gnu grep compatible,
## e.g. solaris, install the gnu grep (ggrep) and specify this below.
##
## grep tool
# GREP="grep"

## egrep tool
# EGREP="egrep"

## sed tool
# SED="sed"

## tail tool
# LAST_LINE="tail -n 1"

## expr tool
# EXPR="expr"


## Describe what additional tables to install. Valid values for the variables
## below are yes/no/ask. With ask (default) it will interactively ask the user
## for an answer, while yes/no allow for automated, unassisted installs.

## If to install tables for the modules in the EXTRA_MODULES variable.
# INSTALL_EXTRA_TABLES=ask

## If to install presence related tables.
# INSTALL_PRESENCE_TABLES=ask

## If to install uid modules related tables.
# INSTALL_DBUID_TABLES=ask

## Define what module tables should be installed.
## If you use the postgres database and want to change the installed tables, then you
## must also adjust the STANDARD_TABLES or EXTRA_TABLES variable accordingly in the
## kamdbctl.base script.

## Kamailio standard modules
# STANDARD_MODULES="standard acc lcr domain group permissions registrar usrloc msilo
#                   alias_db uri_db speeddial avpops auth_db pdt dialog dispatcher
#                   dialplan"

## Kamailio extra modules
# EXTRA_MODULES="imc cpl siptrace domainpolicy carrierroute userblocklist htable purple sca"


## type of aliases used: DB - database aliases; UL - usrloc aliases
## - default: none
# ALIASES_TYPE="DB"

## control engine: RPCFIFO
## - default RPCFIFO
# CTLENGINE="RPCFIFO"

## path to FIFO file for engine RPCFIFO
# RPCFIFOPATH="/run/kamailio/kamailio_rpc.fifo"

## check ACL names; default on (1); off (0)
# VERIFY_ACL=1

## ACL names - if VERIFY_ACL is set, only the ACL names from below list
## are accepted
# ACL_GROUPS="local ld int voicemail free-pstn"

## check if user exists (used by some commands such as acl);
## - default on (1); off (0)
# VERIFY_USER=1

## verbose - debug purposes - default '0'
# VERBOSE=1

## do (1) or don't (0) store plaintext passwords
## in the subscriber table - default '1'
# STORE_PLAINTEXT_PW=0

## Kamailio START Options
## PID file path - default is: /run/kamailio/kamailio.pid
# PID_FILE=/run/kamailio/kamailio.pid

## Kamailio Startup Configuration File
## Default is: kamailio.cfg
# STARTUP_CONFIG_FILE=kamailio.cfg

## Extra start options - default is: not set
## example: start Kamailio with 64MB shared memory: STARTOPTIONS="-m 64"
# STARTOPTIONS=

开始通过脚本初始化数据库配置

$:/usr/local/etc/kamailio$ sudo  /usr/local/sbin/kamdbctl create
[sudo] password for root:
MySQL password for : #要求输入root的密码
INFO: test server charset
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
WARNING: Your current default mysql characters set cannot be used to create DB. Please choice another one from the following list:
armscii8
ascii
big5
binary
cp1250
cp1251
cp1256
cp1257
cp850
cp852
cp866
cp932
dec8
eucjpms
euckr
gb18030
gb2312
gbk
geostd8
greek
hebrew
hp8
keybcs2
koi8r
koi8u
latin1
latin2
latin5
latin7
macce
macroman
sjis
swe7
tis620
ujis
utf16
utf16le
utf32
Enter character set name:
latin1  # 默认的latin1 字符编码
INFO: creating database kamailio ... #自动开始创建数据库
mysql: [Warning] Using a password on the command line interface can be insecure.
INFO: granting privileges to database kamailio ... #自动开始授权
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
INFO: creating standard tables into kamailio ... #自动开始初始化表格
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
INFO: Core Kamailio tables successfully created.
Create the presence related tables? (y/n): y #创建关联表
INFO: creating presence tables into kamailio ...
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
INFO: Presence tables successfully created.
Create the tables for imc cpl siptrace domainpolicy carrierroute
                drouting userblocklist htable purple uac pipelimit mtree sca mohqueue
                rtpproxy rtpengine secfilter? (y/n): y
INFO: creating extra tables into kamailio ... #创建额外表
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
INFO: Extra tables successfully created.
Create the tables for uid_auth_db uid_avp_db uid_domain uid_gflags
                uid_uri_db? (y/n): y
INFO: creating uid tables into kamailio ... #创建uid表
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
INFO: UID tables successfully created.

以上就结束了,可以再使用root账户登录,确认是否初始化完毕。

配置 kamailio.cfg 并启动

/usr/local/etc/kamailio/kamailio.cfg 重点开启#!define WITH_MYSQL #!define WITH_AUTH #!define WITH_USRLOCDB

#!KAMAILIO
#!define WITH_MYSQL
#!define WITH_AUTH
#!define WITH_USRLOCDB
#!define WITH_ANTIFLOOD
#!define WITH_PRESENCE
# change next line to comment to disable logging
#!define WITH_ACCDB
#
# Kamailio SIP Server v5.8 - default configuration script
#     - web: https://www.kamailio.org
#     - git: https://github.com/kamailio/kamailio
#
# Direct your questions about this file to: <[email protected]>
#
# Refer to the Core CookBook at https://www.kamailio.org/wikidocs/
# for an explanation of possible statements, functions and parameters.
#
# Note: the comments can be:
#     - lines starting with #, but not the pre-processor directives,
#       which start with #!, like #!define, #!ifdef, #!endif, #!else, #!trydef,
#       #!subst, #!substdef, ...
#     - lines starting with //
#     - blocks enclosed in between /* */
# Note: the config performs symmetric SIP signaling
#     - it sends the reply to the source address of the request
#     - remove the use of force_rport() for asymmetric SIP signaling
#
# Several features can be enabled using '#!define WITH_FEATURE' directives:
#
# *** To run in debug mode:
#     - define WITH_DEBUG
#     - debug level increased to 3, logs still sent to syslog
#     - debugger module loaded with cfgtrace enabled
#
# *** To enable mysql:
#     - define WITH_MYSQL
#
# *** To enable authentication execute:
#     - enable mysql
#     - define WITH_AUTH
#     - add users using 'kamctl' or 'kamcli'
#
# *** To enable IP authentication execute:
#     - enable mysql
#     - enable authentication
#     - define WITH_IPAUTH
#     - add IP addresses with group id '1' to 'address' table
#
# *** To enable persistent user location execute:
#     - enable mysql
#     - define WITH_USRLOCDB
#
# *** To enable presence server execute:
#     - enable mysql
#     - define WITH_PRESENCE
#     - if modified headers or body in config must be used by presence handling:
#     - define WITH_MSGREBUILD
#
# *** To enable nat traversal execute:
#     - define WITH_NAT
#     - option for NAT SIP OPTIONS keepalives: WITH_NATSIPPING
#     - install RTPProxy: http://www.rtpproxy.org
#     - start RTPProxy:
#        rtpproxy -l _your_public_ip_ -s udp:localhost:7722
#
# *** To use RTPEngine (instead of RTPProxy) for nat traversal execute:
#     - define WITH_RTPENGINE
#     - install RTPEngine: https://github.com/sipwise/rtpengine
#     - start RTPEngine:
#        rtpengine --listen-ng=127.0.0.1:2223 ...
#
# *** To enable PSTN gateway routing execute:
#     - define WITH_PSTN
#     - set the value of pstn.gw_ip
#     - check route[PSTN] for regexp routing condition
#
# *** To enable database aliases lookup execute:
#     - enable mysql
#     - define WITH_ALIASDB
#
# *** To enable speed dial lookup execute:
#     - enable mysql
#     - define WITH_SPEEDDIAL
#
# *** To enable multi-domain support execute:
#     - enable mysql
#     - define WITH_MULTIDOMAIN
#
# *** To enable TLS support execute:
#     - adjust CFGDIR/tls.cfg as needed
#     - define WITH_TLS
#
# *** To enable JSONRPC over HTTP(S) support execute:
#     - define WITH_JSONRPC
#     - adjust event_route[xhttp:request] for access policy
#
# *** To enable anti-flood detection execute:
#     - adjust pike and htable=>ipban settings as needed (default is
#       block if more than 16 requests in 2 seconds and ban for 300 seconds)
#     - define WITH_ANTIFLOOD
#
# *** To load htable module execute:
#     - define WITH_HTABLE
#
# *** To block 3XX redirect replies execute:
#     - define WITH_BLOCK3XX
#
# *** To block 401 and 407 authentication replies execute:
#     - define WITH_BLOCK401407
#
# *** To enable VoiceMail routing execute:
#     - define WITH_VOICEMAIL
#     - set the value of voicemail.srv_ip
#     - adjust the value of voicemail.srv_port
#
# *** To enhance accounting execute:
#     - enable mysql
#     - define WITH_ACCDB
#     - add following columns to database
#!ifdef ACCDB_COMMENT
  ALTER TABLE acc ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
  ALTER TABLE acc ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
  ALTER TABLE acc ADD COLUMN src_ip varchar(64) NOT NULL default '';
  ALTER TABLE acc ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
  ALTER TABLE acc ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
  ALTER TABLE acc ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
  ALTER TABLE missed_calls ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
  ALTER TABLE missed_calls ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
  ALTER TABLE missed_calls ADD COLUMN src_ip varchar(64) NOT NULL default '';
  ALTER TABLE missed_calls ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
  ALTER TABLE missed_calls ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
  ALTER TABLE missed_calls ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
#!endif

####### Include Local Config If Exists #########
import_file "kamailio-local.cfg"

####### Defined Values #########

# *** Value defines - IDs used later in config
#!ifdef WITH_DEBUG
#!define DBGLEVEL 3
#!else
#!define DBGLEVEL 2
#!endif

#!ifdef WITH_MYSQL
# - database URL - used to connect to database server by modules such
#       as: auth_db, acc, usrloc, a.s.o.
#!trydef DBURL "mysql://kamailio:'kamailiorwpassword'@localhost/kamailio"
#!endif

#!ifdef WITH_MULTIDOMAIN
# - the value for 'use_domain' parameters
#!define MULTIDOMAIN 1
#!else
#!define MULTIDOMAIN 0
#!endif

#!ifdef WITH_ANTIFLOOD
# - hash table 'ipban' used to store blocked IP addresses
#!trydef WITH_HTABLE
#!endif

# - flags
#   FLT_ - per transaction (message) flags
#!define FLT_ACC 1
#!define FLT_ACCMISSED 2
#!define FLT_ACCFAILED 3
#!define FLT_NATS 5

#       FLB_ - per branch flags
#!define FLB_NATB 6
#!define FLB_NATSIPPING 7

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

/* LOG Levels: 3=DBG, 2=INFO, 1=NOTICE, 0=WARN, -1=ERR, ... */
debug=DBGLEVEL

/* set to 'yes' to print log messages to terminal or use '-E' cli option */
log_stderror=no

memdbg=5
memlog=5

log_facility=LOG_LOCAL0
log_prefix="{$mt $hdr(CSeq) $ci} "

/* number of SIP routing processes for each UDP socket
 * - value inherited by tcp_children and sctp_children when not set explicitely */
children=8

/* uncomment the next line to disable TCP (default on) */
# disable_tcp=yes

/* number of SIP routing processes for all TCP/TLS sockets */
# tcp_children=8

/* uncomment the next line to disable the auto discovery of local aliases
 * based on reverse DNS on IPs (default on) */
# auto_aliases=no

/* add local domain aliases - it can be set many times */
# alias="sip.mydomain.com"

/* listen sockets - if none set, Kamailio binds to all local IP addresses
 * - basic prototype (full prototype can be found in Wiki - Core Cookbook):
 *      listen=[proto]:[localip]:[lport] advertise [publicip]:[pport]
 * - it can be set many times to add more sockets to listen to */
listen=udp:127.0.0.1:5060

/* life time of TCP connection when there is no traffic
 * - a bit higher than registration expires to cope with UA behind NAT */
tcp_connection_lifetime=3605

/* upper limit for TCP connections (it includes the TLS connections) */
tcp_max_connections=2048

/* upper limit for TCP connections for one ip address - default 1024 */
#tcp_accept_iplimit=1024

#!ifdef WITH_JSONRPC
tcp_accept_no_cl=yes
#!endif

#!ifdef WITH_TLS
enable_tls=yes

/* upper limit for TLS connections */
tls_max_connections=2048

/* For OpenSSL 3 integration
 * functions calling libssl3 can be invoked in a transient thread
 * 0: disable threaded calls
 * 1: use thread executors for process#0 only
 * 2: no thread executors, but use atfork handler to reset thread-locals to NULL
 * 3: use thread executors for all processes */
tls_threads_mode=1
#!endif

/* set it to yes to enable sctp and load sctp.so module */
enable_sctp=no

####### Custom Parameters #########

/* These parameters can be modified at runtime via RPC interface
 * - see the documentation of 'cfg_rpc' module.
 *
 * Format: group.id = value 'desc' description
 * Access: $sel(cfg_get.group.id) or @cfg_get.group.id */

#!ifdef WITH_PSTN
/* PSTN GW Routing
 *
 * - pstn.gw_ip: valid IP or hostname as string value, example:
 * pstn.gw_ip = "10.0.0.101" desc "My PSTN GW Address"
 *
 * - by default is empty to avoid misrouting */
pstn.gw_ip = "" desc "PSTN GW Address"
pstn.gw_port = "" desc "PSTN GW Port"
#!endif

#!ifdef WITH_VOICEMAIL
/* VoiceMail Routing on offline, busy or no answer
 *
 * - by default Voicemail server IP is empty to avoid misrouting */
voicemail.srv_ip = "" desc "VoiceMail IP Address"
voicemail.srv_port = "5060" desc "VoiceMail Port"
#!endif

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

/* set paths to location of modules */
# mpath="/usr/local/lib64/kamailio/modules/"

# when using TLS with OpenSSL it is recommended to load this module
# first so that OpenSSL is initialized correctly
#!ifdef WITH_TLS
loadmodule "tls.so"
#!endif

#!ifdef WITH_MYSQL
loadmodule "db_mysql.so"
#!endif

#!ifdef WITH_JSONRPC
loadmodule "xhttp.so"
#!endif
loadmodule "jsonrpcs.so"
loadmodule "kex.so"
loadmodule "corex.so"
loadmodule "tm.so"
loadmodule "tmx.so"
loadmodule "sl.so"
loadmodule "rr.so"
loadmodule "pv.so"
loadmodule "maxfwd.so"
loadmodule "usrloc.so"
loadmodule "registrar.so"
loadmodule "textops.so"
loadmodule "textopsx.so"
loadmodule "siputils.so"
loadmodule "xlog.so"
loadmodule "sanity.so"
loadmodule "ctl.so"
loadmodule "cfg_rpc.so"
loadmodule "acc.so"
loadmodule "counters.so"

#!ifdef WITH_AUTH
loadmodule "auth.so"
loadmodule "auth_db.so"
#!ifdef WITH_IPAUTH
loadmodule "permissions.so"
#!endif
#!endif

#!ifdef WITH_ALIASDB
loadmodule "alias_db.so"
#!endif

#!ifdef WITH_SPEEDDIAL
loadmodule "speeddial.so"
#!endif

#!ifdef WITH_MULTIDOMAIN
loadmodule "domain.so"
#!endif

#!ifdef WITH_PRESENCE
loadmodule "presence.so"
loadmodule "presence_xml.so"
#!endif

#!ifdef WITH_NAT
loadmodule "nathelper.so"
#!ifdef WITH_RTPENGINE
loadmodule "rtpengine.so"
#!else
loadmodule "rtpproxy.so"
#!endif
#!endif

#!ifdef WITH_HTABLE
loadmodule "htable.so"
#!endif

#!ifdef WITH_ANTIFLOOD
loadmodule "pike.so"
#!endif

#!ifdef WITH_DEBUG
loadmodule "debugger.so"
#!endif

# ----------------- setting module-specific parameters ---------------


# ----- jsonrpcs params -----
modparam("jsonrpcs", "pretty_format", 1)
/* set the path to RPC fifo control file */
# modparam("jsonrpcs", "fifo_name", "/run/kamailio/kamailio_rpc.fifo")
/* set the path to RPC unix socket control file */
# modparam("jsonrpcs", "dgram_socket", "/run/kamailio/kamailio_rpc.sock")
#!ifdef WITH_JSONRPC
modparam("jsonrpcs", "transport", 7)
#!endif

# ----- ctl params -----
/* set the path to RPC unix socket control file */
# modparam("ctl", "binrpc", "unix:/run/kamailio/kamailio_ctl")

# ----- sanity params -----
modparam("sanity", "autodrop", 0)

# ----- tm params -----
# auto-discard branches from previous serial forking leg
modparam("tm", "failure_reply_mode", 3)
# default retransmission timeout: 30sec
modparam("tm", "fr_timer", 30000)
# default invite retransmission timeout after 1xx: 120sec
modparam("tm", "fr_inv_timer", 120000)

# ----- rr params -----
# set next param to 1 to add value to ;lr param (helps with some UAs)
modparam("rr", "enable_full_lr", 0)
# do not append from tag to the RR (no need for this script)
modparam("rr", "append_fromtag", 0)

# ----- registrar params -----
modparam("registrar", "method_filtering", 1)
/* uncomment the next line to disable parallel forking via location */
# modparam("registrar", "append_branches", 0)
/* uncomment the next line not to allow more than 10 contacts per AOR */
# modparam("registrar", "max_contacts", 10)
/* max value for expires of registrations */
modparam("registrar", "max_expires", 3600)
/* set it to 1 to enable GRUU */
modparam("registrar", "gruu_enabled", 0)
/* set it to 0 to disable Path handling */
modparam("registrar", "use_path", 1)
/* save Path even if not listed in Supported header */
modparam("registrar", "path_mode", 0)

# ----- acc params -----
/* what special events should be accounted ? */
modparam("acc", "early_media", 0)
modparam("acc", "report_ack", 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)
/* account triggers (flags) */
modparam("acc", "log_flag", FLT_ACC)
modparam("acc", "log_missed_flag", FLT_ACCMISSED)
modparam("acc", "log_extra",
        "src_user=$fU;src_domain=$fd;src_ip=$si;"
        "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
modparam("acc", "failed_transaction_flag", FLT_ACCFAILED)
/* enhanced DB accounting */
#!ifdef WITH_ACCDB
modparam("acc", "db_flag", FLT_ACC)
modparam("acc", "db_missed_flag", FLT_ACCMISSED)
modparam("acc", "db_url", DBURL)
modparam("acc", "db_extra",
        "src_user=$fU;src_domain=$fd;src_ip=$si;"
        "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
#!endif

# ----- usrloc params -----
modparam("usrloc", "timer_interval", 60)
modparam("usrloc", "timer_procs", 1)
modparam("usrloc", "use_domain", MULTIDOMAIN)
/* enable DB persistency for location entries */
#!ifdef WITH_USRLOCDB
modparam("usrloc", "db_url", DBURL)
modparam("usrloc", "db_mode", 2)
#!endif

# ----- auth_db params -----
#!ifdef WITH_AUTH
modparam("auth_db", "db_url", DBURL)
modparam("auth_db", "calculate_ha1", yes)
modparam("auth_db", "password_column", "password")
modparam("auth_db", "load_credentials", "")
modparam("auth_db", "use_domain", MULTIDOMAIN)

# ----- permissions params -----
#!ifdef WITH_IPAUTH
modparam("permissions", "db_url", DBURL)
modparam("permissions", "load_backends", 1)
#!endif

#!endif

# ----- alias_db params -----
#!ifdef WITH_ALIASDB
modparam("alias_db", "db_url", DBURL)
modparam("alias_db", "use_domain", MULTIDOMAIN)
#!endif

# ----- speeddial params -----
#!ifdef WITH_SPEEDDIAL
modparam("speeddial", "db_url", DBURL)
modparam("speeddial", "use_domain", MULTIDOMAIN)
#!endif

# ----- domain params -----
#!ifdef WITH_MULTIDOMAIN
modparam("domain", "db_url", DBURL)
/* register callback to match myself condition with domains list */
modparam("domain", "register_myself", 1)
#!endif

#!ifdef WITH_PRESENCE
# ----- presence params -----
modparam("presence", "db_url", DBURL)

# ----- presence_xml params -----
modparam("presence_xml", "db_url", DBURL)
modparam("presence_xml", "force_active", 1)
#!endif

#!ifdef WITH_NAT
#!ifdef WITH_RTPENGINE
# ----- rtpengine params -----
modparam("rtpengine", "rtpengine_sock", "udp:127.0.0.1:2223")
#!else
# ----- rtpproxy params -----
modparam("rtpproxy", "rtpproxy_sock", "udp:127.0.0.1:7722")
#!endif
# ----- nathelper params -----
modparam("nathelper", "natping_interval", 30)
modparam("nathelper", "ping_nated_only", 1)
modparam("nathelper", "sipping_bflag", FLB_NATSIPPING)
modparam("nathelper", "sipping_from", "sip:[email protected]")

# params needed for NAT traversal in other modules
modparam("nathelper|registrar", "received_avp", "$avp(RECEIVED)")
modparam("usrloc", "nat_bflag", FLB_NATB)
#!endif

#!ifdef WITH_TLS
# ----- tls params -----
modparam("tls", "config", "/usr/local/etc/kamailio/tls.cfg")
#!endif

#!ifdef WITH_ANTIFLOOD
# ----- pike params -----
modparam("pike", "sampling_time_unit", 2)
modparam("pike", "reqs_density_per_unit", 16)
modparam("pike", "remove_latency", 4)
#!endif

#!ifdef WITH_HTABLE
# ----- htable params -----
#!ifdef WITH_ANTIFLOOD
/* ip ban htable with autoexpire after 5 minutes */
modparam("htable", "htable", "ipban=>size=8;autoexpire=300;")
#!endif
#!endif

#!ifdef WITH_DEBUG
# ----- debugger params -----
modparam("debugger", "cfgtrace", 1)
modparam("debugger", "log_level_name", "exec")
#!endif

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


/* Main SIP request routing logic
 * - processing of any incoming SIP request starts with this route
 * - note: this is the same as route { ... } */
request_route {
    #...
}

后台启动服务

/usr/local/sbin/kamailio start
那么这个时候也有可能你配置文件里面写的有语法、引用等异常,你会看到ERROR: PID file /run/kamailio/kamailio.pid does not exist -- Kamailio start failed,还要去日志看错误信息,很麻烦怎么办呢?

校验下服务,能看到错误输出,如果没有错误,再用后台启动

服务校验

$:/run$ sudo /usr/local/sbin/kamailio -M 8 -E -e -dd
 0(274273) INFO: <core> [main.c:2292]: main(): private (per process) memory: 8388608 bytes
 0(274273) WARNING: <core> [core/socket_info.c:1811]: fix_hostname(): could not rev. resolve 172.19.53.235
 0(274273) WARNING: <core> [core/socket_info.c:1811]: fix_hostname(): could not rev. resolve 172.17.0.1
 0(274273) WARNING: <core> [core/socket_info.c:1811]: fix_hostname(): could not rev. resolve 172.18.0.1
 0(274273) WARNING: <core> [core/socket_info.c:1811]: fix_hostname(): could not rev. resolve 172.19.53.235
 0(274273) WARNING: <core> [core/socket_info.c:1811]: fix_hostname(): could not rev. resolve 172.17.0.1
 0(274273) WARNING: <core> [core/socket_info.c:1811]: fix_hostname(): could not rev. resolve 172.18.0.1
Listening on
             udp: 127.0.0.1:5060
             udp: 172.19.53.235:5060
             udp: 172.17.0.1:5060
             udp: 172.18.0.1:5060
             tcp: 127.0.0.1:5060
             tcp: 172.19.53.235:5060
             tcp: 172.17.0.1:5060
             tcp: 172.18.0.1:5060
Aliases:
             tcp: localhost:5060
             udp: localhost:5060

 0(274273) INFO: <core> [core/tcp_main.c:5218]: init_tcp(): using epoll_lt as the io watch method (auto detected)
 0(274279) INFO: rr [../outbound/api.h:53]: ob_load_api(): unable to import bind_ob - maybe module is not loaded
 0(274279) INFO: rr [rr_mod.c:185]: mod_init(): outbound module not available
 0(274279) INFO: <core> [main.c:3257]: main(): processes (at least): 49 - shm size: 67108864 - pkg size: 8388608
 0(274279) INFO: <core> [core/udp_server.c:167]: probe_max_receive_buffer(): SO_RCVBUF is initially 212992 for fd 9
 0(274279) INFO: <core> [core/udp_server.c:237]: probe_max_receive_buffer(): SO_RCVBUF is finally 425984 on fd 9
 0(274279) INFO: <core> [core/udp_server.c:268]: probe_max_send_buffer(): SO_SNDBUF is initially 212992 for fd 9
 0(274279) INFO: <core> [core/udp_server.c:338]: probe_max_send_buffer(): SO_SNDBUF is finally 425984 on fd 9
 0(274279) INFO: <core> [core/udp_server.c:167]: probe_max_receive_buffer(): SO_RCVBUF is initially 212992 for fd 10
 0(274279) INFO: <core> [core/udp_server.c:237]: probe_max_receive_buffer(): SO_RCVBUF is finally 425984 on fd 10
 0(274279) INFO: <core> [core/udp_server.c:268]: probe_max_send_buffer(): SO_SNDBUF is initially 212992 for fd 10
 0(274279) INFO: <core> [core/udp_server.c:338]: probe_max_send_buffer(): SO_SNDBUF is finally 425984 on fd 10
 0(274279) INFO: <core> [core/udp_server.c:167]: probe_max_receive_buffer(): SO_RCVBUF is initially 212992 for fd 11
 0(274279) INFO: <core> [core/udp_server.c:237]: probe_max_receive_buffer(): SO_RCVBUF is finally 425984 on fd 11
 0(274279) INFO: <core> [core/udp_server.c:268]: probe_max_send_buffer(): SO_SNDBUF is initially 212992 for fd 11
 0(274279) INFO: <core> [core/udp_server.c:338]: probe_max_send_buffer(): SO_SNDBUF is finally 425984 on fd 11
 0(274279) INFO: <core> [core/udp_server.c:167]: probe_max_receive_buffer(): SO_RCVBUF is initially 212992 for fd 12
 0(274279) INFO: <core> [core/udp_server.c:237]: probe_max_receive_buffer(): SO_RCVBUF is finally 425984 on fd 12
 0(274279) INFO: <core> [core/udp_server.c:268]: probe_max_send_buffer(): SO_SNDBUF is initially 212992 for fd 12
 0(274279) INFO: <core> [core/udp_server.c:338]: probe_max_send_buffer(): SO_SNDBUF is finally 425984 on fd 12
 0(274279) INFO: <core> [core/tcp_main.c:3274]: tcp_init(): Set TCP_USER_TIMEOUT=10000 ms
 0(274279) ERROR: <core> [core/tcp_main.c:3279]: tcp_init(): bind(e, 0x7f3eed110ec8, 16) on 127.0.0.1:5060 : Address already in use
 0(274279) INFO: <core> [core/sctp_core.c:53]: sctp_core_destroy(): SCTP API not initialized

直观地看到错误信息,上面这个错误就很明显了,服务端口已经起着了,或者和别的服务端口冲突了。一切正常的话没有ERROR的话,就是没有语法错误。
对于新手的我,改脚本直接上文件写的,有这个命令还是很好用的,能告诉我第几行有错误。

终止服务

$ ps axw | /usr/bin/egrep kamailio
  40551 ?        S      0:00 ./kamailio
  40552 ?        S      0:00 ./kamailio
  40553 ?        S      0:00 ./kamailio
  40554 ?        S      0:00 ./kamailio
  40555 ?        S      0:00 ./kamailio
  40556 ?        S      0:00 ./kamailio
  40557 ?        S      0:00 ./kamailio
  40558 ?        S      0:00 ./kamailio
  40559 ?        S      0:00 ./kamailio
  40560 ?        S      0:00 ./kamailio
  40561 ?        S      0:00 ./kamailio
  40562 ?        S      0:00 ./kamailio
  40563 ?        S      0:00 ./kamailio
  40564 ?        S      0:00 ./kamailio
  40565 ?        S      0:00 ./kamailio
  40566 ?        S      0:00 ./kamailio
  40567 ?        S      0:00 ./kamailio
  40568 ?        S      0:00 ./kamailio
  40569 ?        S      0:00 ./kamailio
  40570 ?        S      0:00 ./kamailio
  40571 ?        S      0:00 ./kamailio
  40572 ?        S      0:00 ./kamailio
  40573 ?        S      0:00 ./kamailio
  40574 ?        S      0:00 ./kamailio
  40575 ?        S      0:00 ./kamailio
  40576 ?        S      0:00 ./kamailio
  40577 ?        S      0:00 ./kamailio
  40578 ?        S      0:00 ./kamailio
  40579 ?        S      0:00 ./kamailio
  40580 ?        S      0:00 ./kamailio
  40581 ?        S      0:00 ./kamailio
  40582 ?        S      0:00 ./kamailio
  40583 ?        S      0:00 ./kamailio
  40584 ?        S      0:00 ./kamailio
  40585 ?        S      4:01 ./kamailio
  40586 ?        S      0:13 ./kamailio
  40587 ?        S      0:00 ./kamailio
  40588 ?        S      0:00 ./kamailio
  40589 ?        S      0:00 ./kamailio
  40590 ?        S      0:01 ./kamailio
  40591 ?        S      0:06 ./kamailio
  40592 ?        S      0:06 ./kamailio
  40593 ?        S      0:06 ./kamailio
  40594 ?        S      0:06 ./kamailio
  40595 ?        S      0:06 ./kamailio
  40596 ?        S      0:06 ./kamailio
  40597 ?        S      0:06 ./kamailio
  40598 ?        S      0:06 ./kamailio
  40599 ?        S      0:02 ./kamailio
 274124 pts/2    S+     0:00 grep -E kamailio

 $ sudo /usr/bin/killall kamailio
 $ ps axw | /usr/bin/egrep kamailio

通常kamailio启动会调起很多端口,stop有时候清不干净,我通常会使用这个命令

;