1. 程式人生 > 其它 >centos7部署php5.6和nginx自動化指令碼

centos7部署php5.6和nginx自動化指令碼

centos7 自動化部署nginx,php5.6 指令碼分享

#!/bin/bash
                                                      
#******************************************************
# Author:                  liuzhaohuan
# Email:                   [email protected]
# File Name:               test_install.sh
# Time:                    2021-11-11 17:07:38
# Version:                 v1.0
# Description:             This is a test script.
                                                      
#******************************************************
#引用系統function,下文使用了action
#將要安裝的tar包和此指令碼放在同一目錄下
. /etc/init.d/functions 
#字型顏色
COLOR="echo -e \\033[01;31m"
END='\033[0m'
#當前所在路徑   
INIT_PATH=`pwd`
#啟動使用者
INSTALL_USER=bankdelyop
#安裝包,需根據實際情況修改
NGINX=nginx-1.20.1.tar.gz
#nginx安裝路徑,可根據實際情況修改
NGINX_INSTALL_PATH=/home/bankdplyop/programs/nginx
#php包 可根據實際情況修改
PHP=php-5.6.15.tar.gz
#安裝後的路徑 ,可根據實際情況修改
PHP_INSTALL_PATH=/home/bankdplyop/programs/php
check (){
cd $INIT_PATH
#判斷安裝包是否存在
if [ ! -e ${SOURCE_TAR}  ];then
	$COLOR"缺少安裝包${SOURCE_TAR}" $END
	$COLOR"請將安裝包放在$INIT_PATH目錄下" $END
	exit
#判斷是否已經安裝該專案
elif [ -e ${INSTALL_PATH} ];then
	action "${INSTALL_PATH}已存在 " false
	exit
else
return
fi
}
install_php(){
$COLOR "開始安裝php" $END
#需要安裝的包 
$COLOR "安裝中" $EDN
yum install -y make cmake gcc gcc-c++ autoconf automake libpng-devel libjpeg-devel zlib libxml2-devel ncurses-devel bison libtool-ltdl-devel libiconv libmcrypt mhash mcrypt libmcrypt-devel pcre-devel openssl-devel freetype-devel libcurl-devel >/dev/null 2>&1 
SOURCE_TAR=$PHP
INSTALL_PATH=${PHP_INSTALL_PATH}
check
tar xf $PHP
PHP_DIR=`echo ${PHP}| sed -nr 's/^(.*[0-9]).*/\1/p'`
cd ${PHP_DIR}
./configure --prefix=${INSTALL_PATH} 	 --with-pdo-mysql -with-iconv-dir --with-zlib --with-libxml-dir --enable-xml --with-curl --enable-fpm --enable-mbstring --with-gd --with-openssl --with-mhash  --enable-pdo  --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-libdir=/usr/lib64 --with-jpeg-dir=/usr/lib64 >   /dev/null  2>&1 
if [ $? == 0 ];then
  make -j  &> /dev/null  && make install  &>  /dev/null
if [ $? == 0 ];then
	action "php安裝成功,安裝路徑為${PHP_INSTALL_PATH}" true
	system_php
	
else
	action "php安裝失敗"  false
fi
  else
	  $COLOR "編譯失敗" $END
fi
}
#初始化指令碼
system_php(){
cd ${INIT_PATH}/$PHP_DIR
cp php.ini-development ${PHP_INSTALL_PATH}/lib/php.ini
cd ${PHP_INSTALL_PATH}/etc
cp php-fpm.conf.default php-fpm.conf

cat  > /usr/lib/systemd/system/php-fpm.service  << EOF

[Unit]
Description=The PHP FastCGI Process Manager
After=network.target

[Service]
Type=simple
PIDFile=${PHP_INSTALL_PATH}/var/run/php-fpm.pid
ExecStart=${PHP_INSTALL_PATH}/sbin/php-fpm --nodaemonize --fpm-config ${PHP_INSTALL_PATH}/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

EOF
   id $INSTALL_USER  > /dev/null || { useradd -s /sbin/nologin -r  ${INSTALL_USER}; $COLOR"建立${INSTALL_USER}使用者"$END; }
    sed -i.bak  -e  "s/^user.*/user = ${INSTALL_USER}/"  -e "s/^group.*/group = ${INSTALL_USER/"  ${PHP_INSTALL_PATH}/etc/php-fpm.conf
    systemctl daemon-reload
    systemctl start php-fpm 
    systemctl is-active  php-fpm &> /dev/null ||  { $COLOR"PHP-FPM 啟動失敗,退出!"$END ; exit; }
    $COLOR"PHP啟動成功,並加入系統命令" $END


}
install_nginx(){
${COLOR}"開始安裝NGINX"$END
#判斷使用者是否存在
id ${INSTALL_USER} >/dev/null   || { useradd -s /sbin/nologin -r  $INSTALL_USER; $COLOR"建立nginx使用者"$END; }
${COLOR}"安裝中----------"$END
yum  -y install gcc pcre-devel openssl-devel zlib-devel perl-devel perl-ExtUtils-Embed  git  >/dev/null 2>&1
SOURCE_TAR=$NGINX
INSTALL_PATH=${NGINX_INSTALL_PATH}
check
tar xf $NGINX
NGINX_DIR=`echo $NGINX| sed -nr 's/^(.*[0-9]).*/\1/p'`
cd ${NGINX_DIR} 
./configure --prefix=${INSTALL_PATH} --user=${INSTALL_USER} --group=${INSTALL_USER} --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_perl_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module >/dev/null 2>&1 
if [ $? == 0 ];then
  make -j  &> /dev/null  && make install  &>  /dev/null
if [ $? == 0 ];then
        action "nginx安裝成功,安裝路徑為${INSTALL_PATH}" true
else
        action "nginx安裝失敗"  false
fi
  else
          $COLOR "編譯失敗" $END
fi






}
echo '
+------------------------------------------------+
|                                                |
|    _o0o_        1. 安裝Nginx                   |
|    08880        2. 安裝php                     | 
|    88"."88      3. 安裝MySQL                   |
|    (|-_-|)      4. 安裝lnmp                    |
|    0\=/0        任意鍵退出                      |
|   __/  \__                                     |
|   ‘\  ///‘                                     |
|  / Linux一鍵 \                                 |
| ||  Server  || ================================| 
| \    ////       一鍵安裝服務                    |
|  ||| i i i  ||| by rookie_lhh                  |
|  ___    ___    ================================|
|___‘. /--.--\ .‘___                             |
+------------------------------------------------+
'
read -p "請您輸入1-8任意字元:" NUM
expr $NUM + 1 >/dev/null 2>&1
if [ "$?" -ne 0 ]
	 then
		   echo "請您輸入{1|2|3|4|}"
		     exit 1
fi
case $NUM in 
	1)
		install_nginx 
		;;
	2)
		install_php
		;;
	3)
		install_mysql
		;;
	4)
		install_all
		;;

esac