#!/bin/bash
#Author Mr.Wu
#PHP安装脚本
#2023-3-1 09:28:08
########################################
#定义变量
dep_pack="php-mcrypt libmcrypt libmcrypt-devel autoconf freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c bzip2-devel libmcrypt-devel freetype-devel libjpeg-devel sqlite-devel libwebp expat-devel"
dec_pack="--prefix=/usr/local/php8.1.9 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-gd --with-zlib --enable-sysvshm --enable-mbstring --with-freetype --with-expat --enable-xml --with-mhash --with-config-file-path=/etc --with-config-file-scan-dir=/usr/local/php8.1.9/etc/ --with-bz2 --with-jpeg"
dev_dir="/dev/null"
php_log="$(pwd)/php-install,log"
src_dir="/usr/local/src"
php_dir="/usr/local/php8.1.9"
aaa="/usr/local/php8.1.9/etc/php-fpm.d/www.conf"
messages=("检查PHP安装包是否存在" "下载PHP安装包" "安装依赖包" "安装oniguruma" "解压PHP安装包" "PHP预编译" "PHP编译" "PHP编译安装" "生成配置文件" "PHP启动管理")
colour=(31 34)
result=("失败!!!" "成功!!!")
########################################
#定义函数
delimiter(){#定义分隔符
echo -e "\033[32;1m※※※※※※※※※※※※※※※※※※※※※※※※※※\033[0m"
}
echoinfo(){#定义输入选项
echo -e "\033[35;1m#$1\033[0m"
}
echoresult(){#定义输出选项
echo -e "\033[$1;1m$2$3\033[0m"
}
if_test(){#if语句判断
if [ $? -eq 0 ];then
echoresult ${colour[1]} $1 ${result[1]}
else
echoresult ${colour[0]} $1 ${result[0]}
exit
fi
}
check_tar(){#检查PHP安装包是否存在
delimiter | tee -a $php_log
echoinfo ${messages[0]} | tee -a $php_log
if [ ! -f $nginx_tar ];then
echo -e "\033[31;1mPHP安装包不存在,请下载!!!\033[0m" | tee -a $php_log
get_tar
else
echo -e "\033[34;1mPHP安装包已经存在,请解压安装!!!\033[0m" | tee -a $php_log
fi
}
get_tar(){#下载PHP安装包
delimiter | tee -a $php_log
echoinfo ${messages[1]} | tee -a $php_log
wget https://www.php.net/distributions/php-8.1.9.tar.gz &> $dev_dir
sleep 3
if_test ${messages[1]} | tee -a $php_log
}
dep_tar(){#安装依赖包
delimiter | tee -a $php_log
echoinfo ${messages[2]} | tee -a $php_log
yum -y install $dep_pack &> $dev_dir
sleep 3
if_test ${messages[2]} | tee -a $php_log
}
ins_onigur(){#安装oniguruma
delimiter | tee -a $php_log
echoinfo ${messages[3]} | tee -a $php_log
wget https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.7.1-1.el7.remi.x86_64.rpm --no-check-certificate &> $dev_dir
wget https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.7.1-1.el7.remi.x86_64.rpm --no-check-certificate &> $dev_dir
rpm -ivh oniguruma5php-6.9.7.1-1.el7.remi.x86_64.rpm &> $dev_dir
rpm -ivh oniguruma5php-devel-6.9.7.1-1.el7.remi.x86_64.rpm &> $dev_dir
yum install -y oniguruma5php-* &> $dev_dir
sleep 3
if_test ${messages[3]} | tee -a $php_log
}
dec_tar(){#解压PHP安装包
delimiter | tee -a $php_log
echoinfo ${messages[4]} | tee -a $php_log
tar -zxvf php-8.1.9.tar.gz -C $src_dir/
sleep 3
if_test ${messages[4]} | tee -a $php_log
}
precom(){#PHP预编译
delimiter | tee -a $php_log
echoinfo ${messages[5]} | tee -a $php_log
cd $src_dir/php-8.1.9/
./configure dec_pack &>> $php_log
sleep 3
if_test ${messages[5]} | tee -a $php_log
}
comp(){#PHP编译
delimiter | tee -a $php_log
echoinfo ${messages[6]} | tee -a $php_log
cd $src_dir/php-8.1.9/
make -j `grep "processor" /proc/cpuinfo | wc -l` &>> $php_log
sleep 3
if_test ${messages[6]} | tee -a $php_log
}
com_ins(){#PHP编译安装
delimiter | tee -a $php_log
echoinfo ${messages[7]} | tee -a $php_log
cd $src_dir/php-8.1.9/
make install &>> $php_log
sleep 3
if_test ${messages[7]} | tee -a $php_log
}
gen_profile(){#生成配置文件
delimiter | tee -a $php_log
echoinfo ${messages[8]} | tee -a $php_log
cd $php_dir/etc/
cp php-fpm.conf.default php-fpm.conf
cp $src_dir/php-8.1.9/php.ini-production $php_dir/etc/php.ini
cp $php_dir/etc/php-fpm.d/www.conf.default $aaa
sed -i "/user = nobody/c \user = nginx" $aaa
sed -i "/group = nobody/c \group = nginx" $aaa
sed -i "/;listen.owner = nobody/c \;listen.owner = nginx" $aaa
sed -i "/;listen.group = nobody/c \;listen.group = nginx" $aaa
#sed -i "/ index index.html index.htm;/c \ index index.php index.html index.htm;" /usr/local/nginx/conf/nginx.conf
sleep 3
if_test ${messages[8]} | tee -a $php_log
}
set_start(){#PHP启动管理
delimiter | tee -a $php_log
echoinfo ${messages[9]} | tee -a $php_log
cd /root/
cp $src_dir/php-8.1.9/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod a+x /etc/init.d/php-fpm
chkconfig --add php-fpm &> $dev_dir
chkconfig php-fpm on &> $dev_dir
/etc/init.d/php-fpm start &> $dev_dir
netstat -antpu | grep php-fpm &>> $php_log
ps -ef | grep grep php-fpm &>> $php_log
/etc/init.d/php-fpm stop &> $dev_dir
systemctl restart php-fpm &>> $php_log
netstat -antpu | grep php-fpm &>> $php_log
ps -ef | grep grep php-fpm &>> $php_log
sleep 3
if_test ${messages[9]} | tee -a $php_log
}
########################################
echo "################BEGIN!!!$(date "+%F %T")################" | tee -a $php_log
#调用函数
check_tar
wait
dep_tar
wait
ins_onigur
wait
dec_tar
wait
precom
wait
comp
wait
com_ins
wait
gen_profile
wait
set_start
wait
echo "################END!!!$(date "+%F %T")################" | tee -a $php_log
########################################