Hướng dẫn cài đặt FusionPBX(v4.4) Freeswitch(v1.8) Debian(v9) PostgreSQL Nginx

Hướng dẫn cài đặt FusionPBX(v4.4) Freeswitch(v1.8) Debian(v9) PostgreSQL Nginx

Fusionpbx là một giao diện quản lí đầy đủ tính năng cho Freeswitch. Hướng dẫn này bao gồm việc cài đặt FusionpbxFreeswitch® với PostgreSQLApache trên Debian v9.

1.Chuẩn bị

Debian v9 (Stretch) x64 minimal install
Freeswitch v1.8
FusionPBX v4
Nginx v1.10
PHP v7.1
PostgreSQL v11

2.Cài đặt

Chỉnh bảng mã về UTF-8

# Select en_US.UTF-8 UTF-8
# dpkg-reconfigure locales

Kiểm tra lại

# locale

Cài đặt packages cần thiết.

# apt update && apt upgrade -y && apt -y remove apache2
# apt -y install git nano dbus sudo nginx curl lsb-release sqlite3 haveged ghostscript libtiff5-dev libtiff-tools at tftpd ssl-cert fail2ban

PHP

Xóa phiên bản cũ PHP nếu đã cài

# apt remove -y php5* php7.0*

Cài đặt các gói phụ thuộc

# apt -y install apt-transport-https ca-certificates

Thêm php7.1 repository

# wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > \
/etc/apt/sources.list.d/php.list
# apt update && apt -y install php7.1 php7.1-cli php7.1-common php7.1-curl php7.1-mcrypt php7.1-pgsql php7.1-sqlite3 php7.1-odbc php7.1-xml php7.1-imap php7.1-fpm

sngrep

# wget http://packages.irontec.com/public.key -q -O - | apt-key add -
echo "deb http://packages.irontec.com/debian $(lsb_release -sc) main" > \
/etc/apt/sources.list.d/irontec.list

# apt update && apt install sngrep

Tắt Selinux

# sed -i 's/\(^SELINUX=\).*/\SELINUX=disabled/' /etc/selinux/config

Tắt Firewall

# systemctl disable firewalld
# systemctl disable iptables
# systemctl stop firewalld
# systemctl stop iptables

Chỉnh lại timezone cho server

# tzselect 
# timedatectl set-timezone Asia/Ho_Chi_Minh

Kiểm tra lại TIMEZONE

# timedatectl status
# systemctl restart rsyslog

Freeswitch

# wget -O - https://files.freeswitch.org/repo/deb/freeswitch-1.8/fsstretch-archive-keyring.asc | apt-key add -
# echo "deb http://files.freeswitch.org/repo/deb/freeswitch-1.8/ $(lsb_release -sc) main" > \
/etc/apt/sources.list.d/freeswitch.list
# apt update && apt -y install freeswitch-all freeswitch-all-dbg freeswitch-sounds* freeswitch-music* gdb

Database
Kiểm tra lại kết nối với database.

# nano +92 /etc/postgresql/11/main/pg_hba.conf
host all all 127.0.0.1/32 trust
# systemctl restart postgresql

Kiểm tra PostgreSQL locale

# sudo -u postgres psql -l

Nêu không phải UTF8 phải cập nhập lại.

# sudo -u postgres psql
# update pg_database set encoding = 6, datcollate = 'en_US.UTF8', datctype = 'en_US.UTF8' where datname = 'template0';
# update pg_database set encoding = 6, datcollate = 'en_US.UTF8', datctype = 'en_US.UTF8' where datname = 'template1';

Tạo User và database

# cd /tmp

Set the DB password variable.

# password=somepassword
# sudo -u postgres psql -c "SET client_encoding = 'UTF8';"
# sudo -u postgres psql -c "CREATE DATABASE fusionpbx;" 
# sudo -u postgres psql -c "CREATE DATABASE freeswitch;"
# sudo -u postgres psql -c "CREATE ROLE fusionpbx WITH SUPERUSER LOGIN PASSWORD '$password';"
# sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE fusionpbx to fusionpbx;"
# sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to fusionpbx;"

Fusionpbx

# mkdir -p /etc/fusionpbx
# mkdir -p /var/www/fusionpbx
# mkdir -p /var/cache/fusionpbx
# git clone -b 4.4 https://github.com/powerpbx/fusionpbx.git /var/www/fusionpbx

Copy thư mục và thay thế  freeswitch  với fusionpbx  như sau:

# mv /etc/freeswitch /etc/freeswitch.orig
# mkdir /etc/freeswitch
# cp -R /var/www/fusionpbx/resources/templates/conf/* /etc/freeswitch

Copy chỉnh lại đúng đường dẫn

# mkdir -p /usr/share/freeswitch/sounds/music/default
# mv /usr/share/freeswitch/sounds/music/*000/ /usr/share/freeswitch/sounds/music/default/

