PHP自动编译安装脚本

1.运行前准备

/usr/local/src/下准备以下四个文件,或者见>>>PHP编译安装<<<

php_dependent.txt:依赖库

php_parameter.txt:编译参数

php_opcache.txt:opcache配置参数

www.conf:www.conf配置参数

php-fpm.service:system服务文件

[root@LNMP src]# cat php_dependent.txt
gcc gcc-c++ cmake cmake3 wget net-tools
zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel openssl-devel
freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel
libmcrypt-devel mhash mcrypt libicu-devel zlib-devel bzip2-devel glibc-devel glib2-devel ncurses-devel
krb5-devel libidn-devel openldap-devel nss_ldap jemalloc-devel
boost-devel libevent-devel readline-devel gmp-devel openjpeg-devel libsepol-devel libselinux-devel postgresql-devel
e2fsprogs-devel fontconfig-devel libaio-devel t1lib-devel libtiff-devel libvpx-devel libXpm-devel libcom_err-devel
libtool bison re2c flex php-common php-gd yasm nasm ntsysv bind-utils
[root@LNMP src]# cat php_opcache.txt
[date]
date.timezone = Asia/Shanghai
[ldap]
extension = "ldap.so"
[opcache]
zend_extension=" /usr/local/php/lib/php/extensions/no-debug-zts-20180731/opcache.so"
opcache.enable=1
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=2
[root@LNMP src]# cat www.conf
[www]
user = nginx
group = nginx
listen = /usr/local/php/var/run/php-cgi.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = dynamic
;pm = static
listen.backlog = -1
pm.max_children = 30
pm.start_servers = 8
pm.min_spare_servers = 4
pm.max_spare_servers = 16
request_terminate_timeout = 120
request_slowlog_timeout = 50
slowlog = var/log/slow.log
[root@LNMP src]# cat php-fpm.service
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
[root@LNMP src]# cat php_parameter.txt
--prefix=/usr/local/php-7.3.5
--with-config-file-path=/usr/local/php/etc
--with-fpm-user=nginx
--with-fpm-group=nginx
--with-curl
--with-freetype-dir
--with-gd
--with-gettext
--with-iconv-dir=/usr/local/libiconv
--with-kerberos
--with-libdir=lib64
--with-libxml-dir
--with-mysqli=mysqlnd
--with-openssl
--with-pcre-regex
--with-pdo-mysql=mysqlnd
--with-pdo-sqlite
--with-pear
--with-jpeg-dir
--with-png-dir
--with-xmlrpc
--with-xsl
--with-zlib
--with-zlib-dir=
--with-bz2
--with-mhash
--with-ldap
--enable-bcmath
--enable-fpm
--enable-inline-optimization
--enable-mbregex
--enable-mbstring
--enable-xml
--enable-zip
--disable-rpath
--enable-mysqlnd
--enable-sysvsem
--enable-sysvshm
--enable-opcache
--enable-session
--enable-pcntl
--enable-shmop
--enable-soap
--enable-sockets
--enable-intl
--enable-maintainer-zts
--disable-fileinfo

2.脚本

