FreeSwitch编译部署
1.安装操作系统依赖组件
sudo apt-get updateapt-get -y install build-essential subversion automake autoconf wget libtool libncurses5-dev
各软件包的作用:
- build-essential:包含 gcc、g++、make 等基本编译工具
- subversion:SVN 版本控制系统客户端
- automake:自动生成 Makefile 的工具
- autoconf:自动配置源代码的工具
- wget:命令行下载工具
- libtool:通用库支持脚本
- libncurses5-dev:终端字符界面开发库
注意:
- 如果是 Ubuntu 22.04+,可能需要安装
libncurses-dev
代替libncurses5-dev
2.安装编译依赖组件
将所有需要的组价都放在/opt/FreeSWITCH目录下,包括后面的FreeSWITCH源码
先安装weget,如果已经安装了则这步骤可以可以省略。
sudo apt-get install wget
2.1.安装编译cmake
sudo apt-get remove cmake
cd /opt/FreeSWITCH/
wget https://cmake.org/files/v3.14/cmake-3.14.0.tar.gz
tar -zxvf cmake-3.14.0.tar.gz
cd cmake-3.14.0
./configure
make -j$(nproc)
sudo make install
2.2.安装 libks
# 更新包索引并安装依赖
sudo apt-get update
sudo apt-get install -y cmake libssl-dev uuid-dev libcurl4-openssl-dev libjansson-dev libwebsockets-dev git# 克隆并编译libks
cd /opt/FreeSWITCH/
git clone https://github.com/signalwire/libks.git
cd libks# 使用独立构建目录
mkdir build && cd build
cmake ..
make -j$(nproc)
sudo make install# 更新动态链接库缓存
sudo ldconfig
2.3.安装 signalwire-c
cd /opt/FreeSWITCH/
git clone https://github.com/signalwire/signalwire-c.git
cd signalwire-c# 创建构建目录并编译
mkdir build && cd build
cmake .
make -j$(nproc)
sudo make install# 创建符号链接
sudo ln -sf /usr/local/lib64/pkgconfig/signalwire_client.pc /usr/lib64/pkgconfig/signalwire_client.pc
2.4.安装 x264
# 安装依赖
sudo apt-get update
sudo apt-get install -y nasm yasm pkg-configcd /opt/FreeSWITCH/
git clone http://git.videolan.org/git/x264.git
cd x264# 配置并编译(禁用汇编优化)
./configure \--prefix=/usr/local \ # 安装路径--enable-shared \ # 构建共享库--disable-asm \ # 禁用汇编优化(适用于非x86架构)--enable-pic # 生成位置无关代码make -j$(nproc)
sudo make install# 更新动态链接库缓存
sudo ldconfig
常见问题:
- 如果遇到 "undefined reference to
SSL_library_init'"错误,需要安装
libssl-dev` - 如果遇到 "libtool: not found" 错误,需要安装
libtool-bin
2.5.安装 libav
sudo apt-get install -y \libfreetype6-dev \ # FreeType字体库开发文件libfontconfig1-dev \ # 字体配置和管理库libxcb-shm0-dev \ # X Window系统共享内存libxcb-xfixes0-dev \ # X Window系统修复扩展libvpx-dev \ # WebM视频编解码器libmp3lame-dev \ # MP3音频编解码器libtheora-dev \ # Theora视频编解码器libvorbis-dev \ # Vorbis音频编解码器libxvidcore-dev \ # Xvid MPEG-4视频编解码器libva-dev \ # 视频加速APIlibvdpau-dev \ # VDPAU视频解码加速libdrm-dev \ # 直接渲染管理器# 克隆并编译libav
cd /opt/FreeSWITCH/
git clone https://gitee.com/nwaycn/libav.git
cd libav# 配置并编译
./configure \--prefix=/usr/local \ # 安装路径--enable-pic \ # 生成位置无关代码--enable-shared \ # 构建共享库--enable-libx264 \ # 启用x264支持--enable-gpl \ # 启用GPL许可证库--extra-libs="-ldl" \ # 链接动态加载库--enable-nonfree \ # 启用非免费编解码器(如libfdk_aac)--enable-libfreetype \ # 启用字体渲染支持--enable-libmp3lame \ # 启用MP3编码--enable-libvpx \ # 启用VP8/VP9编码--enable-libvorbis \ # 启用Vorbis编码--enable-libtheora \ # 启用Theora编码--enable-libxvid # 启用Xvid编码make -j$(nproc)
sudo make install
sudo ldconfig # 更新动态链接库缓存# 复制pkg-config文件(根据系统架构可能需要调整)
sudo mkdir -p /usr/lib64/pkgconfig/
sudo cp /usr/local/lib/pkgconfig/libav*.pc /usr/lib64/pkgconfig/
sudo cp /usr/local/lib/pkgconfig/libswscale.pc /usr/lib64/pkgconfig/
2.6.安装 libpng
cd /opt/FreeSWITCH/
git clone https://github.com/glennrp/libpng.git
cd libpng# 编译
./configure --enable-shared --prefix=/usr/local
make -j$(nproc) && sudo make install# 更新动态链接库缓存
sudo ldconfig# 复制pkg-config文件
sudo cp /usr/local/lib/pkgconfig/libpng* /usr/lib/x86_64-linux-gnu/pkgconfig/ 2>/dev/null || :
2.7.安装opus
cd /opt/FreeSWITCH/
# 克隆Opus源码库
git clone https://gitlab.xiph.org/xiph/opus.git
cd opus# 生成配置脚本(使用安全模式)
./autogen.sh --noconfigure# 配置编译参数
./configure \--prefix=/home/opp/pjsiprel \--libdir=/home/opp/pjsiprel/lib \--enable-shared \--enable-static \--disable-doc \--disable-extra-programs# 编译并安装
make -j$(nproc) && sudo make install
2.8.安装 sofia-sip
cd /opt/FreeSWITCH/git clone https://github.com/freeswitch/sofia-sip.git
cd sofia-sip# 生成配置文件
./bootstrap.sh # 配置编译参数
./configure \--prefix=/usr/local \--enable-shared \--disable-static \--with-pic \--enable-debug=no \--enable-assert=no \CFLAGS="-O2 -march=native" \CXXFLAGS="-O2 -march=native"# 编译并安装
make -j$(nproc) && sudo make install# 更新动态链接库缓存
sudo ldconfig# 验证安装
sofia-sip-ua --version
2.9.安装 spandsp
cd /opt/FreeSWITCH/# 克隆源码库
git clone https://github.com/freeswitch/spandsp.git
cd spandsp# 生成配置文件
./bootstrap.sh# 配置编译参数
./configure \--prefix=/usr/local \--enable-shared \--disable-static \--with-pic \CFLAGS="-O2 -march=native" \CXXFLAGS="-O2 -march=native"# 编译并安装
make -j$(nproc) && sudo make install# 更新动态链接库缓存
sudo ldconfig# 验证安装
pkg-config --modversion spandsp
3.编译安装 Freeswitch
3.1.编译安装命令
cd /opt/FreeSWITCH/# 克隆FreeSWITCH源码
git clone https://github.com/signalwire/freeswitch.git -b v1.10 freeswitch-1.10
cd freeswitch-1.10# 设置环境变量(确保pkg-config能找到之前手动安装的库)
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH"# 安装编译依赖
sudo apt update
sudo apt install -y \build-essential autoconf automake libtool git-core zlib1g-dev \libjpeg-dev libtiff-dev libncurses5-dev libssl-dev libcurl4-openssl-dev \libpcre3-dev libspeex-dev libspeexdsp-dev libldns-dev libedit-dev \libopus-dev libsqlite3-dev libsndfile1-dev libavformat-dev libavcodec-dev \libavutil-dev libswscale-dev liblua5.3-dev libgdbm-dev libdb-dev \yasm libvpx-dev libsrtp2-dev libopusfile-dev# 配置编译选项
./bootstrap.sh -j # 更新配置文件# 配置FreeSWITCH(启用常用模块)
./configure \--prefix=/usr/local/freeswitch \--enable-core-pgsql-support \--enable-zrtp \--enable-opus \--enable-lua \--enable-srtp \CFLAGS="-O2 -march=native" \CXXFLAGS="-O2 -march=native"# 检查配置结果(确保所有依赖都找到)
make menuselect# 编译(使用并行编译)
make -j$(nproc)# 安装
sudo make install# 安装声音文件和XML示例配置
sudo make cd-sounds-install
sudo make cd-moh-install
sudo make samples# 创建FreeSWITCH用户和组
sudo groupadd -r freeswitch
sudo useradd -r -g freeswitch -d /usr/local/freeswitch -s /sbin/nologin freeswitch
sudo chown -R freeswitch:freeswitch /usr/local/freeswitch# 创建系统服务
sudo tee /etc/systemd/system/freeswitch.service > /dev/null <<'EOF'
[Unit]
Description=FreeSWITCH VoIP Server
After=network.target syslog.target[Service]
Type=forking
PIDFile=/usr/local/freeswitch/run/freeswitch.pid
ExecStart=/usr/local/freeswitch/bin/freeswitch -ncwait -u freeswitch -g freeswitch -nonat
ExecStop=/usr/local/freeswitch/bin/freeswitch -stop
Restart=always
RestartSec=5
LimitCORE=infinity
LimitNOFILE=1048576
LimitNPROC=64000
LimitSTACK=65536000
TimeoutStartSec=120
TimeoutStopSec=60[Install]
WantedBy=multi-user.target
EOF# 重载systemd并启动FreeSWITCH
sudo systemctl daemon-reload
sudo systemctl enable freeswitch
sudo systemctl start freeswitch# 验证安装
sudo systemctl status freeswitch
/usr/local/freeswitch/bin/fs_cli -x 'status'
3.2.常见问题解决
如果遇到模块加载失败需要检查
/usr/local/freeswitch/conf/autoload_configs/modules.conf.xml
中对应模块是否启用。如果声音质量不佳,检查
/usr/local/freeswitch/conf/vars.xml
中的编解码优先级配置。如果网络连接有问题,检查防火墙设置:(如果是云服务器,请查看安全组规则)
sudo ufw allow 5060/tcp # SIP信令
sudo ufw allow 5060/udp # SIP信令
sudo ufw allow 16384:32768/udp # RTP媒体
- 如需调试,可临时禁用 systemd 服务,以非守护进程模式启动:
sudo systemctl stop freeswitch
/usr/local/freeswitch/bin/freeswitch -nonat -u freeswitch -g freeswitch
3.3.mod_directory未被编译
1.mod_directory
是默认模块,通常会自动编译,但需确认配置中未被禁用:
# 检查配置结果中是否包含 mod_directory(正常会显示)
grep "mod_directory" modules.conf
- 若显示
applications/mod_directory
,说明已启用; - 若未显示,编辑
modules.conf
,确保applications/mod_directory
未被注释。 - 取消注释重新编译
# 编译(-j 后面的数字为CPU核心数,加速编译)
make -j 4# 安装(仅安装缺失的模块,或重新安装全部)
make install
编译完成后,mod_directory.so
会被生成在 /usr/local/freeswitch/mod/
目录下。
2.验证模块文件是否存在
ls -l /usr/local/freeswitch/mod/mod_directory.so
- 若显示文件信息(如
-rwxr-xr-x
),说明安装成功; - 若仍缺失,检查编译过程中的错误日志(通常在
make
输出中),修复依赖问题后重新编译。
3.加载模块并验证
运行FreeSwitch后
# 进入 FreeSWITCH 控制台
fs_cli# 加载 mod_directory 模块
load mod_directory# 验证模块是否加载成功
module_exists mod_directory # 应返回 true