商城搭建

环境

[root@w1 keepalived-2.0.7]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@w1 keepalived-2.0.7]# uname -a
Linux w1 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
#关闭selinux和iptables

安装依赖

yum -y install libxml2 libxml2-devel gcc gcc-c++  zlib-devel openssl openssl-devel libcurl-devel libcurl  ncurses  libjpeg-devel libpng  libpng-devel  freetype freetype-devel openldap openldap-devel.x86_64 zip unzip wget GeoIP GeoIP-data GeoIP-devel autoconf autoconf

安装tengine

#!/bin/bash
#安装tengine脚本
BASEDIR=/opt/src  #下载的资源保存目录
SOFTDIR=/opt/webserver      #软件安装目录
if [ ! -d $BASEDIR ];then
mkdir -p $BASEDIR
fi
if [ ! -d $SOFTDIR ];then
mkdir -p $SOFTDIR
fi
cd $BASEDIR
#创建用户www
if [ 'grep "www" /etc/passwd | wc -l' ]; then
groupadd www >/dev/null 2>&1
useradd -s /sbin/nologin -M -g www www  >/dev/null 2>&1
fi
#判断目录/var/lib/nginx/client/是否存在,不存在则创建
if [ ! -d /var/lib/nginx/client/ ];then
mkdir -p /var/lib/nginx/client/
fi
#判断目录/var/lib/nginx/client/是否存在,不存在则创建
if [ ! -d /data/proxy/proxy_temp_dir ];then
mkdir -p /data/proxy/proxy_temp_dir 
fi
#下载相关的模块,并解压
#############LuaJIT-2.0.5###########
if [ ! -f LuaJIT-2.0.5.tar.gz ] ;then
wget -c http://luajit.org/download/LuaJIT-2.0.5.tar.gz >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "file download failed , please check out "
exit 1
fi
fi
tar -xf LuaJIT-2.0.5.tar.gz >/dev/null 2>&1
cd LuaJIT-2.0.5
#判断make是否出错,如果出错,则退出脚本
make >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "make failed ,please check it out!"
exit 1
fi
#判断make install是否出错,如果出错退出脚本
make install >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "make failed ,please check it out!"
exit 1
fi
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
if [ ! -d /opt/src ];then
mkdir -p /opt/src
fi
cd $BASEDIR
#############v0.3.1rc1###########
if [ ! -f v0.3.1rc1.tar.gz ] ;then
wget -c https://github.com/simpl/ngx_devel_kit/archive/v0.3.1rc1.tar.gz >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "file download failed , please check out "
exit 1
fi
fi
tar -xf v0.3.1rc1.tar.gz >/dev/null 2>&1
#############v0.10.12rc2###########
if [ ! -f v0.10.12rc2.tar.gz ];then
wget -c https://github.com/openresty/lua-nginx-module/archive/v0.10.12rc2.tar.gz >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "file download failed , please check out "
exit 1
fi
fi
tar -xf v0.10.12rc2.tar.gz
#############zlib-1.2.11###########
if [ ! -f zlib-1.2.11.tar.gz ];then
wget -c  http://www.zlib.net/zlib-1.2.11.tar.gz >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "file download failed , please check out "
exit 1
fi
fi
tar -xf zlib-1.2.11.tar.gz >/dev/null 2>&1
#安装epel 
rpm -ivh   http://ftp.linux.ncsu.edu/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm >/dev/null 2>&1
#############pcre-8.37###########
if [ ! -f pcre-8.37.tar.gz ];then
wget -c http://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "file download failed , please check out "
exit 1
fi
tar -xf pcre-8.37.tar.gz >/dev/null 2>&1
fi
cd pcre-8.37
./configure >/dev/null 2>&1
#判断configure是否出现问题
if [ $? -ne 0 ];then
echo "configure failed ,please check it out!"
exit 1
fi
#判断make是否出错,如果出错,则退出脚本
make >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "make failed ,please check it out!"
exit 1
fi
#判断make install是否出错,如果出错退出脚本
make install >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "make failed ,please check it out!"
exit 1
fi
cd $BASEDIR
#############openssl1.0.2###########
if [ ! -f  openssl-1.0.2c.tar.gz ];then
wget -c  https://ftp.openssl.org/source/old/1.0.2/openssl-1.0.2c.tar.gz >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "file download failed , please check out "
exit 1
fi
fi
tar -xf openssl-1.0.2c.tar.gz >/dev/null 2>&1
##################master#############
if [ ! -f master ];then
wget -c https://codeload.github.com/gnosek/nginx-upstream-fair/zip/master >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "file download failed , please check out "
exit 1
fi
fi
unzip master >/dev/null 2>&1
##############2.3.tar.gz##############
if [ ! -f  2.3.tar.gz ];then
wget -c https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "file download failed , please check out "
exit 1
fi
fi
tar -xf 2.3.tar.gz >/dev/null 2>&1
##############tengine-2.2.2##############
if [ ! -f tengine-2.2.2.tar.gz ];then
wget -c http://tengine.taobao.org/download/tengine-2.2.2.tar.gz >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "file download failed , please check out "
exit 1
fi
fi
tar -xf tengine-2.2.2.tar.gz >/dev/null 2>&1
cd $BASEDIR/tengine-2.2.2
./configure \
--prefix=$SOFTDIR/nginx \
--conf-path=$SOFTDIR/nginx/etc/nginx.conf \
--error-log-path=$SOFTDIR/nginx/logs/error.log \
--pid-path=/var/run/nginx/nginx.pid  \
--lock-path=/var/lock/nginx.lock \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_concat_module \
--with-http_geoip_module \
--http-log-path=$SOFTDIR/nginx/logs/access.log \
--http-client-body-temp-path=/var/lib/nginx/client/ \
--http-proxy-temp-path=/var/lib/nginx/proxy/ \
--http-fastcgi-temp-path=/var/lib/nginx/fcgi/  \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi/  \
--http-scgi-temp-path=/var/lib/nginx/scgi/  \
--with-openssl=/opt/src/openssl-1.0.2c \
--with-zlib=$BASEDIR/zlib-1.2.11 \
--add-module=$BASEDIR/nginx-upstream-fair-master \
--add-module=$BASEDIR/ngx_cache_purge-2.3 \
--add-module=$BASEDIR/lua-nginx-module-0.10.12rc2 \
--add-module=$BASEDIR/ngx_devel_kit-0.3.1rc1 \
--with-pcre=$BASEDIR/pcre-8.37  >/dev/null 2>&1
#判断configure是否出现问题
if [ $? -ne 0 ];then
echo "configure failed ,please check it out!"
exit 1
fi
#判断make是否出错,如果出错,则退出脚本
make  >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "make failed ,please check it out!"
exit 1
fi
#判断make install是否出错,如果出错退出脚本
make install >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "make failed ,please check it out!"
exit 1
fi
#建立软连接
if [ -L /lib64/libluajit-5.1.so.2 ] ;then
rm -f /lib64/libluajit-5.1.so.2
fi
ln  -s /usr/local/lib/libluajit-5.1.so.2  /lib64/libluajit-5.1.so.2 >/dev/null 2>&1
echo " ------------Success install tengine ------------------------"

