# Ubuntu16.04上安装FreeSWITCH1.6

## 操作系统和软件版本

ubuntu16.04

FreeSWITCH v1.6

python2.7

lua5.1

## 准备工作

### 安装系统

最小安装ubuntu16.04系统，安装ssh server

### 安装依赖

```sh
sudo apt-get install -y \
    python-dev swig ffmpeg \
    yasm unixodbc-dev \
    libshout3-dev libmpg123-dev \
    libmp3lame-dev libsndfile-dev \
    autoconf automake devscripts \
    libopus-dev libvorbis0a \
    libogg0 libogg-dev \
    libvorbis-dev gawk \
    g++ git-core libjpeg-dev \
    libncurses5-dev libtool-bin \
    pkg-config libtiff5-dev \
    libperl-dev libgdbm-dev \
    libdb-dev gettext libssl-dev \
    libcurl4-openssl-dev \
    libpcre3-dev libspeex-dev \
    libspeexdsp-dev libsqlite3-dev \
    libedit-dev libldns-dev \
    libpq-dev
```

如果需要开启mod_lua模块，还需要安装lua

```sh
sudo apt-get install -y lua5.1 liblua5.1-dev
```

- 安装一些python依赖

安装dbutils可能会失败，是因为python和pip的版本太老了。

这一步不是必须的，只有在你需要用python2为FreeSWITCH写脚本时才会用到。

```sh
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
python get-pip.py
pip install pydub
pip install python-ESL
pip install pika
pip install dbutils
```

### 下载源码

```sh
git clone https://github.com/signalwire/freeswitch.git
cd freeswitch
git checkout v1.6
git remote rm origin
```

> 最后一句是断开本地目录和远程代码库的关联

## 安装freeswitch

- 配置lua

```bash
cp /usr/include/lua5.1/*.h src/mod/languages/mod_lua/
```

```bash
sudo ln -s /usr/lib/x86_64-linux-gnu/liblua5.1.so /usr/lib/x86_64-linux-gnu/liblua.so
```

- 预处理

```bash
sudo ./bootstrap.sh -j
```

- 预配置模块

```bash
vim modules.conf
```

打开注释

```
formats/mod_shout
languages/mod_python
event_handlers/mod_cdr_pg_csv
asr_tts/mod_unimrcp
```

如果不需要使用lua语言模块，则将下面内容加注释

```
#languages/mod_lua
```

- 编译安装

```bash
./configure --with-python=/usr/bin/python2.7 --with-lua=/usr/bin/lua5.1 --enable-core-pgsql-support
sudo make
sudo make mod_cdr_pg_csv-install
sudo make mod_unimrcp-install
sudo make install
```

- 安装声音包

```bash
sudo make sounds-install
sudo make moh-install
sudo make cd-sounds-install
sudo make cd-moh-install
sudo make uhd-sounds-install
sudo make uhd-moh-install
```

- 全局命令

```bash
sudo ln -sf /usr/local/freeswitch/bin/freeswitch /usr/local/bin/
sudo ln -sf /usr/local/freeswitch/bin/fs_cli /usr/local/bin/
```

- 配置模块

```bash
vim /usr/local/freeswitch/conf/autoload_configs/modules.conf.xml
```

打开注释

```xml
    <load module="mod_python"/>
    <load module="mod_shout"/>
```

添加配置

```xml
    <load module="mod_cdr_pg_csv"/>
    <load module="mod_unimrcp"/>
```

- 配置acl白名单

```bash
vim /usr/local/freeswitch/conf/autoload_configs/acl.conf.xml
```

根据自己网络的实际情况进行配置（照抄大概率无效）

```xml
<list name="domains" default="deny">
<!-- domain= is special it scans the domain from the directory to build t$ -->
<node type="allow" domain="$${domain}"/>
<!-- use cidr= if you wish to allow ip ranges to this domains acl. -->
    <node type="allow" cidr="127.0.0.1/32"/>
</list>
```

- 配置ESL

```bash
vim /usr/local/freeswitch/conf/autoload_configs/event_socket.conf.xml
```

```xml
<configuration name="event_socket.conf" description="Socket Client">
        <settings>
            <param name="nat-map" value="false"/>
            <!--ip 统一为0.0.0.0-->
            <param name="listen-ip" value="0.0.0.0"/>
            <!-- 端口号 默认8021 -->
            <param name="listen-port" value="8021"/>
            <!-- 密码统一ClueCon -->
            <param name="password" value="ClueCon"/>
            <!-- 允许acl白名单内的IP 访问 -->
            <param name="apply-inbound-acl" value="domains"/>
            <!--<param name="apply-inbound-acl" value="loopback.auto"/>-->
            <!--<param name="stop-on-bind-error" value="true"/>-->
        </settings>
</configuration>
```

## 4. 启动freeswitch

```bash
freeswitch -nc
```

通过fs_cli可以进入freeswitch控制台（freeswitch服务启动需要等一段时间，而且可能会很长）

```bash
fs_cli --password=ClueCon
```
