编辑
2025-07-19
学习笔记
00

目录

一、安装编译依赖
二、下载Kamailio源码包并安装
三、配置Kamailio
生成目录的说明
配置mysql数据存储模块
配置kamailio的主要配置文件
设置kamailio启停服务的脚本
可选配置kamailio

一、安装编译依赖

shell
# 基础编译工具 sudo apt install -y gcc g++ make autoconf flex bison pkg-config # 核心依赖库 sudo apt install -y libssl-dev libxml2-dev libpcre3-dev libcurl4-openssl-dev sudo apt install -y libmysqlclient-dev libsnmp-dev libjson-c-dev libmongoc-dev # 其他可选依赖(按需安装) sudo apt install -y libncurses5-dev libreadline-dev uuid-dev libperl-dev

或者是使用下面的命令都安装:

shell
sudo yum install -y gcc gcc-c++ flex bison make openssl openssl-devel libxml2 libxml2-devel zlib-devel ncurses* e2fsprogs-devel uuid-devel libuuid-devel readline6 readline-devel curl-devel mysql-devel lua-devel

二、下载Kamailio源码包并安装

shell
wget http://www.kamailio.org/pub/kamailio/5.7.0/src/kamailio-5.7.0_src.tar.gz tar -zxvf kamailio-5.7.0_src.tar.gz cd ./kamailio-5.7.0 make FLAVOUR=kamailio cfg ##需要添加模块 make FLAVOUR=kamailio include_modules="db_mysql app_lua" cfg cd ./src make all ##缺什么包装什么包就行 make install

三、配置Kamailio

生成目录的说明

  1. /usr/local/sbin 主要可执行文件说明:
  • kamailio - Kamailio SIP 服务器的执行文件
  • kamdbctl – 用于创建管理数据库的脚本
  • kamctl – 用于控制管理Kamailio SIP 服务器的脚本
  • kamcmd – CLI命令行工具,是Kamailio SIP服务器提供的接口
  1. /usr/local/etc/kamailio 配置文件目录
  • kamailio.cfg – kamailio主配置文件,包括路由,全局参数,模块加载等
  • kamctlrc – 定义了一些kamctl和kamdbctl可以使用的命令,如数据库引擎,域名等
  1. /usr/local/share/kamailio 数据库及其表文件 初始化数据库使用
  • mysql mysql表文件
  1. /usr/local/lib64/kamailio 模块目录
  • kamctl 客户端配置文件
  • modules 库文件

配置mysql数据存储模块

默认情况下,不会引入任何数据库存储,我们这边是因为需要做持久化(也是正常使用的需求),前面modules已经引入 db_mysql的模块。 打开配置目录,修改文件中的数据库相关链接信息:

shell
cd /usr/local/etc/kamailio/ vim kamctlrc

修改的内容如下:

yml
## 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=

开始通过脚本初始化数据库配置,参照下面的shell日志内容执行:

shell
$:/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是否有新建表成功。

配置kamailio的主要配置文件

shell
cd /usr/local/etc/kamailio/ #替换配置文件kamailio.cfg #将kamailio.lua复制到/etc/kamailio/kamailio.lua

校验配置文件是否正常,然后方便查看配置错误的信息,可以使用:

shell
$:/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的话,就是没有语法错误。 对于新手的我,改脚本直接上文件写的,有这个命令还是很好用的,能告诉我第几行有错误。

设置kamailio启停服务的脚本

1:查看5060端口是否被占用

shell
netstat -tulnp | grep :5060 tcpdump udp port 5060 -n -vvvv

2:查看是否已经启动了kamailio

shell
ps -aux|grep kamailio pkill kamailio

3: 配置启动脚本

shell
vim /usr/lib/systemd/system/kamailio.service

修改Environment变量CFGFILE和ExecStart的值,让它指向我们的安装目录:

text
[Service] Type=simple User=kamailio Group=kamailio Environment='CFGFILE=/usr/local/etc/kamailio/kamailio.cfg' Environment='SHM_MEMORY=64' Environment='PKG_MEMORY=4' EnvironmentFile=-/etc/sysconfig/kamailio ExecStart=/usr/local/sbin/kamailio -DD -P /var/run/kamailio/kamailio.pid -f $CFGFILE -m $SHM_MEMORY -M $PKG_MEMORY Restart=on-failure

修改后,执行刷新命令:

shell
systemctl daemon-reload

缺省设置条件下,运行Kamailio服务器的用户和组名字都是kamailio。因此,我们需要创建相应的Linux帐户:

shell
mkdir -p /var/run/kamailio groupadd kamailio adduser --system -g kamailio --shell /bin/false --home /var/run/kamailio kamailio #将所有权限设置为/var/run/kamailio chown kamailio:kamailio /var/run/kamailio

4: 启停服务

shell
#配置开机自启 systemctl enable kamailio #启动服务 systemctl start kamailio #停止服务 systemctl stop kamailio #使用kamctl工具重启服务 kamctl restart

可选配置kamailio

text
#在ko_dispatcher表中配置freeswitch信息 #在ko_address 表中配置IP授权信息

本文作者:xiech

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!