#由于需要安装到nginx_http_concat_module和phalcon模块,需要安装git工具下载模块

#/bin/bash

#×××目录

SoftDir='/usr/local/src'

#定义安装日志路径

LOG_INS='/var/log/install.log'

#define make

function MAKE(){

if [ `echo $?` -eq 0 ];then

make >> $LOG_INS 2>&1

else

exit

fi

}

#define make install

function MAKE_INS(){

if [ `echo $?` -eq 0 ];then

make install >> $LOG_INS 2>&1

else

exit

fi

}

#安装系统运维包:

yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl openssl-devel gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel libmcrypt libmcrypt-devel libaio >> $LOG_INS 2>&1

#替换selinux 配置:

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

#=======================================        安装PHP 7.1.7          ====================================

#下载PHP 7.1.7

cd $SoftDir &&  wget http://cn2.php.net/distributions/php-7.1.7.tar.gz

#解压PHP:

tar -zxvf php-7.1.7.tar.gz

#添加php-fpm 用户,不允许登陆系统

useradd -s /sbin/nologin php-fpm

#进入PHP解压目录,配置编译参数:

cd /usr/local/src/php-7.1.7

./configure '--prefix=/usr/local/php' '--with-config-file-path=/usr/local/php/etc' '--with-config-file-scan-dir=/usr/local/php/etc/conf.d' '--enable-fpm' '--with-fpm-user=php-fpm' '--with-fpm-group=php-fpm' '--enable-soap' '--with-openssl' '--with-openssl-dir' '--with-mcrypt' '--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-pdo-mysql=mysqlnd' '--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' '--with-mysql-sock=/tmp/mysql.sock' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--without-pear'  >> $LOG_INS 2>&1

#编译及安装

MAKE && MAKE_INS

#复制Php 主配置:

cp php.ini-production /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

#创建php-fpm 主配置文件:

sed -i '1,$s#;pid = run/php-fpm.pid#pid = /usr/local/php/var/run/php-fpm.pid#g' /usr/local/php/etc/php-fpm.conf

sed -i '1,$s#;error_log = log/php-fpm.log#error_log = /usr/local/php/var/log/php-fpm.log#g' /usr/local/php/etc/php-fpm.conf

echo "include=/usr/local/php/etc/php-fpm.d/*.conf" >> /usr/local/php/etc/php-fpm.conf

cat <<EOF > /usr/local/php/etc/php-fpm.d/www.conf

[global]

pid = /usr/local/php/var/run/php-fpm.pid

error_log = /usr/local/php/var/log/php-fpm.log

[www]

listen = 127.0.0.1:9000

user = php-fpm

group = php-fpm

listen.owner = nginx

listen.group = nginx

pm = dynamic

pm.max_children = 50

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

pm.max_requests = 500

rlimit_files = 1024

EOF

#测试php-fpm 配置:

/usr/local/php/sbin/php-fpm -t

if [ `echo $?` -eq 0 ];then

echo "php-fpm installed successfully"

else

exit

fi

#拷贝php-fpm 启动脚本:

#cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

#centos 7 systemctl启动php-fpm

cp /usr/local/src/php-7.1.7/sapi/fpm/php-fpm.service /usr/lib/systemd/system/

systemctl enable php-fpm.service

systemctl start php-fpm.service

#赋予脚本可执行权限:

#chmod 755 /etc/init.d/php-fpm

#关闭php 版本信息:

sed -i 's/expose_php = On/expose_php = Off/g' /usr/local/php/etc/php.ini

#将php-fpm 写入随机启动:

#chkconfig --add php-fpm && chkconfig php-fpm on

#启动php-fpm

#service php-fpm start

#下载phalcon模块

cd $SoftDir && git clone https://github.com/dreamsxin/cphalcon7.git

cd cphalcon7/ext

#准备环境,配置编译参数

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config >> $LOG_INS 2>&1

#编译及安装

MAKE && MAKE_INS

#修改php.ini,增加扩展模块

mkdir /usr/local/php/etc/conf.d

echo "extension = phalcon.so" >> /usr/local/php/etc/conf.d/phalcon.ini

#重新加载配置

/usr/local/php/sbin/php-fpm -t

systemctl restart php-fpm.service

#/etc/init.d/php-fpm restart

#=======================================        安装nginx 1.12.1          ====================================

#下载

cd $SoftDir && wget http://nginx.org/download/nginx-1.12.1.tar.gz && git clone git://github.com/alibaba/nginx-http-concat.git

#解压:

tar zxf nginx-1.12.1.tar.gz

#创建nginx运行用户

useradd -s /sbin/nologin nginx

#进入解压目录,配置编译参数:

cd ./nginx-1.12.1 && ./configure --prefix=/usr/local/nginx --with-http_realip_module  --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --add-module=/usr/local/src/nginx-http-concat >> $LOG_INS 2>&1

#编译及安装

MAKE && MAKE_INS

#设置软链接

ln -s /usr/local/lib/libmaxminddb.so.0 /usr/lib64

ln -s /usr/local/lib/libprofiler.so.0 /usr/lib64

