install_fusionpbx.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #!/bin/bash
  2. ###############################################
  3. #
  4. # Installation Script to Install FreeSWITCH, FusionPBX, PostgreSQL, PHP, Apache and required
  5. # Supporting software on Centos 6.
  6. # Copyright (C) 2011, Ken Rice <[email protected]>
  7. #
  8. # Version: MPL 1.1
  9. #
  10. # The contents of this file are subject to the Mozilla Public License Version
  11. # 1.1 (the "License"); you may not use this file except in compliance with
  12. # the License. You may obtain a copy of the License at
  13. # http://www.mozilla.org/MPL/
  14. #
  15. # Software distributed under the License is distributed on an "AS IS" basis,
  16. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  17. # for the specific language governing rights and limitations under the
  18. # License.
  19. #
  20. # The Initial Developer of the Original Code is
  21. # Ken Rice <[email protected]>
  22. # Portions created by the Initial Developer are Copyright (C)
  23. # the Initial Developer. All Rights Reserved.
  24. #
  25. # Contributor(s):
  26. #
  27. # Ken Rice <[email protected]>
  28. #
  29. # Also thanks to:
  30. # The FreeSWITCH, FusionPBX and PostgreSQL Crews without them, none of this would be possible
  31. #
  32. ###############################################
  33. VERSION="0.3"
  34. #get the machine type x86_64
  35. MACHINE_TYPE=`uname -m`
  36. cat <<EOT
  37. This Script will install and create base line configs for FreeSWITCH, FusionPBX, Fail2Ban, Monit and PostgreSQL.
  38. It is designed to run on a Centos6 Minimal Install. EPEL will also be temporarily Enabled to get a few packages
  39. not in the main Centos Repositories.
  40. As with anything you will want to review the configs after the installer to make sure they are what you want.
  41. This is Version $VERSION of this script.
  42. EOT
  43. read -r -p "Are you sure? [Y/n] " response
  44. if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
  45. then
  46. echo "Here we go..."
  47. else
  48. echo "Aborting"
  49. exit
  50. fi
  51. # Do a Yum Update to update the system and then install all other required modules
  52. yum update -y
  53. yum -y install autoconf automake gcc-c++ git-core libjpeg-devel libtool make ncurses-devel pkgconfig unixODBC-devel openssl-devel gnutls-devel libogg-devel libvorbis-devel curl-devel libtiff-devel libjpeg-devel python-devel expat-devel zlib zlib-devel bzip2 which postgresql-devel postgresql-odbc postgresql-server subversion screen vim php* ntp sudo ghostscript libtiff
  54. #lets get the Time Right
  55. ntpdate pool.ntp.org
  56. service ntpd start
  57. chkconfig ntpd on
  58. #Disable SELinux (Ken hates this thing)
  59. if [ -x /usr/sbin/setenforce ]; then
  60. setenforce 0
  61. /bin/sed -i -e s,'SELINUX=enforcing','SELINUX=disabled', /etc/sysconfig/selinux
  62. fi
  63. # Lets go Get the FreeSWITCH Source and install it
  64. cd /usr/src
  65. git clone https://stash.freeswitch.org/scm/fs/freeswitch.git
  66. cd freeswitch
  67. ./bootstrap.sh -j
  68. ./configure --without-libcurl -C
  69. make -j `cat /proc/cpuinfo |grep processor |wc -l`
  70. make install
  71. make cd-moh-install && make cd-sounds-install
  72. #add a user for freeswitch
  73. useradd freeswitch
  74. #set ownership, perms, and install init scripts
  75. cd /usr/local/
  76. chown -R freeswitch:freeswitch freeswitch
  77. chmod -R g+w freeswitch
  78. cd /usr/src/freeswitch/build
  79. cp freeswitch.init.redhat /etc/init.d/freeswitch
  80. chmod +x /etc/init.d/freeswitch
  81. cp freeswitch.sysconfig /etc/sysconfig/freeswitch
  82. #Add Settings to freeswitch sysconfig file
  83. cat >> /etc/sysconfig/freeswitch <<EOT
  84. PID_FILE=/var/run/freeswitch/freeswitch.pid
  85. FS_USER=freeswitch
  86. FS_FILE=/usr/local/freeswitch/bin/freeswitch
  87. FS_HOME=/usr/local/freeswitch
  88. EOT
  89. # sym link fs_cli into /usr/local/bin so we don't have to adjust paths
  90. cd /usr/local/bin/
  91. ln -s /usr/local/freeswitch/bin/fs_cli fs_cli
  92. #start installing FusionPBX From Subversion
  93. cd /var/www
  94. svn co http://fusionpbx.googlecode.com/svn/trunk/fusionpbx html
  95. #fix FusionPBX Ownership and Perms
  96. chown -R apache:apache html
  97. cd /usr/local/freeswitch/conf/
  98. chmod 770 `find . -type d`
  99. chmod 660 `find . -type f`
  100. # add apache to the freeswitch Group
  101. usermod -a -G freeswitch apache
  102. ## Install EPEL so we can get monit and ngrep
  103. if [ ${MACHINE_TYPE} == 'x86_64' ]; then
  104. rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
  105. else
  106. rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm
  107. fi
  108. #Install Monit, Fail2Ban, and ngrep
  109. yum install -y monit ngrep fail2ban
  110. #Drop monit configs in the right spot
  111. cat > /etc/monit.d/freeswitch <<EOT
  112. check process freeswitch with pidfile /usr/local/freeswitch/run/freeswitch.pid
  113. group voice
  114. start program = "/etc/init.d/freeswitch start"
  115. stop program = "/etc/init.d/freeswitch stop"
  116. if failed port 5060 type UDP then restart
  117. if 5 restarts within 5 cycles then timeout
  118. depends on freeswitch_bin
  119. depends on freeswitch_rc
  120. check file freeswitch_bin with path /usr/local/freeswitch/bin/freeswitch
  121. group voice
  122. if failed checksum then unmonitor
  123. if failed permission 755 then unmonitor
  124. if failed uid freeswitch then unmonitor
  125. check file freeswitch_rc with path /etc/init.d/freeswitch
  126. group voice
  127. if failed checksum then unmonitor
  128. if failed permission 755 then unmonitor
  129. if failed uid root then unmonitor
  130. if failed gid root then unmonitor
  131. EOT
  132. #Add Fail2Ban configs for
  133. echo > /etc/fail2ban/filter.d/freeswitch.conf << EOT
  134. # Fail2Ban configuration file
  135. #
  136. # Author: Rupa SChomaker
  137. #
  138. [Definition]
  139. # Option: failregex
  140. # Notes.: regex to match the password failures messages in the logfile. The
  141. # host must be matched by a group named "host". The tag "<HOST>" can
  142. # be used for standard IP/hostname matching and is only an alias for
  143. # (?:::f{4,6}:)?(?P<host>[\w\-.^_]+)
  144. # Values: TEXT
  145. #
  146. failregex = \[WARNING\] sofia_reg.c:\d+ SIP auth failure \(REGISTER\) on sofia profile \'\S+\' for \[.*\] from ip <HOST>
  147. \[WARNING\] sofia_reg.c:\d+ SIP auth failure \(INVITE\) on sofia profile \'\S+\' for \[.*\] from ip <HOST>
  148. # Option: ignoreregex
  149. # Notes.: regex to ignore. If this regex matches, the line is ignored.
  150. # Values: TEXT
  151. #
  152. ignoreregex =
  153. EOT
  154. echo >> /etc/fail2ban/jail.conf << EOT
  155. [freeswitch-tcp]
  156. enabled = true
  157. port = 5060,5061,5080,5081
  158. protocol = tcp
  159. filter = freeswitch
  160. logpath = /usr/local/freeswitch/log/freeswitch.log
  161. action = iptables-allports[name=freeswitch-tcp, protocol=all]
  162. sendmail-whois[name=FreeSwitch, dest=root, [email protected]]
  163. [freeswitch-udp]
  164. enabled = true
  165. port = 5060,5061,5080,5081
  166. protocol = udp
  167. filter = freeswitch
  168. logpath = /usr/local/freeswitch/log/freeswitch.log
  169. action = iptables-allports[name=freeswitch-udp, protocol=all]
  170. sendmail-whois[name=FreeSwitch, dest=root, [email protected]]
  171. EOT
  172. echo > /etc/fail2ban/filter.d/fusionpbx.conf << EOT
  173. # Fail2Ban configuration file
  174. #
  175. # Author: soapee01
  176. #
  177. [Definition]
  178. # Option: failregex
  179. # Notes.: regex to match the password failures messages in the logfile. The
  180. # host must be matched by a group named "host". The tag "<HOST>" can
  181. # be used for standard IP/hostname matching and is only an alias for
  182. # (?:::f{4,6}:)?(?P<host>[\w\-.^_]+)
  183. # Values: TEXT
  184. #
  185. #failregex = [hostname] FusionPBX: \[<HOST>\] authentication failed
  186. #[hostname] variable doesn't seem to work in every case. Do this instead:
  187. failregex = .* FusionPBX: \[<HOST>\] authentication failed for
  188. = .* FusionPBX: \[<HOST>\] provision attempt bad password for
  189. # Option: ignoreregex
  190. # Notes.: regex to ignore. If this regex matches, the line is ignored.
  191. # Values: TEXT
  192. #
  193. ignoreregex =
  194. EOT
  195. echo >> /etc/fail2ban/jail.conf << EOT
  196. [fusionpbx]
  197. enabled = true
  198. port = 80,443
  199. protocol = tcp
  200. filter = fusionpbx
  201. logpath = /var/log/messages
  202. action = iptables-allports[name=fusionpbx, protocol=all]
  203. sendmail-whois[name=FusionPBX, dest=root, [email protected]]
  204. EOT
  205. # start up some services and set them to run at boot
  206. service freeswitch start
  207. service httpd restart
  208. chkconfig freeswitch on
  209. chkconfig httpd on
  210. service monit start
  211. chkconfig monit on
  212. # INIT Postgresql, and set it for easyness
  213. #quick hack to postgresql init script to init the DB with trust access **** YOU MAY NOT WANT THIS FOR PRODUCTION ****
  214. /bin/sed -i -e s,'ident','trust', /etc/init.d/postgresql
  215. service postgresql initdb
  216. service postgresql start
  217. chkconfig postgresql on
  218. #set this back to normal
  219. /bin/sed -i -e s,'trust','ident', /etc/init.d/postgresql
  220. #create users for core Freeswitch
  221. cd /var/tmp
  222. sudo -u postgres /usr/pgsql-9.2/bin/createuser -s -e freeswitch
  223. sudo -u postgres /usr/pgsql-9.2/bin/createdb -E UTF8 -O freeswitch freeswitch
  224. # dz create a fusionpbx user and a fusionpbx database.
  225. cd /var/tmp
  226. sudo -u postgres /usr/pgsql-9.2/bin/createuser -s -e fusionpbx
  227. sudo -u postgres /usr/pgsql-9.2/bin/createdb -E UTF8 -O fusionpbx fusionpbx
  228. #disable epel repo for normal use. Leaving it enabled canhave unintended consequences
  229. /bin/sed -i -e s,'enabled=1','enabled=0', /etc/yum.repos.d/epel.repo
  230. #Make the Prompt Pretty and add a few aliases that come in handy
  231. cat >>~/.bashrc <<EOT
  232. export LESSCHARSET="latin1"
  233. export LESS="-R"
  234. export CHARSET="ISO-8859-1"
  235. export PS1='\n\[\033[01;31m\]\u@\h\[\033[01;36m\] [\d \@] \[\033[01;33m\] \w\n\[\033[00m\]<\#>:'
  236. export PS2="\[\033[1m\]> \[\033[0m\]"
  237. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
  238. export VISUAL=vim
  239. umask 022
  240. alias vi='vim'
  241. alias fstop='top -p \`cat /usr/local/freeswitch/run/freeswitch.pid\`'
  242. alias fsgdb='gdb /usr/local/freeswitch/bin/freeswitch \`cat /usr/local/freeswitch/run/freeswitch.pid\`'
  243. alias fscore='gdb /usr/local/freeswitch/bin/freeswitch \`ls -rt core.* | tail -n1\`'
  244. EOT
  245. #Add a screenrc with a status line, a big scroll back and ^\ as the metakey as to not screw with emacs users
  246. cat >> ~/.screenrc <<EOT
  247. hardstatus alwaysignore
  248. startup_message off
  249. escape ^\b
  250. defscrollback 8000
  251. # status line at the bottom
  252. hardstatus on
  253. hardstatus alwayslastline
  254. hardstatus string "%{.bW}%-w%{.rW}%f%n %t%{-}%+w %=%{..G}[%H %l] %{..Y} %m/%d %c "
  255. termcapinfo xterm \'is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l\'
  256. EOT
  257. # and finally lets fix up IPTables so things works correctly
  258. #Block 'friendly-scanner' AKA sipvicious
  259. iptables -I INPUT -p udp --dport 5060 -m string --string "friendly-scanner" --algo bm -j DROP
  260. iptables -I INPUT -p udp --dport 5080 -m string --string "friendly-scanner" --algo bm -j DROP
  261. #rate limit registrations to keep us from getting hammered on
  262. iptables -I INPUT -m string --string "REGISTER sip:" --algo bm --to 65 -m hashlimit --hashlimit 4/minute --hashlimit-burst 1 --hashlimit-mode srcip,dstport --hashlimit-name sip_r_limit -j ACCEPT
  263. # FreeSwitch ports internal SIP profile
  264. iptables -I INPUT -p udp -m udp --dport 5060 -j ACCEPT
  265. iptables -I INPUT -p tcp -m tcp --dport 5060 -j ACCEPT
  266. # FreeSwitch Ports external SIP profile
  267. iptables -I INPUT -p udp -m udp --dport 5080 -j ACCEPT
  268. iptables -I INPUT -p tcp -m tcp --dport 5080 -j ACCEPT
  269. # RTP Traffic 16384-32768
  270. iptables -I INPUT -p udp -m udp --dport 16384:32768 -j ACCEPT
  271. # Ports for the Web GUI
  272. iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
  273. iptables -I INPUT -p tcp -m tcp --dport 443 -j ACCEPT
  274. #save the IPTables rules for later
  275. service iptables save
  276. LOCAL_IP=`ifconfig eth0 | head -n2 | tail -n1 | cut -d' ' -f12 | cut -c 6-`
  277. cat <<EOT
  278. As long as you didnt see errors by this point, PostgreSQL, FreeSWITCH, FusionPBX, Fail2Ban, and Monit should in installed.
  279. Point your browser to http://$LOCAL_IP/ and let the FusionPBX installer take it from there.
  280. EOT