addons.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. # get pwd
  20. sed -i "s@^oneinstack_dir.*@oneinstack_dir=$(pwd)@" ./options.conf
  21. . ./versions.txt
  22. . ./options.conf
  23. . ./include/color.sh
  24. . ./include/memory.sh
  25. . ./include/check_os.sh
  26. . ./include/check_download.sh
  27. . ./include/download.sh
  28. . ./include/get_char.sh
  29. . ./include/zendopcache.sh
  30. . ./include/xcache.sh
  31. . ./include/apcu.sh
  32. . ./include/eaccelerator.sh
  33. . ./include/ZendGuardLoader.sh
  34. . ./include/ioncube.sh
  35. . ./include/ImageMagick.sh
  36. . ./include/GraphicsMagick.sh
  37. . ./include/memcached.sh
  38. . ./include/redis.sh
  39. . ./include/python.sh
  40. # Check if user is root
  41. [ $(id -u) != '0' ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  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. /bin/cp /etc/fail2ban/jail.{conf,local}
  109. sed -i 's@^# \[sshd\]@[sshd]@' /etc/fail2ban/jail.local
  110. sed -i 's@^# enabled = true@enabled = true@' /etc/fail2ban/jail.local
  111. if [ "${OS}" == "CentOS" ]; then
  112. sed -i 's@%(sshd_log)s@/var/log/secure@' /etc/fail2ban/jail.local
  113. /bin/cp files/redhat-initd /etc/init.d/fail2ban
  114. sed -i "s@^FAIL2BAN=.*@FAIL2BAN=${python_install_dir}/bin/fail2ban-client@" /etc/init.d/fail2ban
  115. chmod +x /etc/init.d/fail2ban
  116. chkconfig --add fail2ban
  117. chkconfig fail2ban on
  118. fi
  119. if [[ "${OS}" =~ ^Ubuntu$|^Debian$ ]]; then
  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. cat > /etc/logrotate.d/fail2ban << EOF
  127. /var/log/fail2ban.log {
  128. missingok
  129. notifempty
  130. postrotate
  131. ${python_install_dir}/bin/fail2ban-client flushlogs >/dev/null || true
  132. endscript
  133. }
  134. EOF
  135. kill -9 `ps -ef | grep fail2ban | grep -v grep | awk '{print $2}'`
  136. /etc/init.d/fail2ban start
  137. popd
  138. if [ -e "${python_install_dir}/bin/fail2ban-python" ]; then
  139. echo; echo "${CSUCCESS}fail2ban installed successfully! ${CEND}"
  140. else
  141. echo; echo "${CFAILURE}fail2ban install failed, Please try again! ${CEND}"
  142. fi
  143. popd
  144. }
  145. Uninstall_fail2ban() {
  146. /etc/init.d/fail2ban stop
  147. ${python_install_dir}/bin/pip uninstall -y fail2ban > /dev/null 2>&1
  148. rm -rf /etc/init.d/fail2ban /etc/fail2ban /etc/logrotate.d/fail2ban /var/log/fail2ban.* /var/run/fail2ban
  149. echo; echo "${CMSG}fail2ban uninstall completed${CEND}";
  150. }
  151. ACTION_FUN() {
  152. while :; do
  153. echo
  154. echo "Please select an action:"
  155. echo -e "\t${CMSG}1${CEND}. install"
  156. echo -e "\t${CMSG}2${CEND}. uninstall"
  157. read -p "Please input a number:(Default 1 press Enter) " ACTION
  158. [ -z "${ACTION}" ] && ACTION=1
  159. if [[ ! "${ACTION}" =~ ^[1,2]$ ]]; then
  160. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  161. else
  162. break
  163. fi
  164. done
  165. }
  166. while :;do
  167. printf "
  168. What Are You Doing?
  169. \t${CMSG}1${CEND}. Install/Uninstall PHP opcode cache
  170. \t${CMSG}2${CEND}. Install/Uninstall ZendGuardLoader/ionCube PHP Extension
  171. \t${CMSG}3${CEND}. Install/Uninstall ImageMagick/GraphicsMagick PHP Extension
  172. \t${CMSG}4${CEND}. Install/Uninstall fileinfo PHP Extension
  173. \t${CMSG}5${CEND}. Install/Uninstall memcached/memcache
  174. \t${CMSG}6${CEND}. Install/Uninstall Redis
  175. \t${CMSG}7${CEND}. Install/Uninstall Let's Encrypt client
  176. \t${CMSG}8${CEND}. Install/Uninstall fail2ban
  177. \t${CMSG}q${CEND}. Exit
  178. "
  179. read -p "Please input the correct option: " Number
  180. if [[ ! "${Number}" =~ ^[1-8,q]$ ]]; then
  181. echo "${CFAILURE}input error! Please only input 1 ~ 8 and q${CEND}"
  182. else
  183. case "${Number}" in
  184. 1)
  185. ACTION_FUN
  186. while :; do echo
  187. echo "Please select a opcode cache of the PHP:"
  188. echo -e "\t${CMSG}1${CEND}. Zend OPcache"
  189. echo -e "\t${CMSG}2${CEND}. XCache"
  190. echo -e "\t${CMSG}3${CEND}. APCU"
  191. echo -e "\t${CMSG}4${CEND}. eAccelerator"
  192. read -p "Please input a number:(Default 1 press Enter) " PHP_cache
  193. [ -z "${PHP_cache}" ] && PHP_cache=1
  194. if [[ ! "${PHP_cache}" =~ ^[1-4]$ ]]; then
  195. echo "${CWARNING}input error! Please only input number 1,2,3,4${CEND}"
  196. else
  197. case "${PHP_cache}" in
  198. 1)
  199. PHP_extension=opcache
  200. ;;
  201. 2)
  202. PHP_extension=xcache
  203. ;;
  204. 3)
  205. PHP_extension=apcu
  206. ;;
  207. 4)
  208. PHP_extension=eaccelerator
  209. ;;
  210. esac
  211. break
  212. fi
  213. done
  214. if [ "${ACTION}" = '1' ]; then
  215. Check_PHP_Extension
  216. if [ -e ${php_install_dir}/etc/php.d/ext-ZendGuardLoader.ini ]; then
  217. echo; echo "${CWARNING}You have to install ZendGuardLoader, You need to uninstall it before install ${PHP_extension}! ${CEND}"; echo; exit 1
  218. else
  219. case "${PHP_cache}" in
  220. 1)
  221. pushd ${oneinstack_dir}/src
  222. if [[ "${PHP_main_version}" =~ ^5.[3-4]$ ]]; then
  223. src_url=https://pecl.php.net/get/zendopcache-${zendopcache_version}.tgz && Download_src
  224. Install_ZendOPcache
  225. else
  226. src_url=http://www.php.net/distributions/php-${PHP_detail_version}.tar.gz && Download_src
  227. Install_ZendOPcache
  228. fi
  229. popd
  230. Check_succ
  231. ;;
  232. 2)
  233. if [[ ${PHP_main_version} =~ ^5.[3-6]$ ]]; then
  234. while :; do
  235. read -p "Please input xcache admin password: " xcache_admin_pass
  236. (( ${#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}"
  237. done
  238. checkDownload
  239. Install_XCache
  240. Check_succ
  241. else
  242. echo "${CWARNING}Your php does not support XCache! ${CEND}"; exit 1
  243. fi
  244. ;;
  245. 3)
  246. if [[ "${PHP_main_version}" =~ ^5.[3-6]$|^7.[0-1]$ ]]; then
  247. checkDownload
  248. Install_APCU
  249. Check_succ
  250. else
  251. echo "${CWARNING}Your php does not support APCU! ${CEND}"; exit 1
  252. fi
  253. ;;
  254. 4)
  255. if [[ "${PHP_main_version}" =~ ^5.[3-4]$ ]]; then
  256. checkDownload
  257. Install_eAccelerator
  258. Check_succ
  259. else
  260. echo "${CWARNING}Your php does not support eAccelerator! ${CEND}"; exit 1
  261. fi
  262. ;;
  263. esac
  264. fi
  265. else
  266. Uninstall_succ
  267. fi
  268. ;;
  269. 2)
  270. ACTION_FUN
  271. while :; do echo
  272. echo "Please select ZendGuardLoader/ionCube:"
  273. echo -e "\t${CMSG}1${CEND}. ZendGuardLoader"
  274. echo -e "\t${CMSG}2${CEND}. ionCube Loader"
  275. read -p "Please input a number:(Default 1 press Enter) " Loader
  276. [ -z "${Loader}" ] && Loader=1
  277. if [[ ! "${Loader}" =~ ^[1,2]$ ]]; then
  278. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  279. else
  280. [ "${Loader}" = '1' ] && PHP_extension=ZendGuardLoader
  281. [ "${Loader}" = '2' ] && PHP_extension=0ioncube
  282. break
  283. fi
  284. done
  285. if [ "${ACTION}" = '1' ]; then
  286. Check_PHP_Extension
  287. if [ "${Loader}" = '1' ]; then
  288. if [[ "${PHP_main_version}" =~ ^5.[3-6]$ ]] || [ "${armPlatform}" != 'y' ]; then
  289. if [ -e ${php_install_dir}/etc/php.d/ext-opcache.ini ]; then
  290. echo; echo "${CWARNING}You have to install OpCache, You need to uninstall it before install ZendGuardLoader! ${CEND}"; echo; exit 1
  291. else
  292. ZendGuardLoader_yn='y' && checkDownload
  293. Install_ZendGuardLoader
  294. Check_succ
  295. fi
  296. else
  297. echo; echo "${CWARNING}Your php ${PHP_detail_version} or platform ${TARGET_ARCH} does not support ${PHP_extension}! ${CEND}";
  298. fi
  299. elif [ "${Loader}" = '2' ]; then
  300. if [[ "${PHP_main_version}" =~ ^5.[3-6]$|^7.0$ ]] || [ "${TARGET_ARCH}" != "arm64" ]; then
  301. ionCube_yn='y' && checkDownload
  302. Install_ionCube
  303. Restart_PHP; echo "${CSUCCESS}PHP ioncube module installed successfully! ${CEND}";
  304. else
  305. echo; echo "${CWARNING}Your php ${PHP_detail_version} or platform ${TARGET_ARCH} does not support ${PHP_extension}! ${CEND}";
  306. fi
  307. fi
  308. else
  309. Uninstall_succ
  310. fi
  311. ;;
  312. 3)
  313. ACTION_FUN
  314. while :; do echo
  315. echo "Please select ImageMagick/GraphicsMagick:"
  316. echo -e "\t${CMSG}1${CEND}. ImageMagick"
  317. echo -e "\t${CMSG}2${CEND}. GraphicsMagick"
  318. read -p "Please input a number:(Default 1 press Enter) " Magick
  319. [ -z "${Magick}" ] && Magick=1
  320. if [[ ! "${Magick}" =~ ^[1,2]$ ]]; then
  321. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  322. else
  323. [ "${Magick}" = '1' ] && PHP_extension=imagick
  324. [ "${Magick}" = '2' ] && PHP_extension=gmagick
  325. break
  326. fi
  327. done
  328. if [ "${ACTION}" = '1' ]; then
  329. Check_PHP_Extension
  330. Magick_yn=y && checkDownload
  331. if [ "${Magick}" = '1' ]; then
  332. [ ! -d "/usr/local/imagemagick" ] && Install_ImageMagick
  333. Install_php-imagick
  334. Check_succ
  335. elif [ "${Magick}" = '2' ]; then
  336. [ ! -d "/usr/local/graphicsmagick" ] && Install_GraphicsMagick
  337. Install_php-gmagick
  338. Check_succ
  339. fi
  340. else
  341. Uninstall_succ
  342. [ -d "/usr/local/imagemagick" ] && rm -rf /usr/local/imagemagick
  343. [ -d "/usr/local/graphicsmagick" ] && rm -rf /usr/local/graphicsmagick
  344. fi
  345. ;;
  346. 4)
  347. ACTION_FUN
  348. PHP_extension=fileinfo
  349. if [ "${ACTION}" = '1' ]; then
  350. Check_PHP_Extension
  351. pushd ${oneinstack_dir}/src
  352. src_url=http://www.php.net/distributions/php-${PHP_detail_version}.tar.gz && Download_src
  353. tar xzf php-${PHP_detail_version}.tar.gz
  354. pushd php-${PHP_detail_version}/ext/fileinfo
  355. ${php_install_dir}/bin/phpize
  356. ./configure --with-php-config=${php_install_dir}/bin/php-config
  357. make -j ${THREAD} && make install
  358. popd;popd
  359. rm -rf php-${PHP_detail_version}
  360. echo "extension=fileinfo.so" > ${php_install_dir}/etc/php.d/ext-fileinfo.ini
  361. Check_succ
  362. else
  363. Uninstall_succ
  364. fi
  365. ;;
  366. 5)
  367. ACTION_FUN
  368. while :; do echo
  369. echo "Please select memcache/memcached PHP Extension:"
  370. echo -e "\t${CMSG}1${CEND}. memcache PHP Extension"
  371. echo -e "\t${CMSG}2${CEND}. memcached PHP Extension"
  372. echo -e "\t${CMSG}3${CEND}. memcache/memcached PHP Extension"
  373. read -p "Please input a number:(Default 1 press Enter) " Memcache
  374. [ -z "${Memcache}" ] && Memcache=1
  375. if [[ ! "${Memcache}" =~ ^[1-3]$ ]]; then
  376. echo "${CWARNING}input error! Please only input number 1,2,3${CEND}"
  377. else
  378. [ "${Memcache}" = '1' ] && PHP_extension=memcache
  379. [ "${Memcache}" = '2' ] && PHP_extension=memcached
  380. break
  381. fi
  382. done
  383. if [ "${ACTION}" = '1' ]; then
  384. memcached_yn=y && checkDownload
  385. case "${Memcache}" in
  386. 1)
  387. [ ! -d "${memcached_install_dir}/include/memcached" ] && Install_memcached
  388. Check_PHP_Extension
  389. Install_php-memcache
  390. Check_succ
  391. ;;
  392. 2)
  393. [ ! -d "${memcached_install_dir}/include/memcached" ] && Install_memcached
  394. Check_PHP_Extension
  395. Install_php-memcached
  396. Check_succ
  397. ;;
  398. 3)
  399. [ ! -d "${memcached_install_dir}/include/memcached" ] && Install_memcached
  400. PHP_extension=memcache && Check_PHP_Extension
  401. Install_php-memcache
  402. PHP_extension=memcached && Check_PHP_Extension
  403. Install_php-memcached
  404. [ -f "${phpExtensionDir}/memcache.so" -a "${phpExtensionDir}/memcached.so" ] && { Restart_PHP; echo;echo "${CSUCCESS}PHP memcache/memcached module installed successfully! ${CEND}"; }
  405. ;;
  406. esac
  407. else
  408. PHP_extension=memcache && Uninstall_succ
  409. PHP_extension=memcached && Uninstall_succ
  410. [ -e "${memcached_install_dir}" ] && { service memcached stop > /dev/null 2>&1; rm -rf ${memcached_install_dir} /etc/init.d/memcached /usr/bin/memcached; }
  411. fi
  412. ;;
  413. 6)
  414. ACTION_FUN
  415. PHP_extension=redis
  416. redis_yn=y && checkDownload
  417. if [ "${ACTION}" = '1' ]; then
  418. [ ! -d "${redis_install_dir}" ] && Install_redis-server
  419. Check_PHP_Extension
  420. Install_php-redis
  421. else
  422. Uninstall_succ
  423. [ -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-*; }
  424. fi
  425. ;;
  426. 7)
  427. ACTION_FUN
  428. if [ "${ACTION}" = '1' ]; then
  429. Install_letsencrypt
  430. else
  431. Uninstall_letsencrypt
  432. fi
  433. ;;
  434. 8)
  435. ACTION_FUN
  436. if [ "${ACTION}" = '1' ]; then
  437. Install_fail2ban
  438. else
  439. Uninstall_fail2ban
  440. fi
  441. ;;
  442. q)
  443. exit
  444. ;;
  445. esac
  446. fi
  447. done