!/bin/bash
#关闭防火墙与SELINUX
systemctl stop firewalld
systemctl disable firewalld
sed -i '/^SELINUX/ s/enforcing/disabled/g' /etc/sysconfig/selinux
setenforce 0

#配置yum源
cd /etc/yum.repos.d/
curl -o 163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
sed -i 's/\$releasever/7/g' /etc/yum.repos.d/163.repo
sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/163.repo
yum -y install epel-release
yum -y install wget
yum -y install gcc gcc-c++

#安装依赖环境
yum -y install pcre-devel openssl openssl-devel gd-devel
yum -y groups install 'Development Tools'

#安装nginx
id nginx
if [ $? -ne 0 ];then
groupadd -r nginx
useradd -r -M -s /sbin/nologin -g nginx nginx
fi

#创建日志存放目录
if [ -d /var/log/ninx ];then
mkdir -p /var/log/nginx
fi
chown -R nginx.nginx /var/log/nginx
#下载nginx
cd /usr/src
#wget  wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar xf nginx-1.14.0.tar.gz
cd nginx-1.14.0
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log

make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

#配置变量环境
echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
. /etc/profile.d/nginx.sh
#启动nginx
nginx
ss -antl

#安装mysql
#创建用户和组
id mysql
if [ $? -ne 0 ];then
groupadd -r -g 306 mysql
useradd -M -s /sbin/nologin -g 306 -u 306 mysql
fi
#下载二进制格式的mysql软件包
cd /usr/src
#wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

#解压
tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd  /usr/local/
ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
chown -R mysql.mysql /usr/local/mysql
#添加环境变量
echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
echo $PATH

#建立数据存放目录
cd /usr/local/mysql
if [ -d "/opt/data" ];then
mkdir /opt/data
fi
chown -R mysql.mysql /opt/data/
#初始化数据库
/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/ &> /var/log/mysql.log
temp_password=$(grep 'password' /var/log/mysql.log | awk '{print $NF}')

ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig -v

#生成配置文件
cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF

#配置服务启动脚本
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
#启动mysql
service mysqld start
ss -antl
ps -ef|grep mysql

#修改密码
mysql -uroot -p"$temp_password" --connect-expired-password -e 'set password=password("123456");'

#安装php
#安装依赖包
yum -y install epel-release
yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel

#下载php
cd /usr/src
#wget http://cn.php.net/distributions/php-7.2.8.tar.xz

#编译安装php
tar xf php-7.2.8.tar.xz
cd php-7.2.8/
./configure --prefix=/usr/local/php7 \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir=/usr \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-jpeg-dir \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--with-bz2 \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip

make -j $(cat /proc/cpuinfo |grep processor|wc -l) && make install

#安装后配置
echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
source /etc/profile.d/php7.sh
php -v

#配置php-fpm
cd /usr/src
cd php-7.2.8
cp php.ini-production /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

#编辑php-fpm的配置文件
cat >> /usr/local/php7/etc/php-fpm.conf << 'EOF'
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
EOF

#启动php-fpm
service php-fpm start
ss -antl
ps -ef | grep php

#配置nginx
\cp /usr/local/nginx/conf/nginx.conf{,-bak}
cat > /usr/local/nginx/conf/nginx.conf <<'EOF'
user  nginx;
worker_processes  4;

error_log  logs/error.log;

pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

    #gzip  on;

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
                      index  index.php index.html index.htm;
        }

#error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
              }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {

#    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #
        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;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
EOF

/usr/local/nginx/sbin/nginx -s reload
ps -ef | grep nginx

cd /usr/local/nginx/html/
touch test.php
cat > test.php << EOF
<?php
phpinfo();
?>
EOF

ss -antl