一键编译安装PHP

 

#!/bin/bash
#Author:chenwei
#Time:2020-07-28 11:30:44
#Name:auto_install_php-fpm.sh
#Version:V1.0
#Description:This script is used for automatically

#1.0 环境变量定义
function define_env(){
    TOOLS_PATH="/home/nameke/tools"
    CONF_PATH="/home/nameke/conf"
}

#2.0 确保数据库和web服务软件安装并正常使用
function check_ngx_mysql(){
    echo -e "\033[32m Step2:Check NGINX and MySQL ... \033[0m" && sleep 3;
    ngx_mysql=$(netstat -lntup|egrep "nginx|mysql"|wc -l)
    ngx_mysql_proc=$(ps aux|egrep "nginx|mysql"|egrep -v grep|wc -l)
    if [ $ngx_mysql -gt 0 -a $ngx_mysql_proc -gt 0 ]
    then
        echo -e "\033[32m Nginx and MySQL had installed! \033[0m"
    else
        echo -e "\033[31m Nginx or MySQL doesn\'t installed,please check! \033[0m"
        exit 2
    fi
}

#3.0 检查安装php所需的lib库
function install_lib(){
    rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
    rpm -qa freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel
    yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
    yum install -y freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel
    echo -e "\033[32m Step3:Install lib Inventory succ! \033[0m"
}

#4.0 yum安装无法安装的libiconv库
function install_libiconv(){
    cd $TOOLS_PATH && wget -c -q http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
    tar zxf libiconv-1.14.tar.gz && cd $TOOLS_PATH/libiconv-1.14 && ./configure --prefix=/usr/local/libiconv
    if [ $? -eq 0]
    then
        echo -e "\033[32m Step4:Configure lib libiconv succ! \033[0m"
        sed -i \'s#_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");#/*_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");*/#g\' $TOOLS_PATH/libiconv-1.14/srclib/stdio.in.h
        sed -i \'2a #if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)\n_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");\n#endif\n\' $TOOLS_PATH/libiconv-1.14/srclib/stdio.in.h
        make && make install
        if [ $? -eq 0 ]
        then
            echo -e "\033[32m make and make install libiconv succ! \033[0m"
            cd $TOOLS_PATH
        else
            echo -e "\033[31m make and make install libiconv fail! \033[0m"
            exit 2;
        fi
    else
        echo -e "\033[31m Step4:Configure lib libiconv fail! \033[0m"
        exit 2;
    fi
}
#4.0 安装libmcrypt库
function install_libmcrypt(){
    cd /etc/yum.repos.d/ && wget -c -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    yum -y install libmcrypt-devel && echo "\033[32m Step5:Install libmcrypt succ! \033[0m"
}
#5.0 安装mhash与mcrypt加密扩展库
function install_mhash_mcrypt(){
    yum install -y mhash mcrypt && echo "\033[32m Step6:Install mhash and mcrypt succ! \033[0m"
}
#6.0 解压配置PHP
function install_php(){
cd $TOOLS_PATH && wget -c -q https://www.php.net/distributions/php-7.2.30.tar.gz
tar zxf php-7.2.30.tar.gz && cd $TOOLS_PATH/php-7.2.30 && ./configure \
--prefix=/data/php7.2.30 \
--with-config-file-path=/etc/php7.2.30 \
--with-mysql-sock=/data/mysql3306/var/mysql.sock \
--with-pdo-mysql=shared,mysqlnd \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--enable-fpm \
--with-fpm-user=nameke \
--with-fpm-group=nameke \
--enable-ftp
#特别提示:上述每行结尾的换行符反斜线(\)之后,不能再有任何字符,包括空格。
    if [ $? -eq 0 ]
    then
        echo -e "\033[32m Step7:Configure php-7.2.30 lib succ! \033[0m"
    else
        echo -e "\033[31m Step7:Configure php-7.2.30 lib fail! \033[0m"
        exit 2;
    fi
}
#7.0 编译PHP
function deconde_php(){
    ln -s /data/mysql3306/lib/libmysqlclient.so.20 /usr/lib64
    cd $TOOLS_PATH/php-7.2.30 && touch ext/phar/phar.phar
    make && make install
    if [ $? -eq 0 ]
    then
        echo -e "\033[32m Step8:Make and Make install php-7.2.30 succ! \033[0m"
        cd $TOOLS_PATH
    else
        echo -e "\033[31m Make and Make install php-7.2.30 fail! \033[0m"
        exit 2;
    fi
}