ln -s /usr/local/lib/libunwind.so.8 /usr/lib64

#检测初始化完成的nginx 配置是否有问题

/usr/local/nginx/sbin/nginx  -t

if [ `echo $?` -eq 0 ];then

echo "Nginx installed successfully!"

else

exit

fi

#centos 7创建nginx.service启动脚本

cat <<EOF > /usr/lib/systemd/system/nginx.service

[Unit]

Description=nginx

Documentation=http://nginx.org/en/docs/

After=network-online.target remote-fs.target nss-lookup.target

Wants=network-online.target

[Service]

Type=forking

PIDFile=/usr/local/nginx/logs/nginx.pid

ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s TERM $MAINPID

[Install]

WantedBy=multi-user.target

EOF

#创建nginx 启动脚本,centos 7采用systemctl进行启动,此处注释掉,备用。

#cat <<EOF > /etc/init.d/nginx

##!/bin/bash

## chkconfig: - 30 21

## description: http service.

## Source Function Library

#. /etc/init.d/functions

## Nginx Settings

#

#NGINX_SBIN="/usr/local/nginx/sbin/nginx"

#NGINX_CONF="/usr/local/nginx/conf/nginx.conf"

#NGINX_PID="/usr/local/nginx/logs/nginx.pid"

#RETVAL=0

#prog="Nginx"

#

#start() {

#        echo -n \$"Starting \$prog: "

#        mkdir -p /dev/shm/nginx_temp

#        daemon \$NGINX_SBIN -c \$NGINX_CONF

#        RETVAL=\$?

#        echo

#        return \$RETVAL

#}

#

#stop() {

#        echo -n \$"Stopping \$prog: "

#        killproc -p \$NGINX_PID \$NGINX_SBIN -TERM

#        rm -rf /dev/shm/nginx_temp

#        RETVAL=\$?

#        echo

#        return \$RETVAL

#}

#

#reload(){

#        echo -n \$"Reloading \$prog: "

#        killproc -p \$NGINX_PID \$NGINX_SBIN -HUP

#        RETVAL=\$?

#        echo

#        return \$RETVAL

#}

#

#restart(){

#        stop

#        start

#}

#

#configtest(){

#    \$NGINX_SBIN -c \$NGINX_CONF -t

#    return 0

#}

#

#case "\$1" in

#  start)

#        start

#        ;;

#  stop)

#        stop

#        ;;

#  reload)

#        reload

#        ;;

#  restart)

#        restart

#        ;;

#  configtest)

#        configtest

#        ;;

#  *)

#        echo $"Usage: \$0 {start|stop|reload|restart|configtest}"

#        RETVAL=1

#esac

#

#exit \$RETVAL

#EOF

#赋予脚本可执行权限

#chmod 755 /etc/init.d/nginx

#将nginx加入到开机启动

systemctl enable nginx.service

#备份nginx主配置文件

\cp /usr/local/nginx/conf/nginx.conf{,.bak}

#重新nginx 配置:

cat << EOF > /usr/local/nginx/conf/nginx.conf

user nginx nginx;

error_log /var/log/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

worker_processes  auto;

worker_rlimit_nofile 65535;

events {

use epoll;

worker_connections  65535;

}

http {

include      mime.types;

default_type  application/octet-stream;

sendfile        on;

keepalive_timeout  65;

server_tokens off;

charset utf-8;

tcp_nopush on;

tcp_nodelay on;

gzip on;

gzip_min_length 100;

gzip_buffers 4 16k;

gzip_http_version 1.0;

gzip_comp_level 6;

gzip_types text/plain application/x-javascript text/css application/xml;

gzip_vary on;

server {

listen      80;

server_name  localhost;

location / {

root  html;

index  index.html index.htm index.php;

}

error_page  500 502 503 504  /50x.html;

location = /50x.html {

root  html;

}

location ~ \.php\$ {

root          html;

fastcgi_pass  127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;

include        fastcgi_params;

}

}

}

EOF

#创建PHP测试文件:

echo "<?php  echo phpinfo();  ?>" >/usr/local/nginx/html/index.php

#备份index.html:

mv /usr/local/nginx/html/index.html{,.bak}

#启动nginx:

#service nginx start

systemctl start nginx.service

#将nginx 写入随机启动:

#chkconfig --add nginx && chkconfig nginx on

#开启防火墙的80端口

#iptables -I INPUT -p tcp --dport 80 -j ACCEPT

#清空防火墙配置

iptables -F

#重启防火墙:

#systemctl restart firewalld.service

#设置nginx、mysql 及php 的环境变量:

echo "PATH=$PATH:/usr/local/php/bin:/usr/local/nginx/sbin:/usr/local/mysql/bin:/usr/local/php/sbin" >> /etc/profile

#刷新环境变量配置,使其立即生效:

source /etc/profile

#执行完成后,删除开机启动执行该脚本

if  [ `echo $?` -eq 0 ]; then

sed -i 'php_nginx.sh'd /etc/rc.d/rc.local

else

exit

fi

###########################添加开机启动执行脚本#################################

echo "sh /script/php_nginx.sh" >> /etc/rc.d/rc.local