swoole-compiler-loader.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #! /bin/bash
  2. # Swoole-compiler loader install wizard
  3. # Color
  4. RED='\033[0;31m'
  5. YELLOW='\033[0;33m'
  6. NC='\033[0m' # No Color
  7. # Output Logo
  8. echo ""
  9. echo "+-------------------------------------------------------------------+"
  10. echo -e "| ${RED}Swoole Compiler Loader Installer${NC} |"
  11. echo "+-------------------------------------------------------------------+"
  12. echo "| version 1.0.3 (2018-03-15) |"
  13. echo "+-------------------------------------------------------------------+"
  14. echo ""
  15. # The function which outputs help information
  16. help(){
  17. echo " "
  18. echo -e " ${YELLOW}Usage :${NC} bash $0 [options] [arguments]"
  19. echo " "
  20. echo -e " ${YELLOW}Options :${NC} "
  21. echo " -h --help Show the help of swoole compiler loader"
  22. echo " -s --libsodium Install libsodium library"
  23. echo " "
  24. echo -e " ${YELLOW}Optional Arguments :${NC} "
  25. echo " php_path : the absolute path of php-cli or php-fpm executable path"
  26. echo " "
  27. echo -e " ${YELLOW}Examples : ${NC}"
  28. echo " bash $0"
  29. echo " bash $0 /usr/local/sbin/php-fpm"
  30. echo " base $0 -s"
  31. echo " "
  32. }
  33. # The function which outputs choice
  34. yes_or_no(){
  35. if [ $# -eq 0 ]; then
  36. echo -e " ${RED}Error:${NC}"
  37. echo " Wrong function parameter"
  38. echo ""
  39. exit 1;
  40. fi
  41. while true; do
  42. read -p "$* [y/n]: " yn
  43. case $yn in
  44. [Yy]*) return 0 ;;
  45. [Nn]*) return 1 ;;
  46. * ) echo "Please answer yes or no." ;;
  47. esac
  48. done
  49. }
  50. # The function which judges the execute ability of the php_path
  51. check_php(){
  52. if [ $# -eq 0 ]; then
  53. echo -e " ${RED}Error:${NC}"
  54. echo " Wrong function parameter"
  55. echo ""
  56. exit 1;
  57. fi
  58. check_php_path="$1";
  59. which "$check_php_path" &> /dev/null;
  60. check_php_path_exist=$?;
  61. if [ "$check_php_path_exist" == "0" ]; then
  62. check_php_path=$(which "$check_php_path")
  63. if [ -x "$check_php_path" ]; then
  64. return 0;
  65. else
  66. return 1;
  67. fi
  68. else
  69. return 1;
  70. fi
  71. }
  72. # The function which installs the libsodium libray
  73. install_libsodium(){
  74. back_libsodium_path="./libsodium.so"
  75. if [ ! -e "$back_libsodium_path" ]; then
  76. echo ""
  77. echo -e " ${RED}Error:${NC}"
  78. echo " Not found backed libsodium.so, please check the loader folder"
  79. echo ""
  80. exit 1;
  81. fi
  82. ldconfig
  83. libsodium_exists=$(ldconfig -p | awk '/libsodium/')
  84. if [ -n "$libsodium_exists" ]; then
  85. echo " libsodium.so : found"
  86. else
  87. echo " libsodium.so : not found"
  88. echo -e " ${YELLOW}install libsodium.so...${NC}"
  89. fi
  90. enabled_so_path="/usr/lib"
  91. cp "$back_libsodium_path" "$enabled_so_path"
  92. ldconfig
  93. libSodiumPath="$enabled_so_path/libsodium.so"
  94. if [ ! -e $libSodiumPath ]; then
  95. echo ""
  96. echo -e " ${RED}Error:${NC}"
  97. echo " No found shared library libsodium.so"
  98. echo ""
  99. exit 1;
  100. fi
  101. libsodium_exists=$(ldconfig -p | awk '/libsodium/')
  102. if [ ! -n "$libsodium_exists" ]; then
  103. echo ""
  104. echo -e " ${RED}Error:${NC}"
  105. echo " Fail to install libsodium.so"
  106. echo ""
  107. exit 1;
  108. fi
  109. }
  110. # The function which checks the root
  111. check_root(){
  112. if [[ $EUID -ne 0 ]]; then
  113. echo ""
  114. echo -e " ${RED}This program needs root privilege${NC}"
  115. exit 1
  116. fi
  117. }
  118. # Check the parameter of shell sctipt
  119. if [ "$#" == "1" ] ; then
  120. if [[ ( "$1" == "-h" ) || ( "$1" == "--help" ) ]] ; then
  121. help
  122. exit 1
  123. fi
  124. if [[ ( "$1" == "-s" ) || ( "$1" == "--libsodium" ) ]]; then
  125. # Output checking environment information
  126. echo " "
  127. echo -e " ${YELLOW}Install Libsodium...${NC}"
  128. echo " "
  129. check_root
  130. install_libsodium
  131. echo " "
  132. exit
  133. fi
  134. fi
  135. # Output checking environment information
  136. echo " "
  137. echo -e " ${YELLOW}Checking Environment...${NC}"
  138. echo " "
  139. # Check the bin path of php
  140. php_path=""
  141. if [ $# -eq 0 ]; then
  142. # Check the bin path of php-cli or php-fpm
  143. default_optional_php_paths="php php-fpm php5-fpm php-fpm7.0"
  144. enabled_php_paths=""
  145. for optional_php_path in $default_optional_php_paths
  146. do
  147. check_php "$optional_php_path"
  148. php_exists=$?
  149. if [ "$php_exists" == "0" ]; then
  150. optional_php_path=$(which "$optional_php_path")
  151. enabled_php_paths=$enabled_php_paths" $optional_php_path"
  152. fi
  153. done
  154. if [ "$enabled_php_paths" == "" ]; then
  155. echo ""
  156. echo -e " ${RED}Error:${NC}"
  157. echo " No found php or php-fpm command, please run this script with the path of php"
  158. echo ""
  159. exit 1;
  160. else
  161. pathCount=$(echo "$enabled_php_paths" | awk '{print NF}')
  162. echo " Please select the php path to install loader extension:"
  163. echo ""
  164. indexCount=1
  165. for enabled_php_path in $enabled_php_paths
  166. do
  167. echo " $indexCount : $enabled_php_path"
  168. indexCount=$(($indexCount + 1))
  169. done
  170. echo ""
  171. echo " 0 : Quit this script"
  172. # Choose the bin path to install loader extension
  173. while true; do
  174. preg_str="^[0-$pathCount]$"
  175. echo ""
  176. read -p " Enter a number > ";
  177. if [[ $REPLY =~ $preg_str ]]; then
  178. if [[ $REPLY == 0 ]]; then
  179. echo ""
  180. echo " Exit";
  181. exit 1;
  182. fi
  183. indexCount=1
  184. for enabled_php_path in $enabled_php_paths
  185. do
  186. if [[ $indexCount == $REPLY ]]; then
  187. php_path=$enabled_php_path
  188. fi
  189. indexCount=$(($indexCount + 1))
  190. done
  191. break
  192. else
  193. echo ""
  194. echo " Invalid input";
  195. fi
  196. done
  197. fi
  198. elif [ $# -eq 1 ]; then
  199. php_path="$1"
  200. check_php "$1"
  201. php_exists=$?
  202. if [ "$php_exists" == "0" ]; then
  203. php_path=$(which "$php_path")
  204. else
  205. echo -e " ${RED}Error:${NC}"
  206. echo " Please check if $php_path is existing and executable"
  207. echo ""
  208. exit 1;
  209. fi
  210. else
  211. echo -e " ${RED}Error:${NC}"
  212. echo " Please check your parameters, this shell script supports only one optional parameter."
  213. echo ""
  214. exit 1;
  215. fi
  216. # Check if installed swoole loader
  217. loader_installed=$($php_path -m 2> /dev/null | awk '/swoole_loader/')
  218. if [ -n "$loader_installed" ]; then
  219. # check the version of installed swoole loader
  220. loader_version_installed=$($php_path -i 2> /dev/null | awk '/^swoole_loader version =>/ {print $4}')
  221. echo -e " ${YELLOW}Swoole Compiler Loader $loader_version_installed is installed now${NC}"
  222. echo ""
  223. yes_or_no " Reinstall swoole compiler loader?"
  224. yes_or_no_res=$?
  225. if [ "$yes_or_no_res" == "1" ]; then
  226. echo "";
  227. echo " Exit script";
  228. echo "";
  229. exit 1;
  230. else
  231. echo "";
  232. fi
  233. fi
  234. # Output php path information
  235. echo " php_path : $php_path"
  236. # Check php version
  237. raw_php_version=$($php_path -i 2> /dev/null | awk 'NR != 2 && /^PHP Version =>/ {print $4}')
  238. php_one_version=$(echo "$raw_php_version" | cut -d . -f 1)
  239. php_two_version=$(echo "$raw_php_version" | cut -d . -f 2)
  240. php_version=$php_one_version.$php_two_version
  241. echo " php_version : $php_version"
  242. # Check php sapi
  243. php_sapi=$($php_path -i 2> /dev/null | awk '/^Server API =>/ {print $4,$5,$6}')
  244. echo " php_sapi : $php_sapi"
  245. # Check php thread saftey
  246. php_thread_safety=$($php_path -i 2> /dev/null | awk '/^Thread Safety =>/ {print $4}')
  247. echo " php_thread_safety : $php_thread_safety"
  248. # Check php extension dir
  249. php_extension_dir=$($php_path -i 2> /dev/null | awk '/^extension_dir =>/ {print $3}')
  250. if [ -d "$php_extension_dir" ]; then
  251. echo " php_extension_dir : $php_extension_dir"
  252. else
  253. echo ""
  254. echo -e " ${RED}Error:${NC}"
  255. echo " No found php extension directory : $php_extension_dir"
  256. echo ""
  257. exit 1;
  258. fi
  259. # Check php extension dir
  260. php_ini_path=$($php_path -i 2> /dev/null | awk '/^Loaded Configuration File =>/ {print $5}')
  261. if [ -f "$php_ini_path" ]; then
  262. echo " php_ini_path : $php_ini_path"
  263. else
  264. echo ""
  265. echo -e " ${RED}Error:${NC}"
  266. echo " No found php.ini file: $php_ini_path"
  267. echo ""
  268. exit 1;
  269. fi
  270. # Check xdebug, ioncube loader and zend loader extensions
  271. forbidden_extensions_exists=$($php_path -m 2> /dev/null | awk '/xdebug|ionCube|zend_loader/')
  272. if [ -n "$forbidden_extensions_exists" ]; then
  273. echo ""
  274. echo -e " ${RED}Error:${NC}"
  275. echo " It exists xdebug or ionCuber loader or zend_loader extension, please remove these extensions"
  276. echo ""
  277. exit 1;
  278. fi
  279. # Ensure root privileges.
  280. check_root
  281. # Check libsodium.so which is required for swoole-compiler loader
  282. install_libsodium
  283. # Install swoole_loader.so to php extension directory
  284. swoole_loader_file="swoole_loader"
  285. swoole_loader_file="$swoole_loader_file""$php_one_version""$php_two_version"
  286. if [ "$php_thread_safety" == "disabled" ]; then
  287. swoole_loader_file="$swoole_loader_file"".so"
  288. else
  289. swoole_loader_file="$swoole_loader_file""_zts.so"
  290. fi
  291. real_swoole_loader_path=$php_extension_dir"/"$swoole_loader_file
  292. if [ ! -f "$real_swoole_loader_path" ]; then
  293. echo " swoole_loader.so : not found"
  294. else
  295. echo " swoole_loader.so : found"
  296. fi
  297. swoole_loader_path="./"$swoole_loader_file
  298. if [ -f "$swoole_loader_path" ]; then
  299. echo ""
  300. echo -e " ${YELLOW}installing $swoole_loader_file to php_extension_dir...${NC}"
  301. cp "$swoole_loader_path" "$php_extension_dir"
  302. echo ""
  303. else
  304. echo ""
  305. echo -e " ${RED}Error:${NC}"
  306. echo " No found right swoole loader file : $swoole_loader_file"
  307. echo ""
  308. exit 1;
  309. fi
  310. real_swoole_loader_path=$php_extension_dir"/"$swoole_loader_file
  311. if [ ! -f "$real_swoole_loader_path" ]; then
  312. echo ""
  313. echo -e " ${RED}Error:${NC}"
  314. echo " No found swoole loader file in php extension dir: $real_swoole_loader_path"
  315. echo ""
  316. exit 1;
  317. fi
  318. # Check if exists swoole_loader.so config
  319. raw_ini_loader="extension="$real_swoole_loader_path
  320. ini_loader="$raw_ini_loader"
  321. load_config_exists=$(awk '/swoole_loader/ {print $0}' "$php_ini_path")
  322. if [ -n "$load_config_exists" ]; then
  323. echo " loader_config : found"
  324. sed -i '/swoole_loader/d' "$php_ini_path"
  325. else
  326. echo " loader_config : not found"
  327. fi
  328. echo ""
  329. echo -e " ${YELLOW}adding swoole_loader extension config to php.ini...${NC}"
  330. echo "$ini_loader" >> "$php_ini_path"
  331. # Check if installed swoole loader
  332. loader_installed=$($php_path -m 2> /dev/null | awk '/swoole_loader/')
  333. if [ -n "$loader_installed" ]; then
  334. loader_version_installed=$($php_path -i 2> /dev/null | awk '/^swoole_loader version =>/ {print $4}')
  335. echo ""
  336. echo -e " ${YELLOW}Swoole Compiler Loader $loader_version_installed is installed successfully${NC}"
  337. exit 1;
  338. else
  339. echo ""
  340. echo -e " ${RED}Error:${NC}"
  341. echo " Swoole Compiler Loader failed to install"
  342. echo ""
  343. exit 1;
  344. fi