php-8.4.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #!/bin/bash
  2. Install_PHP84() {
  3. # Check CentOS version
  4. if [ -f /etc/redhat-release ]; then
  5. OS_VERSION=$(grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release | cut -d'.' -f1)
  6. if [ "${OS_VERSION}" -lt "8" ]; then
  7. echo "${CFAILURE}Error: PHP 8.4 cannot be installed on CentOS ${OS_VERSION}. Minimum required version is CentOS 8.${CEND}"
  8. kill -9 $$; exit 1;
  9. fi
  10. fi
  11. pushd ${oneinstack_dir}/src > /dev/null
  12. if [ ! -e "${php_install_dir}/bin/phpize" ]; then
  13. PHP_version=8.4.4
  14. PHP_main_ver=84
  15. # 下载和校验
  16. src_url=https://www.php.net/distributions/php-${PHP_version}.tar.gz && Download_src
  17. # 编译安装
  18. tar xzf php-${PHP_version}.tar.gz
  19. pushd php-${PHP_version} > /dev/null
  20. make clean
  21. [ ! -d "${php_install_dir}" ] && mkdir -p ${php_install_dir}
  22. # PHP 8.4 特定的编译选项
  23. ./configure --prefix=${php_install_dir} \
  24. --with-config-file-path=${php_install_dir}/etc \
  25. --with-config-file-scan-dir=${php_install_dir}/etc/php.d \
  26. --with-fpm-user=${run_user} \
  27. --with-fpm-group=${run_user} \
  28. --enable-mysqlnd \
  29. --with-mysqli=mysqlnd \
  30. --with-pdo-mysql=mysqlnd \
  31. --with-sqlite3 \
  32. --with-pdo-sqlite \
  33. --with-openssl \
  34. --with-zlib \
  35. --with-zip \
  36. --with-curl \
  37. --with-iconv \
  38. --with-gettext \
  39. --with-readline \
  40. --with-ldapsasl \
  41. --with-sodium \
  42. --enable-bcmath \
  43. --enable-fpm \
  44. --enable-xml \
  45. --enable-sysvsem \
  46. --enable-sysvshm \
  47. --enable-sysvmsg \
  48. --enable-shmop \
  49. --enable-sockets \
  50. --enable-mbstring \
  51. --enable-pcntl \
  52. --enable-soap \
  53. --enable-gd \
  54. --enable-intl \
  55. --enable-opcache \
  56. --enable-ftp \
  57. --enable-exif \
  58. --enable-calendar \
  59. --without-pear \
  60. --disable-phar \
  61. --disable-rpath
  62. make -j ${THREAD} && make install
  63. if [ -e "${php_install_dir}/bin/phpize" ]; then
  64. # 配置文件
  65. mkdir -p ${php_install_dir}/etc/php.d
  66. \cp php.ini-production ${php_install_dir}/etc/php.ini
  67. # 设置php.ini配置
  68. tee -a ${php_install_dir}/etc/php.ini <<EOF
  69. ; 内存限制
  70. memory_limit = 256M
  71. ; 输出缓冲
  72. output_buffering = On
  73. ; 输出缓冲
  74. output_buffering =
  75. ; 短标签
  76. short_open_tag = On
  77. ; 暴露php出错信息
  78. expose_php = Off
  79. ; 请求顺序
  80. request_order = "CGP"
  81. ; 时区
  82. date.timezone = ${timezone}
  83. ; 最大上传大小
  84. post_max_size = 100M
  85. ; 最大上传大小
  86. upload_max_filesize = 50M
  87. ; 最大执行时间
  88. max_execution_time = 600
  89. ; 输出缓冲
  90. realpath_cache_size = 2M
  91. ; 禁用函数
  92. disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen
  93. EOF
  94. if [ -e /usr/sbin/sendmail ]; then
  95. tee -a ${php_install_dir}/etc/php.ini <<EOF
  96. ; 发送邮件
  97. sendmail_path = /usr/sbin/sendmail -t -i
  98. EOF
  99. fi
  100. # PHP-FPM配置
  101. \cp sapi/fpm/php-fpm.conf.in ${php_install_dir}/etc/php-fpm.conf
  102. \cp sapi/fpm/www.conf.in ${php_install_dir}/etc/php-fpm.d/www.conf
  103. #sed -i "s@^;pid = run/php-fpm.pid@pid = run/php-fpm.pid@" ${php_install_dir}/etc/php-fpm.conf
  104. # 设置php-fpm.conf配置
  105. sed -i "s@^include=@;include=@" ${php_install_dir}/etc/php-fpm.conf
  106. tee -a ${php_install_dir}/etc/php-fpm.conf <<EOF
  107. ; 进程ID
  108. pid = run/php-fpm.pid
  109. ; 错误日志
  110. error_log = log/php-fpm.log
  111. ; 日志级别
  112. log_level = warning
  113. ; 紧急重启阈值
  114. emergency_restart_threshold = 30
  115. ; 紧急重启间隔
  116. emergency_restart_interval = 60s
  117. ; 进程控制超时
  118. process_control_timeout = 5s
  119. ; 守护进程
  120. daemonize = yes
  121. ; 加载扩展配置
  122. include=${php_install_dir}/etc/php-fpm.d/*.conf
  123. EOF
  124. # 设置php-fpm.d/www.conf配置
  125. tee -a ${php_install_dir}/etc/php-fpm.d/www.conf <<EOF
  126. [${run_user}]
  127. ; 监听
  128. listen = /dev/shm/php-cgi.sock
  129. ; 监听队列
  130. listen.backlog = -1
  131. ; 允许客户端
  132. listen.allowed_clients = 127.0.0.1
  133. ; 监听用户
  134. listen.owner = ${run_user}
  135. ; 监听组
  136. listen.group = ${run_group}
  137. ; 监听模式
  138. listen.mode = 0666
  139. ; 用户
  140. user = ${run_user}
  141. ; 组
  142. group = ${run_group}
  143. ; 进程管理
  144. pm = dynamic
  145. ; 最大进程数
  146. pm.max_children = 12
  147. ; 启动进程数
  148. pm.start_servers = 8
  149. ; 最小空闲进程数
  150. pm.min_spare_servers = 6
  151. ; 最大空闲进程数
  152. pm.max_spare_servers = 12
  153. ; 最大请求数
  154. pm.max_requests = 2048
  155. ; 进程空闲超时
  156. pm.process_idle_timeout = 10s
  157. ; 请求超时
  158. request_terminate_timeout = 120
  159. ; 慢日志超时
  160. request_slowlog_timeout = 0
  161. ; 状态路径
  162. pm.status_path = /php-fpm_status
  163. ; 慢日志
  164. slowlog = var/log/slow.log
  165. ; 文件限制
  166. rlimit_files = 51200
  167. ; 核心限制
  168. rlimit_core = 0
  169. ; 捕获工作者输出
  170. catch_workers_output = yes
  171. ; 环境变量
  172. env[HOSTNAME] = $HOSTNAME
  173. env[PATH] = /usr/local/bin:/usr/bin:/bin
  174. env[TMP] = /tmp
  175. env[TMPDIR] = /tmp
  176. env[TEMP] = /tmp
  177. EOF
  178. # 设置php-fpm内存优化
  179. if [ $Mem -gt 8500 ]; then
  180. tee -a ${php_install_dir}/etc/php-fpm.conf <<EOF
  181. ; 最大进程数
  182. pm.max_children = 80
  183. ; 启动进程数
  184. pm.start_servers = 60
  185. ; 最小空闲进程数
  186. pm.min_spare_servers = 50
  187. ; 最大空闲进程数
  188. pm.max_spare_servers = 80
  189. EOF
  190. elif [ $Mem -gt 6500 ]; then
  191. tee -a ${php_install_dir}/etc/php-fpm.conf <<EOF
  192. ; 最大进程数
  193. pm.max_children = 70
  194. ; 启动进程数
  195. pm.start_servers = 50
  196. ; 最小空闲进程数
  197. pm.min_spare_servers = 40
  198. ; 最大空闲进程数
  199. pm.max_spare_servers = 70
  200. EOF
  201. elif [ $Mem -gt 4500 ]; then
  202. tee -a ${php_install_dir}/etc/php-fpm.conf <<EOF
  203. ; 最大进程数
  204. pm.max_children = 60
  205. ; 启动进程数
  206. pm.start_servers = 40
  207. ; 最小空闲进程数
  208. pm.min_spare_servers = 30
  209. ; 最大空闲进程数
  210. pm.max_spare_servers = 60
  211. EOF
  212. elif [ $Mem -gt 3000 ]; then
  213. tee -a ${php_install_dir}/etc/php-fpm.conf <<EOF
  214. ; 最大进程数
  215. pm.max_children = 50
  216. ; 启动进程数
  217. pm.start_servers = 30
  218. ; 最小空闲进程数
  219. pm.min_spare_servers = 20
  220. ; 最大空闲进程数
  221. pm.max_spare_servers = 50
  222. EOF
  223. else
  224. tee -a ${php_install_dir}/etc/php-fpm.conf <<EOF
  225. ; 最大进程数
  226. pm.max_children = $(($Mem/3/20))
  227. ; 启动进程数
  228. pm.start_servers = $(($Mem/3/30))
  229. ; 最小空闲进程数
  230. pm.min_spare_servers = $(($Mem/3/40))
  231. ; 最大空闲进程数
  232. pm.max_spare_servers = $(($Mem/3/20))
  233. EOF
  234. fi
  235. # 启动脚本
  236. \cp ${oneinstack_dir}/init.d/php-fpm.service /lib/systemd/system/
  237. sed -i "s@/usr/local/php@${php_install_dir}@g" /lib/systemd/system/php-fpm.service
  238. systemctl enable php-fpm
  239. # 环境变量
  240. echo "export PATH=${php_install_dir}/bin:\$PATH" > /etc/profile.d/php.sh
  241. echo "${CSUCCESS}PHP ${PHP_version} installed successfully! ${CEND}"
  242. rm -rf php-${PHP_version}
  243. else
  244. rm -rf ${php_install_dir}
  245. echo "${CFAILURE}PHP ${PHP_version} install failed, Please Contact the author! ${CEND}"
  246. kill -9 $$; exit 1;
  247. fi
  248. popd > /dev/null
  249. fi
  250. popd > /dev/null
  251. }