#!/bin/bash



# message
function message()
{
    if [[ $1 == "error" ]]
    then
        echo -e "\e[1;37;1;41m $2 \e[0m" 
    elif [[ $1 == "normal" ]]
    then
        echo -e "\e[30;1;1;42m $2 \e[0m" 
    fi
}

# help 
function showhelp()
{
    echo '''
##################################################
#  auth:anger                                    #
#  mail:yxmingh@hotmaiil.com                     #
##################################################
#  description:                                  #
#                                                #
#       php auto install script                  #
#       for centos 7.x                           #
#                                                #
##################################################
#  example:                                      #
#                                                #
#       $0 <php version>                         #
#                                                #
#                                                #
##################################################
#  options:                                      #
#                                                #
#       70 | 71 | 72 | 73 | 74                   #
#                                                #
##################################################
#  version:0.1                                   #
##################################################
#  take care! script must be tested before used  #
#  TAKE CARE! SCRIPT MUST BE TESTED BEFROE USED  #
##################################################
    '''
    exit 0
}

# check user
message "normal" "check user."
if [[ $UID != 0 ]]
then
    message "error" "must as user root."
    exit 0
fi

# check arg len
message "normal" "check arg."
if [[  $# != 1 ]]
then
    showhelp
    exit 0
fi

################################################################################## basic depand packages
message "normal" "install depaned soft packages"
yum makecache
yum install -y epel-release
yum install -y install yum-utils
yum install -y gcc make autoconf glibc glibc-devel pkgconfig zlib-devel

################################################################################## nginx install 
message "normal" "install nginx"
cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF

yum-config-manager --enable nginx-mainline
yum -y install nginx
systemctl start nginx
systemctl enable nginx

################################################################################## php install 
# get php version
PHP_VERSION_USER_INPUT=$1
PHP_VERSION_available=("70" "71" "72" "73" "74")
if [[ $PHP_VERSION_USER_INPUT == ${PHP_VERSION_available[0]} ]] 
then 
    PHP_VERSION="php70-php"
elif [[ "$PHP_VERSION_USER_INPUT" == ${PHP_VERSION_available[1]} ]] 
then 
    PHP_VERSION="php71-php"
elif [[ "$PHP_VERSION_USER_INPUT" == ${PHP_VERSION_available[2]} ]] 
then 
    PHP_VERSION="php72-php"
elif [[ "$PHP_VERSION_USER_INPUT" == ${PHP_VERSION_available[3]} ]] 
then 
    PHP_VERSION="php73-php"
elif [[ "$PHP_VERSION_USER_INPUT" == ${PHP_VERSION_available[4]} ]] 
then 
    PHP_VERSION="php74-php"
else
    showhelp
fi

# install remi
message "normal" "install remi"
yum install -y https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm

# install php packages
message "normal" "install php"
yum install -y $PHP_VERSION $PHP_VERSION-common $PHP_VERSION-json $PHP_VERSION-devel \
               $PHP_VERSION-fpm $PHP_VERSION-cli $PHP_VERSION-bcmath $PHP_VERSION-bz2 \
               $PHP_VERSION-curl $PHP_VERSION-pear $PHP_VERSION-gd \
               $PHP_VERSION-mbstring $PHP_VERSION-mysql $PHP_VERSION-opcache $PHP_VERSION-sqlite3 \
               $PHP_VERSION-xml $PHP_VERSION-zip $PHP_VERSION-pecl-memcache $PHP_VERSION-pecl-redis5 

################################################################################## configure files

# create web folder
message "normal" "create project folder"

wwwroot="/opt/www/wwwroot"
nginxconfig="/opt/www/config"
nginxlog="/opt/www/nginx_log"

mkdir -p $wwwroot
mkdir -p $nginxconfig
mkdir -p $nginxlog

chown -R nginx.root $wwwroot
chown -R nginx.root $nginxlog

# copy nginx config file
message "normal" "copy config file"
cp -f fastcgi.conf $nginxconfig
cp -f pathinfo.conf $nginxconfig
cp -f rewrite.conf $nginxconfig
cp -f nginx.conf /etc/nginx/
cp -f default.conf /etc/nginx/conf.d/

## reconfig nginx
message "normal" "reconfig nginx"
CPU_COUNT=`nproc`
NGINX_WORK_PROCESS=`expr $CPU_COUNT \* 2`
sed -i "s/^worker_processes.*/worker_processes $NGINX_WORK_PROCESS;/g" /etc/nginx/nginx.conf

## logrotate
message "normal" "create custom logrotate file"
cat > /etc/logrotate.d/nginx_vhost << EOF
$nginxlog/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 \`cat /var/run/nginx.pid\`
                fi
        endscript
}

EOF

## reconfig php-fpm
message "normal" "reconfig php php-fpm config file"
REMI_ROOT=/etc/opt/remi/php$PHP_VERSION_USER_INPUT
PHP_FPM_CONFIG_FILE=$REMI_ROOT/php-fpm.d/www.conf

sed -i "s#^listen = 127.*#listen = /run/php-fpm.sock#g" $PHP_FPM_CONFIG_FILE
sed -i "s/^user = apache/user = nginx/g" $PHP_FPM_CONFIG_FILE
sed -i "s/^group = apache/group = nginx/g" $PHP_FPM_CONFIG_FILE
sed -i "s/^;listen.backlog.*/listen.backlog = -1/g" $PHP_FPM_CONFIG_FILE
sed -i "s/^;listen.owner =.*/listen.owner = nginx/g" $PHP_FPM_CONFIG_FILE
sed -i "s/^;listen.group =.*/listen.group = nginx/g" $PHP_FPM_CONFIG_FILE
sed -i "s/^;listen.mode =.*/listen.mode = 0660/g" $PHP_FPM_CONFIG_FILE
sed -i "s/^php_value/;php_value/g" $PHP_FPM_CONFIG_FILE

## reconfig php.ini
PHP_INI_CONFIG_FILE=$REMI_ROOT/php.ini
sed -i "s/upload_max_filesize.*/upload_max_filesize = 12M/g" $PHP_INI_CONFIG_FILE
sed -i "s#^;date.timezone.*#date.timezone = 'Asia/Shanghai'#g" $PHP_INI_CONFIG_FILE
sed -i "s/expose_php =.*/expose_php = Off/" $PHP_INI_CONFIG_FILE

## create phpinfo page
message "normal" "create phpinfo page"
echo "<?php echo phpinfo();?>" > $wwwroot/index.php

## create .user.ini file
echo "open_basedir=$wwwroot/:/tmp/:/proc/" > $wwwroot/.user.ini

# start php fpm
message "normal" "restart php-fpm"
systemctl enable php$PHP_VERSION_USER_INPUT-php-fpm.service
systemctl start php$PHP_VERSION_USER_INPUT-php-fpm.service

# restart nginx
message "normal" "restart nginx"
systemctl restart nginx

# print infomation
echo '''
PHP

PHP VERSION : $PHP_VERSION_USER_INPUT
PHP FPM CONFIG FILE : $PHP_FPM_CONFIG_FILE
PHP CONFIG FILE : $PHP_INI_CONFIG_FILE
RESTART PHP FPM : systemctl restart php-fpm.service

NGINX

WWWROOT : $wwwroot
NGINX CONFIG : /etc/nginx/nginx.conf
NGINX VHOST COFNIG : /etc/nginx/conf.d/default.conf
NGINX VHOST LOG : $nginxlog
NGINX RESTART : systemctl restart nginx
''' > /root/.php_install.txt




The tools is on my Cloud

https://cloud.anger.ltd/index.php/s/N1NbfogXitO34o9

The share password is : anger