Thursday, December 20, 2018

Installing Asterisk on CentOS7 (step-by-step)

Shrink home volume size and extend root volume size (if needed): 
umount /home
df -h # verify that /home is unmounted
parted -l # to find type of the home LV filesystem (fxs in my case)
lvremove /dev/centos/home # remove LV from the VG
lvcreate -L 5G -n home centos
mkfs.xfs /dev/mapper/centos-home
mount -a
lvs
lsblk
df -h
lvextend -l +100%FREE /dev/centos/root
lvextend -l +100%FREE /dev/centos/root 
xfs_growfs /dev/centos/root
df -h
lsblk
lvs

Setup networking:
systemctl stop NetworkManager
systemctl disable NetworkManager
chkconfig network on
systemctl start network
vi /etc/sysconfig/network-scripts/ifcfg-eth0 # assign IPADDR / PREFIX / GATEWAY / DNS1 / DNS2
vi /etc/sysconfig/network and add below two lines:
NETWORKING=yes
NOZEROCONF=true
systemctl restart network

Disable SELinux:
vi /etc/sysconfig/selinuix and change:
SELINUX=enabled to SELINUX=disabled

Setup NTP:
yum install -y ntp && ntpdate pool.ntp.org && chkconfig ntpd && service ntpd start
systemctl enable ntpd.service
systemctl status ntpd.service
systemctl start ntpd.service
systemctl status ntpd.service

Preinstall steps:
adduser asteriskpbx && passwd asteriskpbx && yum install sudo && visudo # uncomment #%wheel  ALL=(ALL)       ALL line 
vi /etc/group # add asteriskpbx to wheel group: wheel:x:10:root,asteriskpbx
usermod -L asteriskpbx #make asteriskpbx user nologin

Before Asterisk installation, install all prerequisite packages:
yum -y install make gcc gcc-c++ make subversion libxml2-devel ncurses-devel openssl-devel vim-enhanced man glibc-devel autoconf libnewt kernel-devel kernel-headers linux-headers openssl-devel zlib-devel libsrtp libsrtp-devel uuid libuuid-devel mariadb-server jansson-devel libsqlite3x libsqlite3x-devel epel-release.noarch bash-completion bash-completion-extras unixODBC unixODBC-devel libtool-ltdl libtool-ltdl-devel mysql-connector-odbc mlocate libiodbc

Initial setup MariaDB:
systemctl status mariadb
systemctl enable mariadb
systemctl start mariadb
systemctl status mariadb
/usr/bin/mysql_secure_installation

Asterisk Installation:
yum update -y
yum install wget -y
mkdir -p ~/src/asterisk/
cd src/asterisk/
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-13-current.tar.gz
tar -xzvf asterisk-13-current.tar.gz 
cd asterisk-13.23.1/
If your system is 64 bit and you want PJSIP:
./configure --libdir=/usr/lib64 --with-pjproject-bundled
Otherwise:
./configure
menuselect/menuselect --list-options
make
make install
make samples
make config
safe_asterisk
systemctl status asterisk

After install steps:
chown -R asteriskpbx:asteriskpbx /usr/lib/asterisk/
chown -R asteriskpbx:asteriskpbx /usr/lib64/asterisk/
chown -R asteriskpbx:asteriskpbx /var/lib/asterisk
chown -R asteriskpbx:asteriskpbx /var/spool/asterisk/
chown -R asteriskpbx:asteriskpbx /var/log/asterisk/
chown -R asteriskpbx:asteriskpbx /var/run/asterisk/
chown -R asteriskpbx:asteriskpbx /usr/sbin/ast
chown -R asteriskpbx:asteriskpbx /usr/sbin/asterisk
chown -R asteriskpbx:asteriskpbx /var/log/asterisk
asterisk -r
exit

If want -  load modules needed for PJSIP

vi /etc/asterisk/modules.conf
[modules]
;AUTOLOAD
autoload=yes
;LOAD
preload => res_odbc.so
preload => res_config_odbc.so
load => res_pjsip.so
load => res_pjsip_pubsub.so
load => res_pjsip_session.so
load => chan_pjsip.so
load => res_pjsip_exten_state.so
load => res_pjsip_authenticator_digest.so
load => res_timing_timerfd.so

