#!/bin/sh
# AUTH:xiongchuan
# EMAIL:xiongchuan@bcpmdata.com 
# DATE:2023-04-25

phpUsername="www"             # user which run the php-fpm script
phpGroupname="www"            # group which run the php-fpm script
phpLogdir="/data/logs/php"    # php-fpm log dir
composerUser="www"            # who run composer


#check if php-fpm installed
if_fpm=$(rpm -qa | grep php-fpm | wc -l)
if [ ${if_fpm} -ne 0 ]
    then
      echo "php-fpm have installed, Please check out"
      exit
fi

#php-fpm user check
if_exit_user=$(grep ${phpUsername} /etc/passwd | grep -v apache |wc -l)
if [ ${if_exit_user} -eq 0 ];then
    echo "user ${phpUsername} not fount"
    exit
fi
if_exit_group=$(grep ${phpGroupname} /etc/group | grep -v apache |wc -l)
if [ ${if_exit_group} -eq 0 ];then
    echo "group ${phpGroupname} not fount"
    exit
fi


# install php-fpm 
yum install epel-release -y >/dev/null 
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

echo "php-fpm is installing ......."
#yum --enablerepo=remi-php72 install php php-fpm php-opcache php-redis php-mbstring php-mcrypt php-geoip php-gd php-mongodb php-openssl php-xml php-process php-pdo php-posix php-zip php-pear php-swoole php-pgsql php-bcmath  -y >/dev/null
yum --enablerepo=remi-php74 install  -y php-cli php-bcmath php-fpm php-gd php-mbstring php-opcache php-pdo_mysql php-pdo_pgsql php-pecl-mongodb php-process php-redis php-xml php-zip php-xlswriter php-sodium php-pecl-grpc php-pecl-protobuf php-xdebug > /dev/null 
if [ $? -ne 0 ]
    then  
     echo "php-fpm install failed, Please check out!"
     exit
fi

# 优化中
echo 'php-fpm config .....'
      /usr/bin/sed -i 's/^short_open_tag.*$/short_open_tag = On/' /etc/php.ini
      /usr/bin/sed -i 's/^upload_max_filesize.*$/upload_max_filesize = 512M/' /etc/php.ini
      /usr/bin/sed -i 's/^post_max_size.*$/post_max_size = 512M/' /etc/php.ini
      /usr/bin/sed -i 's/^error_reporting.*$/error_reporting = E_ALL/' /etc/php.ini
      /usr/bin/sed -i 's/^display_errors.*$/display_errors = On/' /etc/php.ini
      /usr/bin/sed -i 's/^log_errors =.*$/log_errors = On/' /etc/php.ini
      is_timezone=$(/usr/bin/sed -n '/^date.timezone.*$/p'  /etc/php.ini  | wc -l)
      if [ $is_timezone -eq 0 ]
        then
          /usr/bin/sed -i '/;date.timezone/a\date.timezone = Asia\/Shanghai' /etc/php.ini
        else
          /usr/bin/sed -i 's/^date.timezone.*$/date.timezone = Asia\/Shanghai/' /etc/php.ini
      fi

