#!/bin/bash
#lnmp auto setup shell script
#2019-05-26
#LNP (php 7.1.26 nginx 1.15.9 ) 源码安装
#Version 2.0
clear
date
echo ""
echo "#####################################"
echo "#                                   #"
echo "# lnp版本：nginx-1.15.9 php-7.1.26  #"
echo "#                                   #"
echo "#####################################"
for i in `seq -w 3 -1 0`
do
echo -ne "$i 秒\b\b\b\b"
sleep 1
done
echo ""
MYVER=`cat /etc/redhat-release |awk '{print $4}'| awk -F [.] '{print $1}'`
#expr 1 + $MYVER &>/dev/null
if [ $MYVER -ne 7 ];then
    echo "请在 7 版本的系统里面运行该脚本....."
    exit
fi
if [ $UID -ne 0 ]; then
    echo "请用 root 用户运行该脚本...."
    exit
fi
    echo '网络检测中.......................'
    echo ' '
    sleep 1
#检测有无外网
ping -c 1 114.114.114.114 > /dev/null 2>&1
if [ $? -eq 0 ];then
    echo '网络正常，程序将继续运行。'
    sleep 1
else
    echo '正在进行第二次网络检测，请稍候.........'
    sleep 1
curl www.baidu.com >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo '检测到网络连接有异常，请检查您的网络设置.....'
    exit
else
    echo ''
    echo '网络正常，程序将继续运行.'
    sleep 1
fi
fi
yum install wget -y >/tmp/a.txt
echo "开始安装 PHP 7.1.26 ................."
sleep 1
###################### 源码安装 PHP 7.1.26 #################################
PSPHP0=`ps -ef |grep php-fpm |grep -v grep | wc -l`
if [ $PSPHP0 -ge 1 ]; then
    echo 'php 已运行，不需要再安装。'
    ps -ef |grep php | grep -v grep
    sleep 1
else
if [ ! -f "/opt/php-7.1.26.tar.gz" ];then
    echo "php-7.1.26.tar.gz 文件不存在，我们将在网上下载......"
    sleep 2
    cd /opt
    wget -q http://www.xchinagroup.top/softdown/centos7/01_php/php-7.1.26.tar.gz
    if [ $? -ne 0 ]; then
        echo ''
        echo "下载有问题，请检查......"
        exit
    fi
fi
#安装依赖包：
yum install -y libtomcrypt* curl-devel libjpeg-devel libpng-devel freetype-devel libxslt libxslt-devel cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel cyrus-sasl-lib m4 autoconf gcc gcc-c++ openssl openssl-devel ncurses-devel pcre pcre-devel zlib* net-tools lrzsz vim wget autoconf automake libxml* ncurses-devel libtool-ltdl-devel* gcc gcc-c++ libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel pcre-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel curl gdbm-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel >/tmp/phpyl.txt
useradd -s /sbin/nologin nginx
#安装libmcrypt-2.5.7
cd /opt
if [ ! -f "/opt/libmcrypt-2.5.7.tar.gz" ];then
    echo "libmcrypt-2.5.7.tar.gz 文件不存在，我们将在网上下载......"
    sleep 2
    cd /opt
    wget -q ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
    if [ $? -ne 0 ]; then
        echo ''
        echo "下载有问题，请检查......"
        exit
    fi
fi
tar -zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
#编译（默认安装到/usr/local/lib/）
./configure
#执行安装
make && make install
#解压php-7.1.26.tar.gz
cd /opt
tar zxf php-7.1.26.tar.gz
cd php-7.1.26/
#预编译
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/etc/conf.d --with-mcrypt=/usr/include --with-mysqli --with-pdo-mysql --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-soap --with-openssl --with-openssl-dir --with-pcre-regex --with-zlib --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --with-pcre-dir --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-gettext --with-gmp --with-mhash --enable-mbstring --with-libmbfl --with-onig --enable-pdo --with-zlib-dir --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd --without-pear -enable-tokenizer --enable-opcache
if [ $? -ne 0 ]; then
    echo "php 7.1.26 预编译有问题，请检查。"
    sleep 2
    exit
