在之前,我们转载了FreeSWITCH官网上有关postgresql的配置的问题,但是在细节上并没有描述,现在就如何在debian8.1上安装FreeSWITCH、Postgresql并使用Postgresql作为FreeSWITCH数据库做个简单介绍。

第一步、安装Postgresql

CentOS 或debian 源码安装 PostgreSQL 9.3.5

安装PostgreSQL前,确认Internet连接正常,以便下载安装文件。

先使用 yum -y update 指令升级系统到最新版本。

本安装将PostgreSQL的数据文件与执行文件分离,如果你打算设置到不同的路径,注意修改对应的执行命令和数据库初始化脚本。

# 修改防火墙设置,打开5432端口

vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT

# 重启防火墙使新设置生效

service iptables restart

# 新增用户组

groupadd postgres

# 新增用户

useradd postgres -g postgres

# 新建数据库执行文件目录

mkdir -p /usr/local/pgsql

# 新建数据库数据文件目录

mkdir -p /db/pgsql/data

# 修改目录拥有者

chown -R postgres /usr/local/pgsql/.
chown -R postgres /db/pgsql/data
chown -R postgres /db/pgsql/data/.

# 编辑PATH搜索路径

vi /etc/profile

Append these 2 lines to the end of the file:

PATH=/usr/local/pgsql/bin:$PATH
export PATH

# 生效PATH搜索路径

source /etc/profile

# 安装编译源码所需的工具和库

yum -y install wget gcc readline-devel zlib-devel make

debian

apt-get install zlibc libghc-zlib-dev libzlcore-dev gcc make git automake

# 进入源码压缩包下载目录

cd /usr/src

# 下载源码压缩包

wget http://ftp.postgresql.org/pub/source/v9.3.5/postgresql-9.3.5.tar.gz

# 解压缩源码包

tar zxvf ./postgresql-9.3.5.tar.gz

# 进入解压缩源码目录

cd ./postgresql-9.3.5

# 执行源码编译配置脚本

./configure

# 编译源码

make

# 安装

make install

# 变更登录用户

su - postgres

# 执行数据库初始化脚本

/usr/local/pgsql/bin/initdb --encoding=utf8 -D /db/pgsql/data

# 退出变更登录

exit

# 复制PostgreSQL执行脚本

cp /usr/src/postgresql-9.3.5/contrib/start-scripts/linux /etc/init.d/postgresql

# 增加执行权限

chmod +x /etc/init.d/postgresql

# 编辑PostgreSQL执行脚本,指定数据库文件目录

vi /etc/init.d/postgresql
PGDATA="/db/pgsql/data"

# 编辑配置文件,配置可访问数据库的网络地址 (注意别忘了去掉#listen_addresses=前面的#)

vi /db/pgsql/data/postgresql.conf
listen_addresses = '*'

# 启动PostgreSQL服务

service postgresql start

# 以postgres用户登录数据库,修改postgres用户的数据库密码

psql -U postgres
 postgres=# ALTER USER postgres PASSWORD '123456';
  postgres=# \q

退出pg登录

exit

# 编辑配置文件,设置密码md5验证

vi /db/pgsql/data/pg_hba.conf
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
#host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5

# 重启数据库服务

service postgresql restart

debian下需要安装 chkconfig工具

设置开机自动启动服务

chkconfig postgresql on

配置LD_LIBRARY_PATH vi /etc/profile

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/pgsql/lib
export LD_LIBRARY_PATH

C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/local/pgsql/include
export C_INCLUDE_PATH

source /etc/profile

二、 安装FreeSWITCH

  1. 配置debian源

vim /etc/apt/sources.list

变成以下内容:

\# deb cdrom:[Debian GNU/Linux 8.1.0 _Jessie_ - Official amd64 DVD Binary-1 20150606-14:19]/ jessie contrib main