cat >>/etc/php.ini << EOF
[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=1024
opcache.optimization_level=1
opcache.force_restart_timeout=3600
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.fast_shutdown=1
opcache.save_comments=0
opcache.validate_timestamps=1
opcache.revalidate_freq=60  //默认每隔2s会验证改变的文件并会更新缓存
EOF


      /usr/bin/sed -i "s|^error_log = /var/log/php-fpm/error.log|error_log = ${phpLogdir}/error.log|" /etc/php-fpm.conf
      #php-fpm配置
      echo  'php-fpm相关参数配置中......'
      #pm.max_requests 处理多少任务后退出防止内存泄漏,request_terminate_timeout 超时后杀进程，防进程卡死
      /usr/bin/sed -i 's/^pm.max_children =.*$/pm.max_children = 1024/' /etc/php-fpm.d/www.conf
      /usr/bin/sed -i 's/^pm.start_servers.*$/pm.start_servers = 50/' /etc/php-fpm.d/www.conf
      /usr/bin/sed -i 's/^pm.min_spare_servers.*$/pm.min_spare_servers = 50/' /etc/php-fpm.d/www.conf
      /usr/bin/sed -i 's/^pm.max_spare_servers.*$/pm.max_spare_servers = 150/' /etc/php-fpm.d/www.conf
      /usr/bin/sed -i 's/^;pm.max_requests.*$/pm.max_requests = 1024/' /etc/php-fpm.d/www.conf
      /usr/bin/sed -i 's/^;request_terminate_timeout.*$/request_terminate_timeout = 60/' /etc/php-fpm.d/www.conf
      /usr/bin/sed -i 's/^;request_slowlog_timeout.*$/request_slowlog_timeout = 10/' /etc/php-fpm.d/www.conf
      /usr/bin/sed -i "s/var\/log\/php-fpm\/www-error.log/${phpLogdir}/www-error.log/g" /etc/php-fpm.d/www.conf
      /usr/bin/sed -i "s|^;slowlog = /var/log/php-fpm/www-slow.log|slowlog = ${phpLogdir}/www-slow.log|" /etc/php-fpm.d/www.conf
      /usr/bin/sed -i "s/^user = apache/user = ${phpUsername}/" /etc/php-fpm.d/www.conf
      /usr/bin/sed -i "s/^group = apache/group = ${phpGroupname}/" /etc/php-fpm.d/www.conf
      /usr/bin/sed -i 's/^;rlimit_files = 1024/rlimit_files = 65535/'  /etc/php-fpm.d/www.conf
      #防内存泄漏
      is_requests=$(/usr/bin/sed -ne '/pm.max_requests.*$/p' /etc/php-fpm.d/www.conf |wc -l)
      is_timeout=$(/usr/bin/sed -ne '/request_terminate_timeout.*$/p' /etc/php-fpm.d/www.conf |wc -l)
      if [ $is_requests -eq 0 ]
        then
          sed -i '/;pm.max_requests = 500/a\pm.max_requests = 1024' /etc/php-fpm.d/www.conf
        else
          sed -i 's/^pm.max_requests.*$/pm.max_requests = 1024/' /etc/php-fpm.d/www.conf
      fi
      if [ $is_timeout -eq 0 ]
        then
          sed -i '/;request_terminate_timeout = 0/a\request_terminate_timeout = 60' /etc/php-fpm.d/www.conf
        else
          sed -i 's/^request_terminate_timeout.*$/request_terminate_timeout = 60/' /etc/php-fpm.d/www.conf
      fi


      mkdir -p  ${phpLogdir}
      chown ${phpUsername}:${phpGroupname} ${phpLogdir} -R
      chown ${phpUsername}:${phpGroupname} /var/lib/php/ -R
      chown ${phpUsername}:${phpGroupname} /var/tmp/systemd*php-fpm.service* -R

      echo "php正在启动，请稍等........"
      /bin/systemctl start php-fpm
      if [ "$?"  -eq 0 ]
        then
          echo  "php启动......"
          echo 'php安装完成，并启动服务'
        else
          echo "php启动失败......"
          echo 'php安装完成,服务启动故障，请排除问题'
          exit
      fi
      echo '设置php服务为开机启动模式'
      systemctl enable php-fpm.service 2>/dev/null
      echo '安装composer'
      curl -o /usr/bin/composer https://mirrors.aliyun.com/composer/composer.phar
      chmod +x /usr/bin/composer
      su - ${composerUser} -c "composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/"
      composer -V || echo "composer install failed, Please checkout"
   ##log cut 
   # nginx log cut
    echo 'config php-fpm cut log.....'
    rm -rf /etc/logrotate.d/php-fpm
    cat >> /etc/logrotate.d/php-fpm  <<EOF
${phpLogdir}/*log {
    missingok
    rotate 7
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /bin/kill -SIGUSR1 \`cat /run/php-fpm/php-fpm.pid 2>/dev/null\` 2>/dev/null || true
    endscript
}

EOF
echo  "php-fpm相关参数配置完成!"
