addons.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  11. clear
  12. printf "
  13. #######################################################################
  14. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  15. # Install/Uninstall Extensions #
  16. # For more information please visit https://oneinstack.com #
  17. #######################################################################
  18. "
  19. # Check if user is root
  20. [ $(id -u) != '0' ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  21. # get pwd
  22. sed -i "s@^oneinstack_dir.*@oneinstack_dir=$(pwd)@" ./options.conf
  23. . ./versions.txt
  24. . ./options.conf
  25. . ./include/color.sh
  26. . ./include/memory.sh
  27. . ./include/check_os.sh
  28. . ./include/check_download.sh
  29. . ./include/download.sh
  30. . ./include/get_char.sh
  31. . ./include/zendopcache.sh
  32. . ./include/xcache.sh
  33. . ./include/apcu.sh
  34. . ./include/eaccelerator.sh
  35. . ./include/ZendGuardLoader.sh
  36. . ./include/ioncube.sh
  37. . ./include/ImageMagick.sh
  38. . ./include/GraphicsMagick.sh
  39. . ./include/memcached.sh
  40. . ./include/redis.sh
  41. . ./include/python.sh
  42. # Check PHP
  43. if [ -e "${php_install_dir}/bin/phpize" ]; then
  44. phpExtensionDir=$(${php_install_dir}/bin/php-config --extension-dir)
  45. PHP_detail_version=$(${php_install_dir}/bin/php -r 'echo PHP_VERSION;')
  46. PHP_main_version=${PHP_detail_version%.*}
  47. case "${PHP_main_version}" in
  48. "5.3")
  49. PHP_version=1
  50. ;;
  51. "5.4")
  52. PHP_version=2
  53. ;;
  54. "5.5")
  55. PHP_version=3
  56. ;;
  57. "5.6")
  58. PHP_version=4
  59. ;;
  60. "7.0" | "7.1")
  61. PHP_version=5
  62. ;;
  63. *)
  64. echo "${CFAILURE}Your PHP version ${PHP_main_version} is not supported! ${CEND}"
  65. kill -9 $$
  66. ;;
  67. esac
  68. fi
  69. # Check PHP Extensions
  70. Check_PHP_Extension() {
  71. [ -e "${php_install_dir}/etc/php.d/ext-${PHP_extension}.ini" ] && { echo "${CWARNING}PHP ${PHP_extension} module already installed! ${CEND}"; exit 1; }
  72. }
  73. # restart PHP
  74. Restart_PHP() {
  75. [ -e "${apache_install_dir}/conf/httpd.conf" ] && /etc/init.d/httpd restart || /etc/init.d/php-fpm restart
  76. }
  77. # Check succ
  78. Check_succ() {
  79. [ -f "${phpExtensionDir}/${PHP_extension}.so" ] && { Restart_PHP; echo;echo "${CSUCCESS}PHP ${PHP_extension} module installed successfully! ${CEND}"; }
  80. }
  81. # Uninstall succ
  82. Uninstall_succ() {
  83. [ -e "${php_install_dir}/etc/php.d/ext-${PHP_extension}.ini" ] && { rm -rf ${php_install_dir}/etc/php.d/ext-${PHP_extension}.ini; Restart_PHP; echo; echo "${CMSG}PHP ${PHP_extension} module uninstall completed${CEND}"; } || { echo; echo "${CWARNING}${PHP_extension} module does not exist! ${CEND}"; }
  84. }
  85. Install_letsencrypt() {
  86. [ ! -e "${python_install_dir}/bin/python" ] && Install_Python
  87. ${python_install_dir}/bin/pip install certbot
  88. if [ -e "${python_install_dir}/bin/certbot" ]; then
  89. echo; echo "${CSUCCESS}Let's Encrypt client installed successfully! ${CEND}"
  90. else
  91. echo; echo "${CFAILURE}Let's Encrypt client install failed, Please try again! ${CEND}"
  92. fi
  93. }
  94. Uninstall_letsencrypt() {
  95. ${python_install_dir}/bin/pip uninstall -y certbot > /dev/null 2>&1
  96. rm -rf /etc/letsencrypt /var/log/letsencrypt /var/lib/letsencrypt ${python_install_dir}
  97. [ "${OS}" == "CentOS" ] && Cron_file=/var/spool/cron/root || Cron_file=/var/spool/cron/crontabs/root
  98. [ -e "$Cron_file" ] && sed -i '/certbot/d' ${Cron_file}
  99. echo; echo "${CMSG}Let's Encrypt client uninstall completed${CEND}";
  100. }
  101. Install_fail2ban() {
  102. [ ! -e "${python_install_dir}/bin/python" ] && Install_Python
  103. pushd ${oneinstack_dir}/src
  104. src_url=http://mirrors.linuxeye.com/oneinstack/src/fail2ban-${fail2ban_version}.tar.gz && Download_src
  105. tar xzf fail2ban-${fail2ban_version}.tar.gz
  106. pushd fail2ban-${fail2ban_version}
  107. ${python_install_dir}/bin/python setup.py install
  108. if [ "${OS}" == "CentOS" ]; then
  109. LOGPATH=/var/log/secure
  110. /bin/cp files/redhat-initd /etc/init.d/fail2ban
  111. sed -i "s@^FAIL2BAN=.*@FAIL2BAN=${python_install_dir}/bin/fail2ban-client@" /etc/init.d/fail2ban
  112. sed -i 's@Starting fail2ban.*@&\n [ ! -e "/var/run/fail2ban" ] \&\& mkdir /var/run/fail2ban@' /etc/init.d/fail2ban
  113. chmod +x /etc/init.d/fail2ban
  114. chkconfig --add fail2ban
  115. chkconfig fail2ban on
  116. fi
  117. if [[ "${OS}" =~ ^Ubuntu$|^Debian$ ]]; then
  118. LOGPATH=/var/log/auth.log
  119. /bin/cp files/debian-initd /etc/init.d/fail2ban
  120. sed -i 's@2 3 4 5@3 4 5@' /etc/init.d/fail2ban
  121. sed -i "s@^DAEMON=.*@DAEMON=${python_install_dir}/bin/\$NAME-client@" /etc/init.d/fail2ban
  122. chmod +x /etc/init.d/fail2ban
  123. update-rc.d fail2ban defaults
  124. fi
  125. [ -z "`grep ^Port /etc/ssh/sshd_config`" ] && ssh_port=22 || ssh_port=`grep ^Port /etc/ssh/sshd_config | awk '{print $2}'`
  126. cat > /etc/fail2ban/jail.local << EOF
  127. [DEFAULT]
  128. ignoreip = 127.0.0.1/8
  129. bantime = 86400
  130. findtime = 600
  131. maxretry = 5
  132. [ssh-iptables]
  133. enabled = true
  134. filter = sshd
  135. action = iptables[name=SSH, port=$ssh_port, protocol=tcp]
  136. logpath = $LOGPATH
  137. EOF
  138. cat > /etc/logrotate.d/fail2ban << EOF
  139. /var/log/fail2ban.log {
  140. missingok
  141. notifempty
  142. postrotate
  143. ${python_install_dir}/bin/fail2ban-client flushlogs >/dev/null || true
  144. endscript
  145. }
  146. EOF
  147. sed -i 's@^iptables = iptables.*@iptables = iptables@' /etc/fail2ban/action.d/iptables-common.conf
  148. kill -9 `ps -ef | grep fail2ban | grep -v grep | awk '{print $2}'` > /dev/null 2>&1
  149. /etc/init.d/fail2ban start
  150. popd
  151. if [ -e "${python_install_dir}/bin/fail2ban-python" ]; then
  152. echo; echo "${CSUCCESS}fail2ban installed successfully! ${CEND}"
  153. else
  154. echo; echo "${CFAILURE}fail2ban install failed, Please try again! ${CEND}"
  155. fi
  156. popd
  157. }
  158. Uninstall_fail2ban() {
  159. /etc/init.d/fail2ban stop
  160. ${python_install_dir}/bin/pip uninstall -y fail2ban > /dev/null 2>&1
  161. rm -rf /etc/init.d/fail2ban /etc/fail2ban /etc/logrotate.d/fail2ban /var/log/fail2ban.* /var/run/fail2ban
  162. echo; echo "${CMSG}fail2ban uninstall completed${CEND}";
  163. }
  164. ACTION_FUN() {
  165. while :; do
  166. echo
  167. echo "Please select an action:"
  168. echo -e "\t${CMSG}1${CEND}. install"
  169. echo -e "\t${CMSG}2${CEND}. uninstall"
  170. read -p "Please input a number:(Default 1 press Enter) " ACTION
  171. [ -z "${ACTION}" ] && ACTION=1
  172. if [[ ! "${ACTION}" =~ ^[1,2]$ ]]; then
  173. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  174. else
  175. break
  176. fi
  177. done
  178. }
  179. while :;do
  180. printf "
  181. What Are You Doing?
  182. \t${CMSG}1${CEND}. Install/Uninstall PHP opcode cache
  183. \t${CMSG}2${CEND}. Install/Uninstall ZendGuardLoader/ionCube PHP Extension
  184. \t${CMSG}3${CEND}. Install/Uninstall ImageMagick/GraphicsMagick PHP Extension
  185. \t${CMSG}4${CEND}. Install/Uninstall fileinfo PHP Extension
  186. \t${CMSG}5${CEND}. Install/Uninstall memcached/memcache
  187. \t${CMSG}6${CEND}. Install/Uninstall Redis
  188. \t${CMSG}7${CEND}. Install/Uninstall Let's Encrypt client
  189. \t${CMSG}8${CEND}. Install/Uninstall fail2ban
  190. \t${CMSG}q${CEND}. Exit
  191. "
  192. read -p "Please input the correct option: " Number
  193. if [[ ! "${Number}" =~ ^[1-8,q]$ ]]; then
  194. echo "${CFAILURE}input error! Please only input 1 ~ 8 and q${CEND}"
  195. else
  196. case "${Number}" in
  197. 1)
  198. ACTION_FUN
  199. while :; do echo
  200. echo "Please select a opcode cache of the PHP:"
  201. echo -e "\t${CMSG}1${CEND}. Zend OPcache"
  202. echo -e "\t${CMSG}2${CEND}. XCache"
  203. echo -e "\t${CMSG}3${CEND}. APCU"
  204. echo -e "\t${CMSG}4${CEND}. eAccelerator"
  205. read -p "Please input a number:(Default 1 press Enter) " PHP_cache
  206. [ -z "${PHP_cache}" ] && PHP_cache=1
  207. if [[ ! "${PHP_cache}" =~ ^[1-4]$ ]]; then
  208. echo "${CWARNING}input error! Please only input number 1,2,3,4${CEND}"
  209. else
  210. case "${PHP_cache}" in
  211. 1)
  212. PHP_extension=opcache
  213. ;;
  214. 2)
  215. PHP_extension=xcache
  216. ;;
  217. 3)
  218. PHP_extension=apcu
  219. ;;
  220. 4)
  221. PHP_extension=eaccelerator
  222. ;;
  223. esac
  224. break
  225. fi
  226. done
  227. if [ "${ACTION}" = '1' ]; then
  228. Check_PHP_Extension
  229. if [ -e ${php_install_dir}/etc/php.d/ext-ZendGuardLoader.ini ]; then
  230. echo; echo "${CWARNING}You have to install ZendGuardLoader, You need to uninstall it before install ${PHP_extension}! ${CEND}"; echo; exit 1
  231. else
  232. case "${PHP_cache}" in
  233. 1)
  234. pushd ${oneinstack_dir}/src
  235. if [[ "${PHP_main_version}" =~ ^5.[3-4]$ ]]; then
  236. src_url=https://pecl.php.net/get/zendopcache-${zendopcache_version}.tgz && Download_src
  237. Install_ZendOPcache
  238. else
  239. src_url=http://www.php.net/distributions/php-${PHP_detail_version}.tar.gz && Download_src
  240. Install_ZendOPcache
  241. fi
  242. popd
  243. Check_succ
  244. ;;
  245. 2)
  246. if [[ ${PHP_main_version} =~ ^5.[3-6]$ ]]; then
  247. while :; do
  248. read -p "Please input xcache admin password: " xcache_admin_pass
  249. (( ${#xcache_admin_pass} >= 5 )) && { xcache_admin_md5_pass=$(echo -n "${xcache_admin_pass}" | md5sum | awk '{print $1}') ; break ; } || echo "${CFAILURE}xcache admin password least 5 characters! ${CEND}"
  250. done
  251. checkDownload
  252. Install_XCache
  253. Check_succ
  254. else
  255. echo "${CWARNING}Your php does not support XCache! ${CEND}"; exit 1
  256. fi
  257. ;;
  258. 3)
  259. if [[ "${PHP_main_version}" =~ ^5.[3-6]$|^7.[0-1]$ ]]; then
  260. checkDownload
  261. Install_APCU
  262. Check_succ
  263. else
  264. echo "${CWARNING}Your php does not support APCU! ${CEND}"; exit 1
  265. fi
  266. ;;
  267. 4)
  268. if [[ "${PHP_main_version}" =~ ^5.[3-4]$ ]]; then
  269. checkDownload
  270. Install_eAccelerator
  271. Check_succ
  272. else
  273. echo "${CWARNING}Your php does not support eAccelerator! ${CEND}"; exit 1
  274. fi
  275. ;;
  276. esac
  277. fi
  278. else
  279. Uninstall_succ
  280. fi
  281. ;;
  282. 2)
  283. ACTION_FUN
  284. while :; do echo
  285. echo "Please select ZendGuardLoader/ionCube:"
  286. echo -e "\t${CMSG}1${CEND}. ZendGuardLoader"
  287. echo -e "\t${CMSG}2${CEND}. ionCube Loader"
  288. read -p "Please input a number:(Default 1 press Enter) " Loader
  289. [ -z "${Loader}" ] && Loader=1
  290. if [[ ! "${Loader}" =~ ^[1,2]$ ]]; then
  291. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  292. else
  293. [ "${Loader}" = '1' ] && PHP_extension=ZendGuardLoader
  294. [ "${Loader}" = '2' ] && PHP_extension=0ioncube
  295. break
  296. fi
  297. done
  298. if [ "${ACTION}" = '1' ]; then
  299. Check_PHP_Extension
  300. if [ "${Loader}" = '1' ]; then
  301. if [[ "${PHP_main_version}" =~ ^5.[3-6]$ ]] || [ "${armPlatform}" != 'y' ]; then
  302. if [ -e ${php_install_dir}/etc/php.d/ext-opcache.ini ]; then
  303. echo; echo "${CWARNING}You have to install OpCache, You need to uninstall it before install ZendGuardLoader! ${CEND}"; echo; exit 1
  304. else
  305. ZendGuardLoader_yn='y' && checkDownload
  306. Install_ZendGuardLoader
  307. Check_succ
  308. fi
  309. else
  310. echo; echo "${CWARNING}Your php ${PHP_detail_version} or platform ${TARGET_ARCH} does not support ${PHP_extension}! ${CEND}";
  311. fi
  312. elif [ "${Loader}" = '2' ]; then
  313. if [[ "${PHP_main_version}" =~ ^5.[3-6]$|^7.0$ ]] || [ "${TARGET_ARCH}" != "arm64" ]; then
  314. ionCube_yn='y' && checkDownload
  315. Install_ionCube
  316. Restart_PHP; echo "${CSUCCESS}PHP ioncube module installed successfully! ${CEND}";
  317. else
  318. echo; echo "${CWARNING}Your php ${PHP_detail_version} or platform ${TARGET_ARCH} does not support ${PHP_extension}! ${CEND}";
  319. fi
  320. fi
  321. else
  322. Uninstall_succ
  323. fi
  324. ;;
  325. 3)
  326. ACTION_FUN
  327. while :; do echo
  328. echo "Please select ImageMagick/GraphicsMagick:"
  329. echo -e "\t${CMSG}1${CEND}. ImageMagick"
  330. echo -e "\t${CMSG}2${CEND}. GraphicsMagick"
  331. read -p "Please input a number:(Default 1 press Enter) " Magick
  332. [ -z "${Magick}" ] && Magick=1
  333. if [[ ! "${Magick}" =~ ^[1,2]$ ]]; then
  334. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  335. else
  336. [ "${Magick}" = '1' ] && PHP_extension=imagick
  337. [ "${Magick}" = '2' ] && PHP_extension=gmagick
  338. break
  339. fi
  340. done
  341. if [ "${ACTION}" = '1' ]; then
  342. Check_PHP_Extension
  343. Magick_yn=y && checkDownload
  344. if [ "${Magick}" = '1' ]; then
  345. [ ! -d "/usr/local/imagemagick" ] && Install_ImageMagick
  346. Install_php-imagick
  347. Check_succ
  348. elif [ "${Magick}" = '2' ]; then
  349. [ ! -d "/usr/local/graphicsmagick" ] && Install_GraphicsMagick
  350. Install_php-gmagick
  351. Check_succ
  352. fi
  353. else
  354. Uninstall_succ
  355. [ -d "/usr/local/imagemagick" ] && rm -rf /usr/local/imagemagick
  356. [ -d "/usr/local/graphicsmagick" ] && rm -rf /usr/local/graphicsmagick
  357. fi
  358. ;;
  359. 4)
  360. ACTION_FUN
  361. PHP_extension=fileinfo
  362. if [ "${ACTION}" = '1' ]; then
  363. Check_PHP_Extension
  364. pushd ${oneinstack_dir}/src
  365. src_url=http://www.php.net/distributions/php-${PHP_detail_version}.tar.gz && Download_src
  366. tar xzf php-${PHP_detail_version}.tar.gz
  367. pushd php-${PHP_detail_version}/ext/fileinfo
  368. ${php_install_dir}/bin/phpize
  369. ./configure --with-php-config=${php_install_dir}/bin/php-config
  370. make -j ${THREAD} && make install
  371. popd;popd
  372. rm -rf php-${PHP_detail_version}
  373. echo "extension=fileinfo.so" > ${php_install_dir}/etc/php.d/ext-fileinfo.ini
  374. Check_succ
  375. else
  376. Uninstall_succ
  377. fi
  378. ;;
  379. 5)
  380. ACTION_FUN
  381. while :; do echo
  382. echo "Please select memcache/memcached PHP Extension:"
  383. echo -e "\t${CMSG}1${CEND}. memcache PHP Extension"
  384. echo -e "\t${CMSG}2${CEND}. memcached PHP Extension"
  385. echo -e "\t${CMSG}3${CEND}. memcache/memcached PHP Extension"
  386. read -p "Please input a number:(Default 1 press Enter) " Memcache
  387. [ -z "${Memcache}" ] && Memcache=1
  388. if [[ ! "${Memcache}" =~ ^[1-3]$ ]]; then
  389. echo "${CWARNING}input error! Please only input number 1,2,3${CEND}"
  390. else
  391. [ "${Memcache}" = '1' ] && PHP_extension=memcache
  392. [ "${Memcache}" = '2' ] && PHP_extension=memcached
  393. break
  394. fi
  395. done
  396. if [ "${ACTION}" = '1' ]; then
  397. memcached_yn=y && checkDownload
  398. case "${Memcache}" in
  399. 1)
  400. [ ! -d "${memcached_install_dir}/include/memcached" ] && Install_memcached
  401. Check_PHP_Extension
  402. Install_php-memcache
  403. Check_succ
  404. ;;
  405. 2)
  406. [ ! -d "${memcached_install_dir}/include/memcached" ] && Install_memcached
  407. Check_PHP_Extension
  408. Install_php-memcached
  409. Check_succ
  410. ;;
  411. 3)
  412. [ ! -d "${memcached_install_dir}/include/memcached" ] && Install_memcached
  413. PHP_extension=memcache && Check_PHP_Extension
  414. Install_php-memcache
  415. PHP_extension=memcached && Check_PHP_Extension
  416. Install_php-memcached
  417. [ -f "${phpExtensionDir}/memcache.so" -a "${phpExtensionDir}/memcached.so" ] && { Restart_PHP; echo;echo "${CSUCCESS}PHP memcache/memcached module installed successfully! ${CEND}"; }
  418. ;;
  419. esac
  420. else
  421. PHP_extension=memcache && Uninstall_succ
  422. PHP_extension=memcached && Uninstall_succ
  423. [ -e "${memcached_install_dir}" ] && { service memcached stop > /dev/null 2>&1; rm -rf ${memcached_install_dir} /etc/init.d/memcached /usr/bin/memcached; }
  424. fi
  425. ;;
  426. 6)
  427. ACTION_FUN
  428. PHP_extension=redis
  429. redis_yn=y && checkDownload
  430. if [ "${ACTION}" = '1' ]; then
  431. [ ! -d "${redis_install_dir}" ] && Install_redis-server
  432. Check_PHP_Extension
  433. Install_php-redis
  434. else
  435. Uninstall_succ
  436. [ -e "${redis_install_dir}" ] && { service redis-server stop > /dev/null 2>&1; rm -rf ${redis_install_dir} /etc/init.d/redis-server /usr/local/bin/redis-*; }
  437. fi
  438. ;;
  439. 7)
  440. ACTION_FUN
  441. if [ "${ACTION}" = '1' ]; then
  442. Install_letsencrypt
  443. else
  444. Uninstall_letsencrypt
  445. fi
  446. ;;
  447. 8)
  448. ACTION_FUN
  449. if [ "${ACTION}" = '1' ]; then
  450. Install_fail2ban
  451. else
  452. Uninstall_fail2ban
  453. fi
  454. ;;
  455. q)
  456. exit
  457. ;;
  458. esac
  459. fi
  460. done