#!/bin/bash
. /etc/init.d/functions
VERSION="php-7.3.5"
PACKAGE_DIR="/usr/local/src"
BASE_DIR="/usr/local/${VERSION}"
WORK_DIR="/usr/local/php"
PARAMETER="${PACKAGE_DIR}/php_parameter.txt"
DEPENDENT_LIB="${PACKAGE_DIR}/php_dependent.txt"
SERVICE_FILE="${PACKAGE_DIR}/php-fpm.service"
OPCACHE="${PACKAGE_DIR}/php_opcache.txt"
WWWCONF="${PACKAGE_DIR}/www.conf"
NGINX_USER="nginx"
###
echo -e "\033[34m######Starting install ${VERSION}######\033[0m"
###Error Tip###
Error(){
action "$1" /bin/false
exit 1
}
###安装依赖###
###添加epel源###
AddEpel(){
if [[ ! -f /etc/yum.repos.d/epel.repo ]]
then
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo || Error "Add epel Failed"
action "epel.repo Added Successfully" /bin/true
else
action "epel.repo Already Exists" /bin/true
fi
}
InstallDep(){
if [[ -f ${DEPENDENT_LIB} ]]
then
for i in `cat ${DEPENDENT_LIB}`
do
echo "Starting install ${i}"
yum install -y ${i} &>/dev/null || Error "Install ${i} Package Failed"
action "${i} Package Installed Successfully" /bin/true
done
else
action "${DEPENDENT_LIB} Is Not Found" /bin/false
fi
}
###安装libiconv###
InstallLibiconv(){
Package_URL="http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz"
Package_Version="libiconv-1.16"
Libiconv_Package=`find ${PACKAGE_DIR} -name ${Package_Version}.tar.gz`
if [[ -d /usr/local/libiconv ]]
then
action "Package Already Installed" /bin/true
else
if [[ ! -f ${Libiconv_Package} ]]
then
action "Starting Download Package" /bin/true
wget -P ${PACKAGE_DIR} ${Package_URL} || Error "Download Package Failed"
action "Package Downloaded Successfully" /bin/true
cd ${PACKAGE_DIR}
tar xf ${Package_Version}.tar.gz || Error "Unzip Package Failed"
action "Package Unzipped Successfully,Starting Install" /bin/true
cd ${Package_Version}
${PACKAGE_DIR}/${Package_Version}/configure --prefix=/usr/local/libiconv || Error "Configure Package Failed"
action "Configured Successfully" /bin/true
make -j2 && make install || Error "Make Package Failed"
action "Libiconv Installed Successfully" /bin/true
else
cd ${PACKAGE_DIR}
tar xf ${Package_Version}.tar.gz || Error "Unzip Package Failed"
action "Package Unzipped Successfully,Starting Install" /bin/true
cd ${Package_Version}
${PACKAGE_DIR}/${Package_Version}/configure --prefix=/usr/local/libiconv || Error "Configure Package Failed"
action "Configured Successfully" /bin/true
make -j2 && make install || Error "Make Package Failed"
action "Libiconv Installed Successfully" /bin/true
fi
fi
}
###卸载旧版本libzip###
RemoveLibzip(){
Libzip_Check="rpm -qa|grep libzip"
if [[ $? == 0 ]]
then
yum remove libzip* -y &>/dev/null || Error "Remove Libzip Failed"
action "Libzip Removed Successfully" /bin/true
else
action "Libzip Already Removed" /bin/true
fi
}
###安装libzip###
InstallLibzip(){
Package_URL="https://libzip.org/download/libzip-1.5.2.tar.gz"
Package_Version="libzip-1.5.2"
Libzip_Package=`find ${PACKAGE_DIR} -name ${Package_Version}.tar.gz`
if [[ -L /usr/local/lib64/libzip.so.5 ]]
then
action "Package Already Installed" /bin/true
else
if [[ ! -f ${Libzip_Package} ]]
then
action "Starting Download Package" /bin/true
wget -P ${PACKAGE_DIR} ${Package_URL} || Error "Download Package Failed"
action "Package Downloaded Successfully" /bin/true
cd ${PACKAGE_DIR}
tar xf ${Package_Version}.tar.gz || Error "Unzip Package Failed"
action "Package Unzipped Successfully,Starting Install" /bin/true
cd ${Package_Version} && mkdir build && cd build && cmake3 ..
action "Configured Successfully" /bin/true
make -j2 && make install || Error "Make Package Failed"
action "Libiconv Installed Successfully" /bin/true
else
cd ${PACKAGE_DIR}
tar xf ${Package_Version}.tar.gz || Error "Unzip Package Failed"
action "Package Unzipped Successfully,Starting Install" /bin/true
cd ${Package_Version} && mkdir build && cd build && cmake3 ..
action "Configured Successfully" /bin/true
make -j2 && make install || Error "Make Package Failed"
action "Libiconv Installed Successfully" /bin/true
fi
fi
}
###添加lib搜索路径###
AddLibPath(){
Lib_Check=`cat /etc/ld.so.conf|grep /usr/local/lib64`
if [[ $? != 0 ]]
then
echo -e "/usr/local/lib64\n/usr/local/lib\n/usr/lib\n/usr/lib64\n/usr/local/libiconv/lib" >> /etc/ld.so.conf || Error "AddLibPath Failed"
action "AddLibPath Success" /bin/true
else
action "LibPath Already Exists" /bin/true
fi
ldconfig -v &>/dev/null
}
###创建用户###
CreateUser(){
User_Check=`cat /etc/passwd|grep ${NGINX_USER}`
if [[ $? != 0 ]]
then
useradd -s /sbin/nologin -M ${NGINX_USER} || Error "Create ${NGINX_USER} User Failed"
action "${NGINX_USER} User Created Successfully" /bin/true
else
action "User ${NGINX_USER} Exists" /bin/true
fi
}
###下载安装包###
DownloadPackages(){
Package_URL="https://www.php.net/distributions/php-7.3.5.tar.gz"
Php_Package=`find ${PACKAGE_DIR} -name ${VERSION}*.tar.gz`
if [[ ! -f ${Php_Package} ]]
then
action "Starting Download Package" /bin/true
wget -P ${PACKAGE_DIR} ${Package_URL} || Error "Download Package Failed"
action "Package Downloaded Successfully" /bin/true
else
action "Package Already Exists" /bin/true
fi
}
UnzipPackages(){
Php_Folder=`find ${PACKAGE_DIR} -name ${VERSION}*.tar.gz -exec basename {} .tar.gz \;`
Php_Package=`find ${PACKAGE_DIR} -name ${VERSION}*.tar.gz`
if [[ ! -d ${PACKAGE_DIR}/${Php_Folder} ]]
then
echo "Starting Unzip ${VERSION} Package,Just Wait"
tar xf ${Php_Package} -C ${PACKAGE_DIR}|| Error "Unzip Package Failed"
action "Package Unzipped Successfully" /bin/true
else
action "Package Already Unzipped" /bin/true
fi
}
###编译安装###
InstallPhp(){
Php_Path="${PACKAGE_DIR}/${Php_Folder}"
if [[ ! -d ${BASE_DIR} ]]
then
if [[ -f ${PACKAGE_DIR}/php_parameter.txt ]]
then
cd ${Php_Path}
${Php_Path}/configure `cat ${PARAMETER}` || Error "Configure Package Failed"
action "Configured Successfully" /bin/true
make -j2 && make install || Error "Make Package Failed"
action "${VERSION} Installed Successfully" /bin/true
ln -s ${BASE_DIR} ${WORK_DIR} || Error "Link ${Php_Path} ${WORK_DIR} Failed"
action "Link ${Php_Path} ${WORK_DIR} Successfully" /bin/true
else
action "PARAMETER.TXT Is Not Found" /bin/false
fi
else
action "${VERSION} Already Installed" /bin/true
fi
}
###设置环境变量###
InitEnv(){
bashrc="/etc/profile"
Check_Env=`cat ${bashrc}|grep ${WORK_DIR}`
if [[ ${Check_Env} == "" ]]
then
echo "export PATH=/usr/local/php/bin:\$PATH" >> ${bashrc} || Error "Set Environment Failed"
action "Environment Setted Successfully" /bin/true
else
action "${BASE_DIR} Is Already Exists In ${bashrc}" /bin/true
fi
source /etc/profile
}
###创建配置文件###
CreateConFile(){
Php_Folder=`find ${PACKAGE_DIR} -name ${VERSION}*.tar.gz -exec basename {} .tar.gz \;`
FilePath="/usr/local/php/etc"
if [[ ! -f ${FilePath}/php.ini ]]
then
cp ${PACKAGE_DIR}/${Php_Folder}/php.ini-production ${FilePath}/php.ini.bak || Error "Cp File Failed"
action "Cp File Success" /bin/true
cd ${FilePath}
cp php-fpm.conf.default php-fpm.conf
egrep -v '^$|;' php-fpm.d/www.conf.default > php-fpm.d/www.conf
egrep -v '^$|;' php.ini.bak > php.ini
action "File Created Successfully" /bin/true
else
action "Php.ini Already Exists" /bin/true
fi
}
###php.ini参数优化###
OptPara(){
Para_Check=`cat ${FilePath}/php.ini|grep 'expose_php = Off'`
if [[ $? != 0 ]]
then
sed -i 's@expose_php = On@expose_php = Off@g' ${FilePath}/php.ini
sed -i 's@short_open_tag = Off@short_open_tag = ON@g' ${FilePath}/php.ini
sed -i 's@max_execution_time = 30@max_execution_time = 120@g' ${FilePath}/php.ini
sed -i 's@max_input_time = 60@max_input_time = 120@g' ${FilePath}/php.ini
sed -i 's@memory_limit = 128M@memory_limit = 128M@g' ${FilePath}/php.ini
sed -i 's@post_max_size = 8M@post_max_size = 16M@g' ${FilePath}/php.ini
action "Parameters Optimized Successfully" /bin/true
else
action "${FilePath}/php.ini Already Setted" /bin/true
fi
Check_Item="/usr/local/php/lib/php/extensions"
Opcache_Check=`cat ${FilePath}/php.ini|grep ${Check_Item}`
if [[ $? != 0 ]]
then
cat ${OPCACHE} >> ${FilePath}/php.ini || Error "Set Opcache Parameters Failed"
action "Opcache Parameters Setted Successfully" /bin/true
else
action "Opcache Parameters Already Setted" /bin/true
fi
}
###创建php-cgi.sock目录###
CreateSockPath(){
SockPath="/usr/local/php/var/run"
if [[ ! -d ${SockPath} ]]
then
mkdir -p ${SockPath}
chown -R ${NGINX_USER}:${NGINX_USER} ${SockPath} || Error "Change The Directory Owner Failed"
action "${SockPath} Setted Successfully" /bin/true
else
Owner_Check=`ls -l -d ${SockPath}|awk '{print $3}'`
if [[ "${Owner_Check}" != "${NGINX_USER}" ]]
then
chown -R ${NGINX_USER}:${NGINX_USER} ${SockPath} || Error "Change The Directory Owner Failed"
action "${SockPath} Setted Successfully" /bin/true
fi
fi
}
###优化www.conf###
OptWWWConf(){
www_check=`cat ${FilePath}/php-fpm.d/www.conf|grep ${SockPath}`
if [[ $? != 0 ]]
then
if [[ -f ${FilePath}/php-fpm.d/www.conf ]]
then
cat ${WWWCONF} > ${FilePath}/php-fpm.d/www.conf || Error "Set www.conf Failed"
action "www.conf Setted Successfully" /bin/true
else
action "www.conf Is Not Found" /bin/false
fi
else
action "www.conf Already Setted" /bin/true
fi
}
###配置php-fpm###
OptFpm(){
Fpm_Check=`cat ${FilePath}/php-fpm.conf|grep ${SockPath}`
if [[ $? != 0 ]]
then
sed -i 's@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@g' ${FilePath}/php-fpm.conf
sed -i 's@;error_log = log/php-fpm.log@error_log = /usr/local/php/var/log/php-fpm.log@g' ${FilePath}/php-fpm.conf
sed -i 's@;log_level = notice@log_level = notice@g' ${FilePath}/php-fpm.conf
action "${FilePath}/php-fpm.conf Setted Successfully" /bin/true
else
action "${FilePath}/php-fpm.conf Already Setted" /bin/true
fi
}
###配置system服务###
PhpService(){
File_Check="/lib/systemd/system/php-fpm.service"
if [[ ! -f ${File_Check} ]]
then
cp ${SERVICE_FILE} /lib/systemd/system/ || Error "Copy Service File Failed"
action "Copy Service File Successfully" /bin/true
else
action "Service File Already Exists" /bin/true
fi
}
###启动服务###
StartPhp(){
systemctl start php-fpm || Error "Php-fpm Start Failed"
systemctl enable php-fpm || Error "Php-fpm Start Failed"
action "php-fpm Start Successfully" /bin/true
}
TaskMain(){
echo -e "\033[32mStep One:Starting ADD epel.repo\033[0m"
AddEpel
echo -e "\033[32mStep One:Starting Install Dependency Packages\033[0m"
InstallDep
echo -e "\033[32mStep Two:Starting Install Dependency Packages Libiconv\033[0m"
InstallLibiconv
echo -e "\033[32mStep Three:Starting Remove Libzip\033[0m"
RemoveLibzip
echo -e "\033[32mStep Four:Starting Install Libzip\033[0m"
InstallLibzip
echo -e "\033[32mStep Five:Starting Add LibPath\033[0m"
AddLibPath
echo -e "\033[32mStep Six:Starting Create Nginx User\033[0m"
CreateUser
echo -e "\033[32mStep Seven:Starting Download Package\033[0m"
DownloadPackages
echo -e "\033[32mStep Eight:Starting Unzip Package\033[0m"
UnzipPackages
echo -e "\033[32mStep Nine:Starting Install PHP\033[0m"
InstallPhp
echo -e "\033[32mStep Ten:Starting Set Environment\033[0m"
InitEnv
echo -e "\033[32mStep Eleven:Starting Create Configure Files\033[0m"
CreateConFile
echo -e "\033[32mStep Twelve:Starting Optimize Parameters\033[0m"
OptPara
echo -e "\033[32mStep Thirteen:Starting Create SockPath\033[0m"
CreateSockPath
echo -e "\033[32mStep Fourteen:Starting Optimize WWW.CONF Parameters\033[0m"
OptWWWConf
echo -e "\033[32mStep Fifteen:Starting Optimize PHP-FPM Parameters\033[0m"
OptFpm
echo -e "\033[32mStep Sixteen:Starting Copy Service File\033[0m"
PhpService
echo -e "\033[32mStep Seventeen:Starting Start Service\033[0m"
StartPhp
echo -e "\033[32mCongratulations, PHP Installation Was Successfully\033[0m"
}
TaskMain


「 文章如果对你有帮助,请点个赞哦^^ 」 

0

这篇文章有 2 条评论

评论关闭。