addons.sh 18 KB

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