Php-fpm config

# sed 's#post_max_size = .*#post_max_size = 80M#g' -i /etc/php/7.1/fpm/php.ini
# sed 's#upload_max_filesize = .*#upload_max_filesize = 80M#g' -i /etc/php/7.1/fpm/php.ini
# sed 's#max_execution_time = .*#max_execution_time = 120#g' -i /etc/php/7.1/fpm/php.ini
# systemctl restart php7.1-fpm

Nginx config

# mkdir -p /etc/nginx/ssl
# cd /etc/nginx/sites-available
# wget https://raw.githubusercontent.com/powerpbx/fusionpbx-install.sh/master/debian/resources/nginx/fusionpbx
# sed -i /etc/nginx/sites-available/fusionpbx -e 's#unix:.*;#unix:/var/run/php/php7.1-fpm.sock;#g'
# ln -s /etc/nginx/sites-available/fusionpbx /etc/nginx/sites-enabled/fusionpbx
# rm /etc/nginx/sites-enabled/default

Link to self signed certificate

# ln -s /etc/ssl/private/ssl-cert-snakeoil.key /etc/ssl/private/nginx.key
# ln -s /etc/ssl/certs/ssl-cert-snakeoil.pem /etc/ssl/certs/nginx.crt

Dùng Let’s Encrypt

# mkdir -p /var/www/letsencrypt/
# systemctl restart nginx

Tạo file chạy cùng hệ thống khi khởi động.

# systemctl stop freeswitch
# rm -r /run/freeswitch
#nano /etc/systemd/system/freeswitch.service
[Unit]
Description=freeswitch
After=syslog.target network.target local-fs.target postgresql.service

[Service]
Type=forking
RuntimeDirectory=freeswitch
PIDFile=/run/freeswitch/freeswitch.pid
Environment="DAEMON_OPTS=-ncwait -nonat"
EnvironmentFile=-/etc/default/freeswitch
ExecStart=/usr/bin/freeswitch $DAEMON_OPTS
TimeoutSec=45s
Restart=always

User=www-data
Group=www-data

LimitCORE=infinity
LimitNOFILE=100000
LimitNPROC=60000
LimitSTACK=250000
LimitRTPRIO=infinity
LimitRTTIME=infinity
IOSchedulingClass=realtime
IOSchedulingPriority=2
CPUSchedulingPriority=89
UMask=0007

; Comment this out if using OpenVZ
CPUSchedulingPolicy=rr

[Install]
WantedBy=multi-user.target

Tạo file môi trường.

# cat >> /etc/default/freeswitch << EOF
# Uncommented variables will override variables in unit file
# User=""
# Group=""
# DAEMON_OPTS=""
EOF

Cấp quyền sở hữu và phân quyền try cập

Quyền sở hữu

# chown -R www-data. /etc/freeswitch /etc/fusionpbx /var/cache/fusionpbx /var/lib/freeswitch \
/var/log/freeswitch /usr/share/freeswitch /var/www/fusionpbx /var/run/freeswitch

Quyền try cập

# find /etc/freeswitch -type d -exec chmod 755 {} \;
# find /etc/fusionpbx -type d -exec chmod 755 {} \;
# find /var/cache/fusionpbx -type d -exec chmod 755 {} \;
# find /var/lib/freeswitch -type d -exec chmod 755 {} \;
# find /var/log/freeswitch -type d -exec chmod 755 {} \;
# find /usr/share/freeswitch -type d -exec chmod 755 {} \;
# find /var/www -type d -exec chmod 755 {} \;

# find /etc/freeswitch -type f -exec chmod 664 {} \;
# find /etc/fusionpbx -type f -exec chmod 664 {} \;
# find /var/lib/freeswitch -type f -exec chmod 664 {} \;
# find /var/log/freeswitch -type f -exec chmod 664 {} \;
# find /usr/share/freeswitch -type f -exec chmod 664 {} \;
# find /var/www -type f -exec chmod 664 {} \;

# chmod -R 755 /var/www/fusionpbx/secure

# chmod 755 /etc/cron.daily/fusionpbx-backup
# chmod 755 /etc/cron.daily/fusionpbx-maintenance

Cho phép khởi động cùng hệ thống

# systemctl daemon-reload
# systemctl enable freeswitch
# systemctl restart freeswitch

Vào trình duyệt và try cập IP

http://xx.xx.xx.xx

Username: superadmin
Password: someSuperadminPassword

Database Name: fusionpbx
Database Username: fusionpbx
Database Password: somepassword(The database user password created earlier)
Create Database Options: uncheck
Create Database Username:
Create Database Password :

Bấm chọn như trên và đợi 1 đến 2 phút là xong

