#!/bin/bash
yum install epel-release -y # 安装扩展包并更新系统内核
yum list
yum -y install wget vim pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel
# 安装php依赖组件(包含Nginx依赖)
cd /tmp
groupadd www # 创建一个 www 的组
useradd -g www www # 创建用户www 归属于 www组
yum install -y wget ; yum install -y star
# 安装wget 下载工具和star 解压缩工具
wget http://am1.php.net/distributions/php-7.2.1.tar.gz
tar xvf php-7.2.1.tar.gz # tar -xvf 是解压,-cvf 是创建压缩包
cd php-7.2.1
# 创建用户和组,并下载php安装包解压
cp -frp /usr/lib64/libldap* /usr/lib/
#### 编译安装 是一条命令的开始
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--with-mcrypt \
--with-libmbfl \
--enable-ftp \
--with-gd \
--enable-gd-jis-conv \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm
#### 编译安装 是一条命令结束
make -j 4 && make install
cp php.ini-development /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
setphpini=`find / -name "php.ini"` # 定义一个变量名字为:setphpini他的值为一个 find 查找文件 php.ini 命令返回的结果
sed -i "s/expose_php.*/expose_php\=off/g" $setphpini
# sed -i 修改文件,匹配前一个字符串及其后面所有字符串,修改为后一个; \ 转译特殊字符 $setphpini 是一个文件路径变量
sed -i "s/short_open_tag.*/short_open_tag\=ON/g" $setphpini
sed -i "s/max_execution_time.*/max_execution_time\=300/g" $setphpini
sed -i "s/max_input_time.*/max_input_time\=300/g" $setphpini
sed -i "s/memory_limit.*/memory_limit\=128M/g" $setphpini
sed -i "s/post_max_size.*/post_max_size\=30M/g" $setphpini
sed -i "s/date.timezone.*/date.timezone\=Asia\/Shanghai/g" $setphpini
sed -i "s/memory_limit.*/memory_limit\=128M/g" $setphpini
sed -i "s/mbstring.func_overload.*/mbstring.func_overload\=2/g" $setphpini
NR=`cat -n $setphpini | grep "mbstring.func_overload" | awk -F" " '{print $1}' | sed "s/ //g"`
# 定义行号变量 NR ; grep匹配字符串所在行, 打印第一列,删除空格
sed -i "$NR a\extension = "/usr/local/php/lib/php/extensions/no-debug-zts-20170718/ldap.so"" $setphpini
# 写入一个字符串, 在行 $NR 的 a\ 下一行写入 、
NR=`cat -n $setphpini | grep "\[opcache" | awk -F" " '{print $1}' | sed "s/ //"`
sed -i "$NR a\zend_extension=/usr/local/php/lib/php/extensions/no-debug-zts-20170718/opcache.so" $setphpini
sed -i "s/opcache.max_accelerated_files.*/opcache.max_accelerated_files\=4000/g" $setphpini
sed -i "s/opcache.revalidate_freq.*/opcache.revalidate_freq\=60/g" $setphpini
sed -i "s/opcache.fast_shutdown.*/opcache.fast_shutdown\=1/g" $setphpini
sed -i "s/opcache.enable_cli.*/opcache.enable_cli\=1/g" $setphpini
sed -i "s/disable_functions.*/disable_functions\=passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthrug" $setphpini
NR=`cat -n $setphpini | grep disable_functions | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR a\listen = /var/run/www/php-cgi.sock " $setphpini
############################
wwwconf=`find / -name "www.conf" | grep "usr"` ; echo "修改配置文件:"$wwwconf
############################
NR=`cat -n $wwwconf | grep "listen \= " | awk -F" " '{print $1}' | sed "s/ //g"`
# cat -n 查看文件显示行号, awk 字段处理打印第一列为行号
sed -i "$NR s/listen.*/listen \= \/var\/run\/www\/php-cgi.sock/g" $wwwconf
# 获取到行号,sed -i 指定行号的内容进行修改
NR=`cat -n $wwwconf | grep "listen.owner = www" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR a\listen.owner = www" $wwwconf # sed -i 在指定行号$NR的 a\ 的下一行插入字符串
NR=`cat -n $wwwconf | grep "listen.group = www" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR a\listen.group = www" $wwwconf
NR=`cat -n $wwwconf | grep "listen.mode =" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR a\listen.mode = 0660" $wwwconf
NR=`cat -n $wwwconf | grep "listen.allowed_clients" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR a\listen.allowed_clients = 127.0.0.1" $wwwconf
NR=`cat -n $wwwconf | grep "listen.backlog" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR a\listen.backlog = -1" $wwwconf
NR=`cat -n $wwwconf | grep "pm.max_children =" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR s/pm.max_children \=.*/pm.max_children \= 180/g" $wwwconf
NR=`cat -n $wwwconf | grep "pm.start_servers =" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR s/pm.start_servers \=.*/pm.start_servers \= 50/g" $wwwconf
NR=`cat -n $wwwconf | grep "pm.min_spare_servers =" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR s/pm.min_spare_servers \=.*/pm.min_spare_servers \= 50/g" $wwwconf
NR=`cat -n $wwwconf | grep "pm.max_spare_servers =" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR s/pm.max_spare_servers \=.*/pm.max_spare_servers \= 180/g" $wwwconf
NR=`cat -n $wwwconf | grep "request_terminate_timeout =" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR a\request_terminate_timeout = 120" $wwwconf
# $NR 在制定行, a 是下一行插入字符串
NR=`cat -n $wwwconf | grep "request_slowlog_timeout =" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR a\request_slowlog_timeout = 50" $wwwconf
NR=`cat -n $wwwconf | grep "slowlog =" | awk -F" " '{print $1}' | sed "s/ //g"` ;
sed -i "$NR a\slowlog = var/log/slow.log" $wwwconf
mkdir /var/run/www/
chown -R www:www /var/run/www # 创建php-cgi.sock存放目录
NR=`cat -n /usr/local/php/etc/php-fpm.conf | grep "pid =" | awk -F" " '{print $1}' | sed "s/ //g"`
sed -i "$NR a\pid = /usr/local/php/var/run/php-fpm.pid" /usr/local/php/etc/php-fpm.conf
echo -e "
[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
" > /usr/lib/systemd/system/php-fpm.service
systemctl enable php-fpm.service
systemctl restart php-fpm.service