Linux 使用脚本安装PHP7.4.6

该脚本首先安装一些编译工作所必须的安装包,然后检测是否有php7.4.6安装包,如果没有,请按提示输入y,脚本自动下载并进行后续的安装工作,如果有安装包,脚本将会解压并继续后续的编译安装工作。下面是脚本的源码:

#!/bin/bash
yum install -y -q autoconf automake apr apr-devel apr-util apr-util-devel \
bison bzip2-devel cpp fontconfig-devel freetype-devel gcc gcc-c++ compat-dapl \
compat-db-headers compat-db47 compat-gcc-44 compat-gcc-44-c++ compat-glibc \
compat-glibc-headers compat-libcap1 compat-libf2c-34 compat-libgfortran-41 \
compat-libtiff3 compat-openldap ncurses-deveal wget met-tools \
libxml2-devel libXpm-devel libcurl php-mysqlnd freetype-devel \
fontconfig-devel openssl-devel curl curl-devel oniguruma \
oniguruma-devel sqlite-devel t1lib t1lib-devel libjpeg libjpeg-devel

#wget https://libzip.org/download/libzip-1.3.2.tar.gz
tar xf libzip-1.2.0.tar.gz
cd libzip-1.2.0/
./configure --prefix=/usr/local/libzip && make && make install

ln -s /usr/lib64/libltdl.so /usr/lib/libltdl.so
cp -rfp /usr/lib64/libXpm.so* /usr/lib/ && cd ~
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib:/usr/lib64
export PKG_CONFIG_PATH=/usr/local/libzip/lib/pkgconfig
if [ -f php-7.4.6.tar.gz ]
then
echo '安装包已存在,不需要下载了'
else
echo '没有安装包,请准备下载'
read -p "请输入y开始下载: " n

n2=`echo $n | sed 's/y//g'`

if [ -n "$n2" ];then
echo "你输入的不是y,程序将退出."
exit
fi


case $n in
y)
echo "开始下载"
wget https://www.php.net/distributions/php-7.4.6.tar.gz
;;
*)
echo "please input y"
;;
esac
fi

tar -zxf `find / -name php-7.4.6.tar.gz` -C /usr/local/src/
cd /usr/local/src/php-7.4.6
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --with-freetype --with-jpeg --with-zlib --with-libxml --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --enable-gd --enable-gd-jis-conv --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-pear --enable-maintainer-zts --without-gdbm --with-gnu-ld --enable-libgcc
echo "预编译完成,开始正式安装PHP,等待5秒后开始"
sleep 5

make 2>&1 |tee /root/make.log && make install
echo "安装完毕,开始初始化的工作"
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
rm -rf /etc/php.ini
cp /usr/local/src/php-7.4.6/php.ini-production /usr/local/php/etc/php.ini
ln -s /usr/local/php/etc/php.ini /etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
cp /usr/local/src/php-7.4.6/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig php-fpm on
echo "user=nginx
group=nginx">>/usr/local/php/etc/php-fpm.conf
echo "<?php
phpinfo();
?>
">/usr/local/nginx/html/info.php
echo "执行命令 vim /usr/local/php/etc/php-fpm.conf,将 pid=run/php-fpm.pid的注释去掉"
echo "修改 vim /usr/local/nginx/conf/nginx.conf文件,添加 user=nginx nginx;
将fastcgi_param 这一行修改 $document_root$fastcgi_script_name;
最后执行 service php-fpm start nginx -s reload"

脚本运行完毕后,请按照提示修改配置php-fpm和nginx的配置文件,并输入IP地址/info.php进行测试,如果有显示php详细信息,那么php和niginx功能正常。