\# deb cdrom:[Debian GNU/Linux 8.1.0 _Jessie_ - Official amd64 DVD Binary-1 20150606-14:19]/ jessie contrib main

\# deb http://security.debian.org/ jessie/updates main contrib
\# deb-src http://security.debian.org/ jessie/updates main contrib

deb http://mirrors.163.com/debian jessie main non-free contrib
deb http://mirrors.163.com/debian jessie-proposed-updates main contrib non-free
deb http://mirrors.163.com/debian-security jessie/updates main contrib non-free

deb http://security.debian.org jessie/updates main contrib non-free
\# 以上禁掉cdrom和deb原生源,改为163的,加快速度
\# jessie-updates, previously known as 'volatile'
\# A network mirror was not selected during install. The following entries
\# are provided as examples, but you should amend them as appropriate
\# for your mirror of choice.
\#
\# deb http://ftp.debian.org/debian/ jessie-updates main contrib
\# deb-src http://ftp.debian.org/debian/ jessie-updates main contrib
  1. 下载并编译 a. 添加freeswitch的源echo "deb http://files.freeswitch.org/repo/deb/debian/ jessie main" > /etc/apt/sources.list.d/99FreeSWITCH.test.listwget -O - http://files.freeswitch.org/repo/deb/debian/key.gpg |apt-key add - apt-get update

b. 安装依赖 DEBIAN_FRONTEND=none APT_LISTCHANGES_FRONTEND=none apt-get install -y --force-yes freeswitch-video-deps-most

c. git FreeSWITCH的源码

git config --global pull.rebase true
git clone https://freeswitch.org/stash/scm/fs/freeswitch.git freeswitch.git
cd freeswitch.git
./bootstrap.sh -j

d. 在modules.conf中开启pg_cdr vim modules.conf

去掉 mod_cdr_pg_csv 前的#

e. 编译并install

./configure --enable-core-pgsql-support 
    make
    make install
    make cd-sounds-install 
    make cd-moh-install 
    make samples

f. 配置FreeSWITCH使用Postgresql

<param name="odbc-dsn" value="pgsql://hostaddr=127.0.0.1 dbname=freeswitch user=postgres password='123456' options='-c client_min_messages=NOTICE' application_name='freeswitch'" />

这就是置于FreeSWITCH中的pg配置

如db.conf.xml:

<configuration name="db.conf" description="LIMIT DB Configuration">
  <settings>
    <!--<param name="odbc-dsn" value="dsn:user:pass"/>-->
    <param name="odbc-dsn" value="pgsql://hostaddr=127.0.0.1 dbname=freeswitch user=postgres password='123456' options='-c client_min_messages=NOTICE' application_name='freeswitch'" />
  </settings>
</configuration>

其余的配置文件如下:

db.conf.xml
fifo.conf.xml
voicemail.conf.xml
switch.conf.xml
internal.xml
external.xml

配置cdr,先建立库及表

su - postgres
psql -U postgres
\#postgres> create database cdr;
\#postgres> 
create table cdr (
    id                        serial primary key,
    local_ip_v4               inet not null,
    caller_id_name            varchar,
    caller_id_number          varchar,
    destination_number        varchar not null,
    context                   varchar not null,
    start_stamp               timestamp with time zone not null,
    answer_stamp              timestamp with time zone,
    end_stamp                 timestamp with time zone not null,
    duration                  int not null,
    billsec                   int not null,
    hangup_cause              varchar not null,
    uuid                      uuid not null,
    bleg_uuid                 uuid,
    accountcode               varchar,
    read_codec                varchar,
    write_codec               varchar,
    sip_hangup_disposition    varchar,
    ani                       varchar
);
\#postgres>\q
exit

修改pg_cdr.conf.xml文件中的db-info

<param name="db-info" value="host=localhost dbname=cdr user=postgres password=123456 connect_timeout=10" />

那么基本数据库就会使用postgresql了,从而避免sqlite 文件破坏动不动让系统无法工作的问题。