Đăng nhập vào chọn Advanced > Default Settings > Adminer(auto_login) > Enabled = True
Tải lại trang Advanced > Adminer và xem database.

Cho phép freeswitch kết nối database.
Freeswitch kết nối mặc định sqlite. Thay đổi kết nối đến PostgreSQL. Làm như sau:

# nano +147 /etc/freeswitch/autoload_configs/switch.conf.xml

<param name="core-db-dsn" value="pgsql://hostaddr=127.0.0.1 dbname=freeswitch user=fusionpbx password='somepassword'" />

# systemctl restart freeswitch

Cài đặt firewall

# apt -y install firewalld
# systemctl enable firewalld
# systemctl start firewalld

# firewall-cmd --permanent --zone=public --add-service={http,https}
# firewall-cmd --permanent --zone=public --add-port={5060,5061,5080,5081}/tcp
# firewall-cmd --permanent --zone=public --add-port={5060,5061,5080,5081}/udp
# firewall-cmd --permanent --zone=public --add-port=16384-32768/udp
# firewall-cmd --reload
# firewall-cmd --list-all

Tạo cron backup dữ liệu hàng ngày dùng script bên dưới.

# nano /etc/cron.daily/fusionpbx-backup
#!/bin/sh

#Định nghĩa các biến đầu vào.
db_host=127.0.0.1
db_port=5432
now=$(date +%Y-%m-%d)
mkdir -p /var/backups/fusionpbx/postgresql

# Xóa postgres backups cũ nếu than 7 ngày.
find /var/backups/fusionpbx/postgresql/fusionpbx_pgsql* -mtime +7 -exec rm {} \;

# Xóa backup chính nếu hơn 7 ngày.
find /var/backups/fusionpbx/*.tgz -mtime +7 -exec rm {} \;

# Tạo database backup
pg_dump --verbose -Fc --host=${db_host} --port=${db_port} -U fusionpbx fusionpbx --schema=public -f /var/backups/fusionpbx/postgresql/fusionpbx_pgsql_${now}.sql

# Kết hợp nén và tạo file backup chính.
tar -zvcf /var/backups/fusionpbx/backup_${now}.tgz /var/backups/fusionpbx/postgresql/fusionpbx_pgsql_${now}.sql \
/var/www/fusionpbx /usr/share/freeswitch/scripts /var/lib/freeswitch/storage \
/var/lib/freeswitch/recordings /etc/fusionpbx /etc/freeswitch
# chmod 755 /etc/cron.daily/fusionpbx-backup

Để tránh hệ thống bị quá tải dùng script để xóa file ghi âm, voicemails, fax, …

# nano /etc/cron.daily/fusionpbx-maintenance
#!/bin/sh

# Xóa freeswitch logs hơn 7 ngày.
find /var/log/freeswitch/freeswitch.log.* -mtime +7 -exec rm {} \;

## Nếu muốn xóa file ghi âm hơn 90 ngày thì bỏ #.
#find /var/lib/freeswitch/recordings/*/archive/*  -name '*.wav' -mtime +90 -exec rm {} \;
#find /var/lib/freeswitch/recordings/*/archive/*  -name '*.mp3' -mtime +90 -exec rm {} \;

## Nếu muốn xóa fax hơn 90 ngày thì bỏ #. 
#find /var/lib/freeswitch/storage/fax/*  -name '*.tif' -mtime +90 -exec rm {} \; 
#find /var/lib/freeswitch/storage/fax/*  -name '*.pdf' -mtime +90 -exec rm {} \; 

## Nếu muốn xóa voicemails hơn 90 ngày thì bỏ #
#find /var/lib/freeswitch/storage/voicemail/default/*  -name 'msg_*.wav' -mtime +90 -exec rm {} \;
#find /var/lib/freeswitch/storage/voicemail/default/*  -name 'msg_*.mp3' -mtime +90 -exec rm {} \;
#psql --host=127.0.0.1 --username=fusionpbx -c "delete from v_voicemail_messages WHERE to_timestamp(created_epoch) < NOW() - INTERVAL '90 days'"

## Nếu muốn xóa dữ liệu báo cáo hơn 90 ngày thì bỏ #.
#psql --host=127.0.0.1 --username=fusionpbx -c "delete from v_xml_cdr WHERE start_stamp < NOW() - INTERVAL '90 days'"
# chmod 755 /etc/cron.daily/fusionpbx-maintenance

Kết luận: Qua bài viết này tôi đã hướng dẫn các bạn xây dựng được một tổng đài IP dựa trên mã nguồn mở đó là freeswitch và kết hợp giao diện fusionpbx database kết nối postgresql.
Chúc các bạn thành công!

Tham khảo: https://docs.fusionpbx.com/en/latest/getting_started/quick_install.html

Bài viết liên quan

Leave a Reply

Your email address will not be published. Required fields are marked *