php-7.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # Blog: http://blog.linuxeye.com
  4. Install_PHP-7()
  5. {
  6. cd $oneinstack_dir/src
  7. . ../functions/download.sh
  8. . ../functions/check_os.sh
  9. . ../options.conf
  10. src_url=http://ftp.gnu.org/pub/gnu/libiconv/libiconv-$libiconv_version.tar.gz && Download_src
  11. src_url=http://downloads.sourceforge.net/project/mcrypt/Libmcrypt/$libmcrypt_version/libmcrypt-$libmcrypt_version.tar.gz && Download_src
  12. src_url=http://downloads.sourceforge.net/project/mhash/mhash/$mhash_version/mhash-$mhash_version.tar.gz && Download_src
  13. src_url=http://downloads.sourceforge.net/project/mcrypt/MCrypt/$mcrypt_version/mcrypt-$mcrypt_version.tar.gz && Download_src
  14. src_url=https://downloads.php.net/~ab/php-$php_7_version.tar.gz && Download_src
  15. tar xzf libiconv-$libiconv_version.tar.gz
  16. cd libiconv-$libiconv_version
  17. ./configure --prefix=/usr/local
  18. [ -n "`cat /etc/issue | grep 'Ubuntu 13'`" ] && sed -i 's@_GL_WARN_ON_USE (gets@//_GL_WARN_ON_USE (gets@' srclib/stdio.h
  19. [ -n "`cat /etc/issue | grep 'Ubuntu 14'`" ] && sed -i 's@gets is a security@@' srclib/stdio.h
  20. make && make install
  21. cd ../
  22. /bin/rm -rf libiconv-$libiconv_version
  23. tar xzf libmcrypt-$libmcrypt_version.tar.gz
  24. cd libmcrypt-$libmcrypt_version
  25. ./configure
  26. make && make install
  27. ldconfig
  28. cd libltdl/
  29. ./configure --enable-ltdl-install
  30. make && make install
  31. cd ../../
  32. /bin/rm -rf libmcrypt-$libmcrypt_version
  33. tar xzf mhash-$mhash_version.tar.gz
  34. cd mhash-$mhash_version
  35. ./configure
  36. make && make install
  37. cd ../
  38. /bin/rm -rf mhash-$mhash_version
  39. echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
  40. ldconfig
  41. OS_CentOS='ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config \n
  42. if [ `getconf WORD_BIT` == 32 ] && [ `getconf LONG_BIT` == 64 ];then \n
  43. ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1 \n
  44. else \n
  45. ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1 \n
  46. fi'
  47. OS_command
  48. tar xzf mcrypt-$mcrypt_version.tar.gz
  49. cd mcrypt-$mcrypt_version
  50. ldconfig
  51. ./configure
  52. make && make install
  53. cd ../
  54. /bin/rm -rf mcrypt-$mcrypt_version
  55. tar xzf php-$php_7_version.tar.gz
  56. id -u $run_user >/dev/null 2>&1
  57. [ $? -ne 0 ] && useradd -M -s /sbin/nologin $run_user
  58. cd php-$php_7_version
  59. make clean
  60. ./buildconf
  61. [ ! -d "$php_install_dir" ] && mkdir -p $php_install_dir
  62. [ "$PHP_cache" == '1' ] && PHP_cache_tmp='--enable-opcache' || PHP_cache_tmp='--disable-opcache'
  63. if [ "$Apache_version" == '1' -o "$Apache_version" == '2' ];then
  64. CFLAGS= CXXFLAGS= ./configure --prefix=$php_install_dir --with-config-file-path=$php_install_dir/etc \
  65. --with-apxs2=$apache_install_dir/bin/apxs $PHP_cache_tmp --disable-fileinfo \
  66. --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
  67. --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib \
  68. --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-exif \
  69. --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-inline-optimization \
  70. --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl \
  71. --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-ftp \
  72. --with-gettext --enable-zip --enable-soap --disable-ipv6 --disable-debug
  73. else
  74. CFLAGS= CXXFLAGS= ./configure --prefix=$php_install_dir --with-config-file-path=$php_install_dir/etc \
  75. --with-fpm-user=$run_user --with-fpm-group=$run_user --enable-fpm $PHP_cache_tmp --disable-fileinfo \
  76. --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
  77. --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib \
  78. --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-exif \
  79. --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-inline-optimization \
  80. --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl \
  81. --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-ftp \
  82. --with-gettext --enable-zip --enable-soap --disable-ipv6 --disable-debug
  83. fi
  84. make ZEND_EXTRA_LIBS='-liconv'
  85. make install
  86. if [ -d "$php_install_dir/bin" ];then
  87. echo -e "\033[32mPHP install successfully! \033[0m"
  88. else
  89. rm -rf $php_install_dir
  90. echo -e "\033[31mPHP install failed, Please Contact the author! \033[0m"
  91. kill -9 $$
  92. fi
  93. [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=$php_install_dir/bin:\$PATH" >> /etc/profile
  94. [ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep $php_install_dir /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=$php_install_dir/bin:\1@" /etc/profile
  95. . /etc/profile
  96. # wget -c http://pear.php.net/go-pear.phar
  97. # $php_install_dir/bin/php go-pear.phar
  98. /bin/cp php.ini-production $php_install_dir/etc/php.ini
  99. # Modify php.ini
  100. Mem=`free -m | awk '/Mem:/{print $2}'`
  101. if [ $Mem -gt 1024 -a $Mem -le 1500 ];then
  102. Memory_limit=192
  103. elif [ $Mem -gt 1500 -a $Mem -le 3500 ];then
  104. Memory_limit=256
  105. elif [ $Mem -gt 3500 -a $Mem -le 4500 ];then
  106. Memory_limit=320
  107. elif [ $Mem -gt 4500 ];then
  108. Memory_limit=448
  109. else
  110. Memory_limit=128
  111. fi
  112. sed -i "s@^memory_limit.*@memory_limit = ${Memory_limit}M@" $php_install_dir/etc/php.ini
  113. sed -i 's@^output_buffering =@output_buffering = On\noutput_buffering =@' $php_install_dir/etc/php.ini
  114. sed -i 's@^;cgi.fix_pathinfo.*@cgi.fix_pathinfo=0@' $php_install_dir/etc/php.ini
  115. sed -i 's@^short_open_tag = Off@short_open_tag = On@' $php_install_dir/etc/php.ini
  116. sed -i 's@^expose_php = On@expose_php = Off@' $php_install_dir/etc/php.ini
  117. sed -i 's@^request_order.*@request_order = "CGP"@' $php_install_dir/etc/php.ini
  118. sed -i 's@^;date.timezone.*@date.timezone = Asia/Shanghai@' $php_install_dir/etc/php.ini
  119. sed -i 's@^post_max_size.*@post_max_size = 50M@' $php_install_dir/etc/php.ini
  120. sed -i 's@^upload_max_filesize.*@upload_max_filesize = 50M@' $php_install_dir/etc/php.ini
  121. sed -i 's@^;upload_tmp_dir.*@upload_tmp_dir = /tmp@' $php_install_dir/etc/php.ini
  122. sed -i 's@^max_execution_time.*@max_execution_time = 600@' $php_install_dir/etc/php.ini
  123. sed -i 's@^;realpath_cache_size.*@realpath_cache_size = 2M@' $php_install_dir/etc/php.ini
  124. sed -i 's@^disable_functions.*@disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen@' $php_install_dir/etc/php.ini
  125. sed -i 's@^session.cookie_httponly.*@session.cookie_httponly = 1@' $php_install_dir/etc/php.ini
  126. sed -i 's@^mysqlnd.collect_memory_statistics.*@mysqlnd.collect_memory_statistics = On@' $php_install_dir/etc/php.ini
  127. [ -e /usr/sbin/sendmail ] && sed -i 's@^;sendmail_path.*@sendmail_path = /usr/sbin/sendmail -t -i@' $php_install_dir/etc/php.ini
  128. if [ "$PHP_cache" == '1' ];then
  129. sed -i 's@^\[opcache\]@[opcache]\nzend_extension=opcache.so@' $php_install_dir/etc/php.ini
  130. sed -i 's@^;opcache.enable=.*@opcache.enable=1@' $php_install_dir/etc/php.ini
  131. sed -i "s@^;opcache.memory_consumption.*@opcache.memory_consumption=$Memory_limit@" $php_install_dir/etc/php.ini
  132. sed -i 's@^;opcache.interned_strings_buffer.*@opcache.interned_strings_buffer=8@' $php_install_dir/etc/php.ini
  133. sed -i 's@^;opcache.max_accelerated_files.*@opcache.max_accelerated_files=4000@' $php_install_dir/etc/php.ini
  134. sed -i 's@^;opcache.revalidate_freq.*@opcache.revalidate_freq=60@' $php_install_dir/etc/php.ini
  135. sed -i 's@^;opcache.save_comments.*@opcache.save_comments=0@' $php_install_dir/etc/php.ini
  136. sed -i 's@^;opcache.fast_shutdown.*@opcache.fast_shutdown=1@' $php_install_dir/etc/php.ini
  137. sed -i 's@^;opcache.validate_timestamps.*@opcache.validate_timestamps=1@' $php_install_dir/etc/php.ini
  138. sed -i 's@^;opcache.enable_cli.*@opcache.enable_cli=1@' $php_install_dir/etc/php.ini
  139. sed -i 's@^;opcache.use_cwd.*@opcache.use_cwd=1@' $php_install_dir/etc/php.ini
  140. sed -i 's@^opcache.max_accelerated_files.*@opcache.max_accelerated_files=100000@' $php_install_dir/etc/php.ini
  141. sed -i 's@^;opcache.max_wasted_percentage.*@opcache.max_wasted_percentage=5@' $php_install_dir/etc/php.ini
  142. sed -i 's@^;opcache.consistency_checks.*@opcache.consistency_checks=0@' $php_install_dir/etc/php.ini
  143. sed -i 's@^;opcache.optimization_level.*@;opcache.optimization_level=0@' $php_install_dir/etc/php.ini
  144. fi
  145. if [ "$Apache_version" != '1' -a "$Apache_version" != '2' ];then
  146. # php-fpm Init Script
  147. /bin/cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  148. chmod +x /etc/init.d/php-fpm
  149. OS_CentOS='chkconfig --add php-fpm \n
  150. chkconfig php-fpm on'
  151. OS_Debian_Ubuntu='update-rc.d php-fpm defaults'
  152. OS_command
  153. cat > $php_install_dir/etc/php-fpm.conf <<EOF
  154. ;;;;;;;;;;;;;;;;;;;;;
  155. ; FPM Configuration ;
  156. ;;;;;;;;;;;;;;;;;;;;;
  157. ;;;;;;;;;;;;;;;;;;
  158. ; Global Options ;
  159. ;;;;;;;;;;;;;;;;;;
  160. [global]
  161. pid = run/php-fpm.pid
  162. error_log = log/php-fpm.log
  163. log_level = warning
  164. emergency_restart_threshold = 30
  165. emergency_restart_interval = 60s
  166. process_control_timeout = 5s
  167. daemonize = yes
  168. ;;;;;;;;;;;;;;;;;;;;
  169. ; Pool Definitions ;
  170. ;;;;;;;;;;;;;;;;;;;;
  171. [$run_user]
  172. listen = /dev/shm/php-cgi.sock
  173. listen.backlog = -1
  174. listen.allowed_clients = 127.0.0.1
  175. listen.owner = $run_user
  176. listen.group = $run_user
  177. listen.mode = 0666
  178. user = $run_user
  179. group = $run_user
  180. pm = dynamic
  181. pm.max_children = 12
  182. pm.start_servers = 8
  183. pm.min_spare_servers = 6
  184. pm.max_spare_servers = 12
  185. pm.max_requests = 2048
  186. pm.process_idle_timeout = 10s
  187. request_terminate_timeout = 120
  188. request_slowlog_timeout = 0
  189. slowlog = log/slow.log
  190. rlimit_files = 51200
  191. rlimit_core = 0
  192. catch_workers_output = yes
  193. ;env[HOSTNAME] = $HOSTNAME
  194. env[PATH] = /usr/local/bin:/usr/bin:/bin
  195. env[TMP] = /tmp
  196. env[TMPDIR] = /tmp
  197. env[TEMP] = /tmp
  198. EOF
  199. [ -d "/run/shm" -a ! -e "/dev/shm" ] && sed -i 's@/dev/shm@/run/shm@' $php_install_dir/etc/php-fpm.conf $oneinstack_dir/vhost.sh $oneinstack_dir/conf/nginx.conf
  200. if [ $Mem -le 3000 ];then
  201. sed -i "s@^pm.max_children.*@pm.max_children = $(($Mem/2/20))@" $php_install_dir/etc/php-fpm.conf
  202. sed -i "s@^pm.start_servers.*@pm.start_servers = $(($Mem/2/30))@" $php_install_dir/etc/php-fpm.conf
  203. sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = $(($Mem/2/40))@" $php_install_dir/etc/php-fpm.conf
  204. sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = $(($Mem/2/20))@" $php_install_dir/etc/php-fpm.conf
  205. elif [ $Mem -gt 3000 -a $Mem -le 4500 ];then
  206. sed -i "s@^pm.max_children.*@pm.max_children = 80@" $php_install_dir/etc/php-fpm.conf
  207. sed -i "s@^pm.start_servers.*@pm.start_servers = 50@" $php_install_dir/etc/php-fpm.conf
  208. sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 40@" $php_install_dir/etc/php-fpm.conf
  209. sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 80@" $php_install_dir/etc/php-fpm.conf
  210. elif [ $Mem -gt 4500 -a $Mem -le 6500 ];then
  211. sed -i "s@^pm.max_children.*@pm.max_children = 90@" $php_install_dir/etc/php-fpm.conf
  212. sed -i "s@^pm.start_servers.*@pm.start_servers = 60@" $php_install_dir/etc/php-fpm.conf
  213. sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 50@" $php_install_dir/etc/php-fpm.conf
  214. sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 90@" $php_install_dir/etc/php-fpm.conf
  215. elif [ $Mem -gt 6500 -a $Mem -le 8500 ];then
  216. sed -i "s@^pm.max_children.*@pm.max_children = 100@" $php_install_dir/etc/php-fpm.conf
  217. sed -i "s@^pm.start_servers.*@pm.start_servers = 70@" $php_install_dir/etc/php-fpm.conf
  218. sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 60@" $php_install_dir/etc/php-fpm.conf
  219. sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 100@" $php_install_dir/etc/php-fpm.conf
  220. elif [ $Mem -gt 8500 ];then
  221. sed -i "s@^pm.max_children.*@pm.max_children = 120@" $php_install_dir/etc/php-fpm.conf
  222. sed -i "s@^pm.start_servers.*@pm.start_servers = 80@" $php_install_dir/etc/php-fpm.conf
  223. sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 70@" $php_install_dir/etc/php-fpm.conf
  224. sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 120@" $php_install_dir/etc/php-fpm.conf
  225. fi
  226. #[ "$Web_yn" == 'n' ] && sed -i "s@^listen =.*@listen = $local_IP:9000@" $php_install_dir/etc/php-fpm.conf
  227. service php-fpm start
  228. elif [ "$Apache_version" == '1' -o "$Apache_version" == '2' ];then
  229. service httpd restart
  230. fi
  231. cd ..
  232. [ -d "$php_install_dir" ] && /bin/rm -rf php-$php_7_version
  233. cd ..
  234. }