#8.0 配置PHP引擎配置文件php.ini
function config_php(){
    ln -s /data/php7.2.30 /data/php #&& ls -l /data/php/php.ini*
    cd $TOOLS_PATH/php-7.2.30 && cp php.ini-production /data/php/lib/php.ini
    ls -l /data/php/lib/php.ini && echo -e "\033[32m Step8:Config php-7.2.30 link file succ! \033[0m"
}
#9.0 配置PHP服务(FastCGI方式)的配置文件php-fpm.conf
function config_php_fpm(){
    cd /data/php/etc && \cp -f php-fpm.conf.default php-fpm.conf
    cp php-fpm.d/www.conf.default php-fpm.d/www.conf && echo -e "\033[32m Step9:Config php_fpm succ! \033[0m"
}
#10.0 启动PHP服务并检查(FastCGI方式)
function check_start_php(){
    /data/php/sbin/php-fpm -t && /data/php/sbin/php-fpm
    ps -ef|grep php-fpm
    lsof -i:9000
    phpfpm_port=$(netstat -antlp|grep 9000|egrep -v grep|wc -l)
    phpfpm_proc=$(ps aux|grep php-fpm|egrep -v grep|wc -l)
    if [ $phpfpm_port -gt 0 -a $phpfpm_proc -gt 0 ]
    then
        echo -e "\033[32m Step10:Start php-fpm succ! \033[0m"
    else
        echo -e "\033[32m Step10:Start php-fmp fail!,please check\033[0m"
        exit 2;
    fi
}

#11.0 配置nginx支持php程序请求访问
#11.1 查看当前配置
function config_ngx_php(){
    #cd /etc/nginx/conf && cp nginx.conf nginx.conf.bak-$(date +%Y%m%d-%H%m)
    #\cp -f $CONF_PATH/webdemo.conf /etc/nginx/conf/nginx.conf
    /etc/nginx/sbin/nginx -t && /etc/nginx/sbin/nginx -s reload
#11.2 编写index.php测试PHP解析请求
    \cp -f $CONF_PATH/test_php.php /webroot/webdemo/
    \cp -f $CONF_PATH/test_mysql.php /webroot/webdemo/
    cat >>/webroot/webdemo/test_mysql.php<<EOF
<?php
    $servername = "localhost";
    $username = "root";
    $password = "13.14xiaoCY";
    //$link_id=mysql_connect(\'主机名\',\'用户\',\'密码\');
    //mysql -u用户 -p密码 -h 主机
    $conn = mysqli_connect($servername, $username, $password);
    if ($conn) {
        echo "mysql successful by root !\n";
    }
    else{
        die("Connection failed: " . mysqli_connect_error());
    }
?>
EOF
    chown -R nameke:nameke /webroot/webdemo/
    ls -l /webroot/webdemo/
}

#13、设置开机自启动
function autostart_phpfpm(){
    echo -e "#start nginx+php-fpm by oldboy at 20200913" >>/etc/rc.local
    #echo -e "/etc/nginx/sbin/nginx -t && /etc/nginx/sbin/nginx" >>/etc/rc.local
    echo -e "/data/php/sbin/php-fpm -t && /data/php/sbin/php-fpm">>/etc/rc.local
    #echo -e "/data/mysql3306/bin/mysqld --defaults-file=/data/mysql3306/etc/my.cnf &" >>/etc/rc.local
    echo -e "\033[32m config auto_start php-fpm nginx mysql succ !\033[0m"
}
function main(){
    if [ $UID -ne 0 ]
    then
        echo -e "\033[31m You must be root to run this script! \033[0m"
        exit 2;
    else
        define_env;
        check_ngx_mysql;
        install_lib;
        install_libiconv;
        install_libmcrypt;
        install_mhash_mcrypt;
        install_php;
        deconde_php;
        config_php;
        config_php_fpm;
        check_start_php;
        config_ngx_php;
        autostart_phpfpm;
        echo -e "\033[32m End of install php-fpm! \033[0m"
        exit 2
}
main

 

版权声明:本文为chenwei01原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/chenwei01/articles/14719999.html