vi /etc/asterisk/asterisk.conf and uncomment and change this 2 lines:
runuser = asteriskpbx           ; The user to run as.
rungroup = asteriskpbx        ; The group to run as.

asterisk -rx "core restart now"

setup CEL with ODBC

odbcinst -j # verify that odbcinst.ini & odbc.ini are created
mysql -u root -p mysql
> CREATE USER 'asterisk'@'%' IDENTIFIED BY 'some_secret_password';
> CREATE DATABASE asterisk;
> GRANT ALL PRIVILEGES ON asterisk.* TO 'asterisk'@'%';

mysql -u asterisk -p asterisk # check access with asterisk user
odbcinst -q -d # verify that [MySQL] driver is seen
vi /etc/odbc.ini and add following:
[asterisk-connector]  # connector-name / DSN
Description =MySQL connection to 'asterisk' database
Driver =MySQL # driver name from the odbcinst.ini
Database =asterisk # database to connect
Server =localhost  # we'll connect to the server itself
charset =UTF8
UserName =asterisk # DB user-name for asterisk DB
Password =some_secret_password # asterisk DB-user password
Port =3306
Socket =/var/lib/mysql/mysql.sock

echo "select 1" | isql -v asterisk-connector asterisk 'some_secret_password' # check connection

Add connector to the asterisk res_odbc.conf:
[asterisk]
enabled => yes
dsn => asterisk-connector ; DSN is Data Source Name (from odbc.ini)
username => asterisk
password => some_secret_password
;pooling => no ; old option, replaced by max_connections
;limit => 1  ; old option, replaced by max_connections
pre-connect => yes
max_connections => 1

asterisk -rx "core restart now"
asterisk -rx "odbc show" # must show "Number of active connections: 1 (out of 1)"

vi cel.conf add following:
[general]
enable=yes
apps=all
events=all

vi cel_odbc.conf add following:
[mycel]
connection=asterisk
table=cel

vi /etc/my.cnf  add following under [mysqld]:
character_set_server=utf8

mysql -u asterisk -p asterisk
CREATE TABLE cel
(
id INT(20) NOT NULL AUTO_INCREMENT,
eventtype INT(11) COLLATE utf8_general_ci NOT NULL,
eventtime TIMESTAMP NOT NULL,
userdeftype VARCHAR(30) COLLATE utf8_general_ci NULL,
cid_name VARCHAR(80) COLLATE utf8_general_ci NULL,
cid_num VARCHAR(80) COLLATE utf8_general_ci NULL,
cid_ani VARCHAR(80) COLLATE utf8_general_ci NULL,
cid_rdnis VARCHAR(80) COLLATE utf8_general_ci NULL,
cid_dnid VARCHAR(80) COLLATE utf8_general_ci NULL,
exten VARCHAR(30) COLLATE utf8_general_ci NULL,
context VARCHAR(30) COLLATE utf8_general_ci NULL,
channame VARCHAR(30) COLLATE utf8_general_ci NULL,
appname VARCHAR(30) COLLATE utf8_general_ci NULL,
appdata VARCHAR(150) COLLATE utf8_general_ci NULL,
accountcode VARCHAR(30) COLLATE utf8_general_ci NULL,
peeraccount VARCHAR(30) COLLATE utf8_general_ci NULL,
uniqueid VARCHAR(30) COLLATE utf8_general_ci NULL,
linkedid VARCHAR(30) COLLATE utf8_general_ci NULL,
amaflags INT(11) NULL,
userfield VARCHAR(30) COLLATE utf8_general_ci NULL,
peer VARCHAR(30) COLLATE utf8_general_ci NULL,
PRIMARY KEY (id),
KEY uniqueid (uniqueid)
)
ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

asterisk -r asterisk
*CLI> module reload res_odbc.so asterisk
*CLI> module reload cel_odbc.so asterisk
*CLI> cel show status