fi
#编译
make
#安装
make install
#拷贝文件：
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
cd /usr/local/php/etc/php-fpm.d
cp www.conf.default www.conf
cp /opt/php-7.1.26/php.ini-development /usr/local/php/etc/php.ini
cp -r /opt/php-7.1.26/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
#修改配置文件
sed -i 's#;pid = run/php-fpm.pid#pid = run/php-fpm.pid#g' /usr/local/php/etc/php-fpm.conf
sed -i 's#short_open_tag = Off#short_open_tag = On#g' /usr/local/php/etc/php.ini
chmod +x /etc/init.d/php-fpm
#配置开机启动
chkconfig --add /etc/init.d/php-fpm
chkconfig php-fpm on
systemctl start php-fpm
#判断php-fpm 是否正常启动
PSPHP1=`ps -ef |grep php-fpm |grep -v grep | wc -l`
if [ $PSPHP1 -lt 1 ]; then
    echo 'php-fpm 启动有问题，请检查。'
    exit
else
    netstat -luntp
    echo 'php-fpm 启动成功,程序将继续运行.'
    sleep 1
fi
fi
echo "开始安装 Nginx 1.15.9 ............................................."
sleep 2
################ 源码安装 nginx-1.15.9 ################################
PSNGINX0=`netstat -luntp |grep nginx |wc -l`
if [ $PSNGINX0 -ge 1 ]; then
    echo '已有nginx 进程运行，不需要再安装。'
    echo ''
    ps -ef |grep nginx | grep -v grep
    echo ''
    sleep 1
else
if [ ! -f "/opt/nginx-1.15.9.tar.gz" ];then
    echo "nginx-1.15.9.tar.gz 文件不存在，我们将在网上下载....."
    sleep 2
    cd /opt
    wget http://www.xchinagroup.top/softdown/centos7/02_nginx/nginx-1.15.9.tar.gz
    if [ $? -ne 0 ]; then
             echo ''
             echo "下载有问题，请检查......"
             exit
    fi 

fi
##安装依赖包
yum install gcc gcc-c++ pcre pcre-devel openssl openssl-devel ncurses-devel -y
cd /opt
tar zxf nginx-1.15.9.tar.gz
cd nginx-1.15.9/
./configure --with-http_ssl_module --with-stream --with-http_stub_status_module --prefix=/usr/local/nginx/
make
make install
#/usr/local/nginx/sbin/nginx
#配置nginx 由systemctl来管理。
#创建nginx.service
cd /usr/lib/systemd/system
touch nginx.service
chmod 754 nginx.service
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=True
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
#备份配置文件
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.$(date +%F%H%M%S)
#添加nginx 整合PHP代码
cat >/usr/local/nginx/conf/nginx.conf << EOF
worker_processes 4;
worker_rlimit_nofile 100000; #更改worker进程的最大打开文件数限制.
events {
worker_connections 409600;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nodelay on;
tcp_nopush on;
server_tokens off; #隐藏版本号。
keepalive_timeout 60;
gzip on;
gzip_disable "msie6"; #设置成IE6或者更低版本禁用gzip功能。
gzip_proxied any;
gzip_min_length 1000; #设置对数据启用压缩的最少字节数。
gzip_comp_level 6;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
try_files \$uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}
}
EOF
systemctl enable nginx
systemctl start nginx
##判断服务是否有启动成功。
if [ $? -ne 0 ]; then
    echo ''
    echo "nginx 未能启动成功，请检查....."
    exit
else
    netstat -luntp
    echo ''
    echo 'nginx-1.15.9 安装成功。'
    echo '---------------------------------------------------'
    echo ''
    sleep 1
    date
fi
fi