nginx.conf配置

#nginx.conf  配置
user www www;
worker_rlimit_nofile 65535;
error_log   logs/error.log;
pid     /var/run/nginx.pid;

events {
    use epoll;
    worker_connections 65535;
    #设置nginx收到一个新连接通知后接受尽可能多的连接
    multi_accept on ;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #设置日志格式
    log_format  access_ws  '$http_Cdn_Src_Ip - $remote_addr - $remote_user [$time_local] "$request_method http://$http_host$uri?$query_string $server_protocol" '
            '$status $body_bytes_sent "$http_referer" $request_time $upstream_addr $upstream_response_time "$http_user_agent"';
    log_format  access  '$http_x_forwarded_for - $remote_addr - $remote_user [$time_local] "$request_method http://$http_host$uri?$query_string $server_protocol" '
            '$status $body_bytes_sent "$http_referer" $request_time $upstream_addr $upstream_response_time "$http_user_agent"';
    log_format access_json '{"@timestamp":"$time_iso8601",'
    '"host":"$server_addr",'
    '"clientip":"$http_x_forwarded_for",'
    '"size":$body_bytes_sent,'
    '"responsetime":$request_time,'
    '"upstreamtime":"$upstream_response_time",'
    '"upstreamhost":"$upstream_addr",'
    '"http_host":"$host",'
    '"url":"$uri",'
    '"domain":"$host",'
    '"xff":"$http_x_forwarded_for",'
    '"referer":"$http_referer",'
    '"agent":"$http_user_agent",'
    '"method":"$request_method",'
    '"status":"$status"}';
    #设置总体的http的log
    access_log  logs/access.log;

    sendfile        on;
    tcp_nopush     on;
    #开启异步
    tcp_nodelay on ;
    #开启错误页面跳转,跳转在server里面配置
    fastcgi_intercept_errors on;
    client_header_buffer_size 4k;
    large_client_header_buffers 4 16k;
    #隐藏版本号
    server_tokens off;
    #头伪装 仅限于淘宝的tengine
    server_tag Microsoft-IIS/8.5;
    #限制并发连接
    limit_req_zone $http_x_forwarded_for zone=user:30m rate=30r/s;
    #客户端链接超时时间
    keepalive_timeout  65;
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    #指定压缩级别
    gzip_comp_level 9;
    #指定压缩类型
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    #加vary头
    gzip_vary on;

    #引入虚拟主机
    include vhost.d/*.conf;
    #引入代理主机
    include proxy.d/*.conf;
    # include /home/wwwroot/.htaccess;

    client_header_timeout 30;
    client_body_timeout 30;
}

虚拟主机配置

在nginx.onf的同级目录下创建虚拟主机配置目录vhosts,创建web1.conf  web2.conf  zabbix.conf
以不同端口配置虚拟主机
[root@www1 vhosts]# cat /opt/webserver/etc/vhost.d/web1.conf 
server {
    listen 80;
    server_name  vcom.shop.mobikylin.com;

    listen 443;
        ssl_certificate   /root/.acme.sh/vcom.shop.mobikylin.com/vcom.shop.mobikylin.com.cer;
        ssl_certificate_key  /root/.acme.sh/vcom.shop.mobikylin.com/vcom.shop.mobikylin.com.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        if ($server_port = 80 ) {
                return 301 https://$host$request_uri;
        }

          location  / {
            proxy_pass http://vcom_server;
            proxy_set_header X-Real-IP $remote_addr;
            access_log  logs/shop.access.log;
            error_log  logs/shop.error.log;
        }    

        location /status
        {
            stub_status on;
            access_log off;
            allow 172.16.17.229;
            allow 127.0.0.1;
            deny all;
        }
    }

代理主机配置

upstream vcom_server    {
        server 172.16.17.227:80 weight=4 max_fails=2 fail_timeout=30s;
        server 172.16.17.228:80 weight=4 max_fails=2 fail_timeout=30s;
}

nginx启动脚本

#! /bin/bash
# Source function library.
. /etc/init.d/functions
# Progran name
prog="nginx"
start() {
      echo -n $"Starting $prog: "
        if [ -e /var/lock/nginx.lock ]; then
            if [ -e /var/run/nginx.pid ] && [ -e /proc/`cat /var/run/nginx.pid ` ]; then
            echo -n $"cannot start $prog: nginx is already running."
            failure $"cannot start $prog: nginx is already running."
            echo
            return 1
        fi
    fi
    /opt/webserver/nginx/sbin/nginx
    RETVAL=$?
    [ $RETVAL -eq 0 ] && success $"$prog start" || failure $"$prog start"
    [ $RETVAL -eq 0 ] && touch /var/lock/nginx.lock
    echo
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    if [ ! -e /var/lock/nginx.lock ] || [ ! -e /var/run/nginx.pid  ]; then
        echo -n $"cannot stop $prog: nginx is not running."
        failure $"cannot stop $prog: nginx is not running."
        echo
        return 1
    fi
    PID=`cat /var/run/nginx.pid `
    if checkpid $PID 2>&1; then
        # TERM first, then KILL if not dead
        kill -TERM $PID >/dev/null 2>&1
        usleep 100000
        if checkpid $PID && sleep 1 && checkpid $PID && sleep 3 && checkpid $PID; then
            kill -KILL $PID >/dev/null 2>&1
            usleep 100000
        fi
    fi
    checkpid $PID
    RETVAL=$((! $?))
    [ $RETVAL -eq 0 ] && success $"$prog shutdown" || failure $"$prog shutdown"
    [ $RETVAL -eq 0 ] && rm -f /var/lock/nginx.lock;
    echo
    return $RETVAL
}

status() {
    status $prog
    RETVAL=$?
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $prog: "
    if [ ! -e /var/lock/nginx.lock ] || [ ! -e /var/run/nginx.pid  ]; then
        echo -n $"cannot reload $prog: nginx is not running."
        failure $"cannot reload $prog: nginx is not running."
        echo
        return 1
    fi
    kill -HUP `cat /var/run/nginx.pid ` >/dev/null 2>&1
    RETVAL=$?
    [ $RETVAL -eq 0 ] && success $"$prog reload" || failure $"$prog reload"
    echo
    return $RETVAL
}

case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    restart
    ;;
reload)
    reload
    ;;
status)
    status
    ;;
link)
    netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
    ;;
ps)
    ps -ef | grep nginx | wc -l
    ;;
condrestart)
    [ -f /var/lock/nginx.lock ] && restart || :
    ;;
t)
    /opt/webserver/nginx/sbin/nginx -t 
    ;;
configtest)
       /opt/webserver/nginx/sbin/nginx -t
      ;;
 *)
    echo $"Usage: $0 {start|stop|status|reload|restart|condrestart|configtest|link|ps}"
    exit 1
esac

nginx启动脚本设置

#启动脚本放在/etc/init.d下,添加权限
chmod +x /etc/init.d/nginx
sed -i 's/\r//g' /etc/init.d/nginx  
ln -s /etc/init.d/nginx  /usr/bin
#启动nginx,两种方式启动
nginx start    或者    service nginx start 

安装redis主从

redis安装配置脚本

#!/bin/bash
BASEDIR=/opt/src  #下载的资源保存目录
SOFTDIR=/opt/webserver      #软件安装目录
#密码和端口根据需要修改
REDIS_PASS=123qwe 
REDIS_PORT1=6379
REDIS_PORT2=6380
if [ ! -d $BASEDIR ];then
  mkdir -p $BASEDIR
fi
if [ ! -d $SOFTDIR ];then
  mkdir -p $SOFTDIR
fi
REDIS_VERSION=redis-4.0.11
REDIS_URL=http://download.redis.io/releases/$REDIS_VERSION.tar.gz
#判断安装的包是否存在,如果指定的SOFTDIR里面不存在相应的包则下载
cd $BASEDIR
if [ ! -f $REDIS_VERSION.tar.gz ];then
    wget $REDIS_URL >/dev/null 2>&1
    if [ $? -ne 0 ];then
        echo "file download failed , please check out "
        exit 1
    fi
fi
if [ -d $REDIS_VERSION ];then
    rm -rf $REDIS_VERSION
fi
tar -xf $REDIS_VERSION.tar.gz >/dev/null 2>&1
cd $REDIS_VERSION
make distclean >/dev/null 2>&1
make >/dev/null 2>&1
if [ $? -ne 0 ];then
    echo " make failed , please check out "
    exit 1
fi
#创建redis的启动脚本目录
if [  -d $SOFTDIR/redis ];then
    rm -rf  $SOFTDIR/redis/*    
fi
mkdir -p  $SOFTDIR/redis/bin
mkdir -p  $SOFTDIR/redis/conf
mkdir -p  $SOFTDIR/redis/logs
touch $SOFTDIR/redis/logs/$REDIS_PORT1.log 
touch $SOFTDIR/redis/logs/$REDIS_PORT2.log
#创建可执行文件
cp -a $BASEDIR/$REDIS_VERSION/src/redis* /$SOFTDIR/redis/bin
rm -rf $SOFTDIR/redis/bin/*.o 
rm -rf $SOFTDIR/redis/bin/*.h
rm -rf $SOFTDIR/redis/bin/*.c 
chmod +x $SOFTDIR/redis/bin/*
rm -rf /usr/bin/redis* 
ln -s $SOFTDIR/redis/bin/* /usr/bin 
#创建redis $REDIS_PORT1 端口配置文件,作为主配置文件
if [ ! -f $SOFTDIR/redis/conf/REDIS_PORT1.conf ];then
    cp -a $BASEDIR/$REDIS_VERSION/redis.conf    $SOFTDIR/redis/conf/$REDIS_PORT1.conf
    sed -i 's/daemonize no/daemonize yes/g' $SOFTDIR/redis/conf/$REDIS_PORT1.conf
    sed -i 's/dbfilename dump.rdb/dbfilename dump'$REDIS_PORT1'.rdb/g' $SOFTDIR/redis/conf/$REDIS_PORT1.conf
    sed -i 's/# requirepass foobared/requirepass '$REDIS_PASS'/g' $SOFTDIR/redis/conf/$REDIS_PORT1.conf
    sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g' $SOFTDIR/redis/conf/$REDIS_PORT1.conf
    sed -i 's/logfile ""/logfile "logs\/'$REDIS_PORT1'.log"/g' $SOFTDIR/redis/conf/$REDIS_PORT1.conf
fi
#创建redis $REDIS_PORT2 端口的服务器,做为从服务器
if [ ! -f $SOFTDIR/redis/conf/REDIS_PORT2.conf ];then
    cp -a $SOFTDIR/redis/conf/$REDIS_PORT1.conf $SOFTDIR/redis/conf/$REDIS_PORT2.conf
    sed -i 's/'$REDIS_PORT1'/'$REDIS_PORT2'/g' $SOFTDIR/redis/conf/$REDIS_PORT2.conf
    echo slaveof 127.0.0.1 $REDIS_PORT1 >>$SOFTDIR/redis/conf/$REDIS_PORT2.conf
    echo masterauth $REDIS_PASS >> $SOFTDIR/redis/conf/$REDIS_PORT2.conf
fi
num=`grep "vm.overcommit_memory = 1" /etc/sysctl.conf |wc -l` 
if  [ $num -ne 1 ];then
    echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
    sysctl -p >/dev/null 2>&1
fi
cd $SOFTDIR/redis/
./bin/redis-server conf/$REDIS_PORT1.conf >/dev/null 2>&1
if [ $? -ne 0 ];then
    echo $REDIS_PORT1启动失败
    exit 1
fi 
./bin/redis-server conf/$REDIS_PORT2.conf >/dev/null 2>&1
if [ $? -ne 0 ];then
    echo $REDIS_PORT2启动失败
    exit 1
fi 
echo "成功安装并启动redis主从,密码123qwe, 端口主6379,从6380。。。。"

redis报错解决:

25451:signal-handler (1536400042) Received SIGTERM scheduling shutdown...
25451:S 08 Sep 17:47:22.227 # User requested shutdown...
25451:S 08 Sep 17:47:22.228 # Redis is now ready to exit, bye bye...
25516:C 08 Sep 17:48:24.201 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
25516:C 08 Sep 17:48:24.201 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=25516, just started
25516:C 08 Sep 17:48:24.201 # Configuration loaded
25517:S 08 Sep 17:48:24.203 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
25517:S 08 Sep 17:48:24.203 # Server initialized
25517:S 08 Sep 17:48:24.204 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
25517:S 08 Sep 17:48:24.204 # Error condition on socket for SYNC: Connection refused
25517:S 08 Sep 17:48:25.224 # Error condition on socket for SYNC: Connection refused
25517:S 08 Sep 17:48:26.243 # Error condition on socket for SYNC: Connection refused
25517:S 08 Sep 17:48:27.267 # Error condition on socket for SYNC: Connection refused
25517:S 08 Sep 17:48:28.282 # Error condition on socket for SYNC: Connection refused
25517:S 08 Sep 17:48:29.310 # Error condition on socket for SYNC: Connection refused
25517:S 08 Sep 17:48:30.326 # Error condition on socket for SYNC: Connection refused
25517:S 08 Sep 17:48:31.345 # Error condition on socket for SYNC: Connection refused

解决办法:
[root@www1 redis]# echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
[root@www1 redis]# sysctl vm.overcommit_memory=1
[root@www1 redis]# echo "1280"> /proc/sys/net/core/somaxconn 
[root@www1 redis]# echo ulimit -n 65535 >>/etc/profile   

安装php

php安装脚本

#!/bin/bash
BASEDIR=/opt/src  #下载的资源保存目录
SOFTDIR=/opt/webserver      #软件安装目录
if [ 'grep "www" /etc/passwd | wc -l' ]; then
    groupadd www  >/dev/null 2>&1
    useradd -s /sbin/nologin -M -g www www  >/dev/null 2>&1
fi
if [ -s /usr/lib/libldap.so ];then
    ln -s /usr/lib64/libldap.so /usr/lib/ >/dev/null 2>&1
fi
if [ ! -d $BASEDIR ];then
    mkdir -p $BASEDIR
fi
if [ ! -d $SOFTDIR ];then
    mkdir -p $SOFTDIR
fi
cd $BASEDIR
if [ ! -f php-7.2.9.tar.bz2 ] ;then
    wget http://cn2.php.net/distributions/php-7.2.9.tar.bz2 >/dev/null 2>&1
    if [ $? -ne 0 ];then
        echo "download failed ,please check it out!" 
        exit 1
    fi
fi
tar -xf php-7.2.9.tar.bz2  >/dev/null 2>&1
cd php-7.2.9
./configure --prefix=$SOFTDIR/php --exec-prefix=$SOFTDIR/php \
--sbindir=$SOFTDIR/php/sbin --includedir=$SOFTDIR/php/include \
--libdir=$SOFTDIR/php/lib/php --mandir=$SOFTDIR/php/php/man \
--with-config-file-path=$SOFTDIR/php/etc --with-mhash --with-openssl \
--with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gd --with-iconv \
--with-zlib --enable-zip --enable-inline-optimization \
--disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath \
--enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-pcntl \
--enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext \
--enable-session --with-curl --with-jpeg-dir --with-freetype-dir \
--enable-opcache --enable-fpm --with-fpm-user=www \
--with-fpm-group=www --without-gdbm --enable-fast-install --disable-fileinfo  >/dev/null 2>&1
if [ $? -ne 0 ];then
    echo "configure failed ,please check it out!"
    exit 1
fi
#echo "make php-7, please wait for 20 minutes"
make  >/dev/null 2>&1
if [ $? -ne 0 ];then
    echo "make failed ,please check it out!"
    exit 1
fi
make install >/dev/null 2>&1
if [ $? -ne 0 ];then
    echo "install failed ,please check it out!"
    exit 1
fi
##########创建配置文件php.ini##############
if [ ! -f $SOFTDIR/etc/php.ini ];then
    cp $BASEDIR/php-7.2.9/php.ini-production $SOFTDIR/php/etc/php.ini
fi
#创建配置文件php-fpm.ini
cd $SOFTDIR/php/etc
if [ ! -f php-fpm.conf ];then
    cp php-fpm.conf.default php-fpm.conf
fi
#创建配置文件www.conf
cd php-fpm.d/
if [ ! -f www.conf ];then
    cp www.conf.default www.conf
fi
#清空配置文件,加入自定义的一些配置
echo "">$SOFTDIR/php/etc/php-fpm.d/www.conf
cat <<EOF >$SOFTDIR/php/etc/php-fpm.d/www.conf
[www]
user = www
group = www
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 20
pm.start_servers=10
pm.min_spare_servers=10
pm.max_spare_servers=20
EOF
chmod +x $SOFTDIR/php/sbin/php-fpm 
rm -rf /usr/bin/php-fpm
cp -a $SOFTDIR/php/sbin/php-fpm  /etc/init.d/
ln -s /etc/init.d/php-fpm  /usr/bin  
pkill php-fpm
/opt/webserver/php/sbin/php-fpm
echo "------------------Success install php7.2.9--------------------"

php启动脚本

#! /bin/sh

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

prefix=/opt/webserver/php
exec_prefix=${prefix}

php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
wait_for_pid () {
    try=0
    while test $try -lt 35 ; do

        case "$1" in
            'created')
            if [ -f "$2" ] ; then
                try=''
                break
            fi
            ;;
            'removed')
            if [ ! -f "$2" ] ; then
                try=''
                break
            fi
            ;;
        esac
        echo -n .
        try=`expr $try + 1`
        sleep 1
    done
}
case "$1" in
    start)
        echo -n "Starting php-fpm "
        $php_fpm_BIN --daemonize $php_opts
        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        fi
        wait_for_pid created $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            # chmod 777 /tmp/php-fpm.sock
            echo " done"
        fi
    ;;
    stop)
        echo -n "Gracefully shutting down php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
        kill -QUIT `cat $php_fpm_PID`
        wait_for_pid removed $php_fpm_PID
        if [ -n "$try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;

    status)
        if [ ! -r $php_fpm_PID ] ; then
            echo "php-fpm is stopped"
            exit 0
        fi
        PID=`cat $php_fpm_PID`
        if ps -p $PID | grep -q $PID; then
            echo "php-fpm (pid $PID) is running..."
        else
            echo "php-fpm dead but pid file exists"
        fi
    ;;
    force-quit)
        echo -n "Terminating php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -TERM `cat $php_fpm_PID`
        wait_for_pid removed $php_fpm_PID
        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
    restart)
        $0 stop
        $0 start
    ;;
    reload)
        echo -n "Reload service php-fpm "
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
        kill -USR2 `cat $php_fpm_PID`
        echo " done"
    ;;
    configtest)
        $php_fpm_BIN -t
    ;;
    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
        exit 1
    ;;

esac

php启动脚本设置

#启动脚本放在/etc/init.d下,添加权限
chmod +x /etc/init.d/php-fpm
sed -i 's/\r//g' /etc/init.d/php-fpm  
ln -s /etc/init.d/php-fpm  /usr/bin
#启动nginx,两种方式启动
php-fpm start    或者  service php-fpm start 

php的扩展插件安装

#安装php-redis
wget https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz
tar -xf 3.1.4.tar.gz
cd phpredis-3.1.4
/opt/webserver/php/bin/phpize
./configure --with-php-config=/opt/webserver/php/bin/php-config
make && make install
echo  "extension=redis.so" >>/opt/webserver/php/etc/php.ini

#安装yaconf
wget https://codeload.github.com/laruence/yaconf/zip/master
unzip master
cd yaconf-master
/opt/webserver/php/bin/phpize
./configure --with-php-config=/opt/webserver/php/bin/php-config
make && make install
echo  "extension=yaconf.so" >>/opt/webserver/php/etc/php.ini

#安装apcu
wget https://codeload.github.com/krakjoe/apcu/zip/master
unzip master
apcu-master
/opt/webserver/php/bin/phpize
./configure --with-php-config=/opt/webserver/php/bin/php-config
make && make install

vim /opt/webserver/php/etc/php.ini
    extension=apcu.so
    # 开启APC
    apc.enabled=1
    # 分配给APC的内存
    apc.shm_size=32M
    # 缓存条目允许在婚宠去内逗留的秒数,0表示永久,建议值为7200~86400
    apc.ttl=7200
    # 是否为CLI版本启用APC功能,仅用于测试和调试目的才开启此选项。
    apc.enable_cli=1

安装mysql5.7

安装脚本

#!/bin/bash
BASEDIR=/opt/src 
SOFTDIR=/opt/webserver
MYSQL_DATA_PAHT=/home/data/mysql 
#添加mysql用户
if [ 'grep "mysql" /etc/passwd | wc -l' ]; then
    groupadd -r mysql >/dev/null 2>&1
    useradd -r -s /sbin/nologin -M -g mysql mysql >/dev/null 2>&1
fi
#判断安装目录是否存在
if [ ! -d $BASEDIR ];then
    mkdir -p $BASEDIR
fi
if [ ! -d $SOFTDIR ];then
    mkdir -p $SOFTDIR
fi
#删除原来安装的目录,重新安装
if [  -d  $MYSQL_DATA_PAHT ];then
    rm  -rf $MYSQL_DATA_PAHT/*  
fi
#删除曾经安装的mysqld的启动文件
if [ -f /usr/bin/mysqld ];then
    rm -rf /usr/bin/mysqld 
fi 
#删除默认安装的mysql配置文件
if [ -f /etc/my.cnf ] ;then
    rm -rf /etc/my.cnf
fi 
if [ -f /etc/init.d/mysqld ] ;then
    rm -rf /etc/init.d/mysqld
fi 
mkdir -p $MYSQL_DATA_PAHT
chown -R mysql:mysql $MYSQL_DATA_PAHT
cd $BASEDIR
#判断安装的tar包是否存在,不存在则下载
if [ ! -f mysql-5.7.23-linux-glibc2.12-x86_64.tar ];then
    wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar  >/dev/null 2>&1
    if [  $? -ne 0 ];then
        echo "Downloads mysql failed, please check out ..."
        exit 1
    fi   
fi    
#解压文件,并放置到指定的目录
tar -xf mysql-5.7.23-linux-glibc2.12-x86_64.tar >/dev/null 2>&1
if [  $? -ne 0 ];then
    echo "解压mysql-5.7.23-linux-glibc2.12-x86_64.tar失败 "
    exit 1
fi
tar -xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz  >/dev/null 2>&1
if [  $? -ne 0 ];then
    echo "解压mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz失败 "
    exit 1
fi
if [ -d $SOFTDIR/mysql ];then
    rm -rf $SOFTDIR/mysql
fi
mv mysql-5.7.23-linux-glibc2.12-x86_64 $SOFTDIR/mysql/ 
source  /etc/profile >/dev/null 2>&1
cat << EOF > /etc/my.cnf
[client]
socket=$SOFTDIR/mysql/mysql.sock
[mysqld]
user=mysql
basedir=$SOFTDIR/mysql/
datadir=$MYSQL_DATA_PAHT
socket=$SOFTDIR/mysql/mysql.sock
pid-file=$SOFTDIR/mysql/mysqld.pid
log-error=$SOFTDIR/mysql/mysql.err
log-bin=mysql-bin
server-id=1
EOF
cp $SOFTDIR/mysql/support-files/mysql.server /etc/init.d/mysqld  
chmod +x /etc/init.d/mysqld
ln -s /etc/init.d/mysqld /usr/bin/
rm -rf /usr/bin/mysql
ln -s $SOFTDIR/mysql/bin/mysql /usr/bin/
touch $SOFTDIR/mysql/mysql.err 
chown -R mysql:mysql $SOFTDIR/mysql
$SOFTDIR/mysql/bin/mysqld --initialize  --user=mysql --basedir=$SOFTDIR/mysql --datadir=$MYSQL_DATA_PAHT  --explicit_defaults_for_timestamp >/dev/null 2>&1
#/opt/webserver/mysql/bin/mysqld --initialize --user=mysql --basedir=/opt/webserver/mysql --datadir=/home/data/mysql --explicit_defaults_for_timestamp
if [  $? -ne 0 ];then
    echo "初始化失败...."
    exit 1
fi
service mysqld start  >/dev/null 2>&1
if [  $? -ne 0 ];then
    echo "mysql 启动失败...."
    exit 1
fi
#查看默认mysql登陆密码
mysqlpd=`grep password $SOFTDIR/mysql/mysql.err |awk -F "root@localhost: " '{print $2}'`
echo "修改初始化密码为123456"
mysql -uroot -p$mysqlpd  -e "set password=password('123456') "  --connect-expired-password   >/dev/null 2>&1
echo "#################yes mysql install with password : 123456  ###########################"

mysql备份脚本

#!/bin/bash
#按天存放备份的文件,每天整点备份存放到当天的目录里
USER=root
export password=123qwe
back_path=/home/data/mysql_backup
date=`date +%Y%m%d`
filename=`date +%Y%m%d-%H`
mkdir -p $back_path/$date 
# mysqldump -u$USER -p$password -A > $back_path/$date/$filename.sql
/opt/webserver/mysql/bin/mysqldump  -u$USER -p$password -A --opt -F --single-transaction > $back_path/$date/$filename.sql 

mysql备份数据定时删除脚本

#!/bin/bash
#按目录的时间定时删除7天前的备份,每天定时执行一次
back_path=/home/data/mysql_backup
cd $back_path

current_date=`date +%Y%m%d`
current_date_stamp=`date -d "$current_date" +%s`

dele_date_stamp=$(($current_date_stamp-604800))
for  filename in  `ls $back_path `;
do
    file_time_stamp=`date -d "$filename" +%s`
    if [ $file_time_stamp -lt $dele_date_stamp ] ; then
        rm -rf $back_path/$filename
        echo "delete $filename"
    fi
done
坚持原创技术分享,您的支持将鼓励我继续创作!