#!/bin/bash
WORKDIR=/tmp/
PHP73_DIR=/usr/local/php73
DEFAULT_SWAP=0

createSwap(){
  if [ `cat /proc/meminfo | grep SwapTotal | awk -F " " '{print $2}'` -ne 0 ]
  then
    return 0
  fi
  if [ `cat /proc/meminfo | grep MemTotal | awk -F " " '{print $2}'` -le 1048576 ]
  then
    echo "Mem lower than 1GB,creating swap..."
    dd if=/dev/zero of=/swap bs=1M count=2048
    mkswap -f /swap
    swapon /swap && echo "SWAP create success."
    DEFAULT_SWAP=1
  fi
}

installDependence(){
  yum install -y libxml2-devel openssl-devel  curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel xslt libxslt-devel
  yum update -y curl curl-devel
  yum remove -y libzip
}

compileLibzip(){
  echo "start install libzip."
  cd ${WORKDIR}
  if [ -f libzip-1.2.0.tar.gz ]
  then
    rm -rf libzip-1.2.0.tar.gz
  fi
  wget https://code.aliyun.com/yh11/download/raw/master/libzip-1.2.0.tar.gz
  tar -zxvf libzip-1.2.0.tar.gz
  cd libzip-1.2.0
  ./configure
  make && make install
  if [ $? -ne 0 ]
  then
    echo "libzip install failed."
    exit 127
  fi
  ln -s /usr/local/lib/libzip/include/zipconf.h /usr/local/include
}

installPHP(){
  echo "Install PHP 7.3"
  cd ${WORKDIR}
  if [ -f php-7.3.9.tar.gz ]
  then
    rm -rf php-7.3.9.tar.gz
  fi
  wget https://code.aliyun.com/yh11/download/raw/master/php-7.3.9.tar.gz
  tar -xvf php-7.3.9.tar.gz
  cd php-7.3.9
  ./configure --prefix=/usr/local/php73  --enable-soap --enable-cgi --with-mysql=/usr/local/mysql --with-mysqli=mysqlnd --with-gd --with-pdo-mysql=mysqlnd --with-zlib --enable-zip --enable-fpm --without-pear --disable-phar --with-openssl --enable-mbstring=all --with-jpeg-dir=/usr --with-png-dir=/usr --with-curl --with-freetype-dir=/usr --enable-gd-native-ttf --with-xsl=/usr --enable-calendar --enable-exif --enable-ftp --with-iconv --enable-bcmath --with-mcrypt=/usr/local/libmcrypt --enable-opcache
  make && make install
  if [ $? -ne 0 ]
  then
    echo "PHP install failed."
    exit 127
  fi
  cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm73
  cp ${PHP73_DIR}/etc/php-fpm.conf.default ${PHP73_DIR}/etc/php-fpm.conf
  chmod +x /etc/init.d/php-fpm73
}

createConfig(){

cat << EOF > ${PHP73_DIR}/etc/php-fpm.d/www.conf
[www]
listen = /home/www/logs/php73-fpm.sock
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 128
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 300
rlimit_files = 1024
slowlog = /home/www/logs/php73-fpm-slow.log
EOF

}

modifyApache(){
  sed -i 's#ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/home/www/logs/php-fpm.sock|fcgi://127.0.0.1/home/www/htdocs#ProxyPassMatch ^/(.*\\.php(/.*)?)$ unix:/home/www/logs/php73-fpm.sock|fcgi://127.0.0.1/home/www/htdocs#g' /usr/local/apache/conf/httpd.conf
}

restartService(){
  chkconfig php-fpm off
  chkconfig php-fpm73 on
  /etc/init.d/php-fpm stop
  /etc/init.d/php-fpm73 start
  /etc/init.d/apachectl restart
}

delSwap(){
  if [ ${DEFAULT_SWAP} -eq 1 ]
  then
    swapoff /swap
    rm -rf /swap
  fi
}

createSwap
installDependence
compileLibzip
installPHP
createConfig
modifyApache
restartService
delSwap