shell脚本一键部署环境

代理主机的配置

  • 首先介绍环境是两台虚拟机,是为了交任务才这样配置,一台虚拟机有数据库、php、nginx、keepalived,一台虚拟机有nginx、keepalived,未做动静分离,只配置了nginx反向代理php与tomcat的请求,还弄了nginx负载均衡,创建了一个以.conf结尾的文件,实现由请求路劲的ip_hash分权,最后弄了keepalived,实现ip漂移,主机的权重为100,从机的权重为50,所以正常情况下虚拟ip在主机内,但是如果主机挂了,虚拟ip会判别,然后到另一台代理服务器工作。
  • 遇到的难点主要就是对脚本的不熟悉,对任务的理解不清晰,
    对日志文件的查错不熟悉。而且很容易遇到技术难题,因为对shell不熟悉,会出各种各样的问题,非常考验仔细度,而且每次为了实现某个功能会先建一个脚本来达到效果再加进自己的脚本中。然后就是改端口,因为nginx与httpd在同一台机子,所以nginx一直无法打开让我郁闷很久,总以为是脚本写错了,把配置改错了。。。然后经过老师指点后我就把httpd的端口号从20到21,然后两者就能在一个服务器共存了。
  • shell脚本的目录很简单,就是一个主shell(t8.sh),多个副shell(t9.sh、t10.sh、t11.sh、t12.sh、t13.sh),依down的流程从头到尾执行,每一个地方嵌套一个子shell,用source来嵌入子脚本,当子脚本执行完毕则回到父脚本继续执行,所以子脚本分别实现了安装配置数据库、php、nginx、keepalived的功能,主shell就只是一个单纯的菜单,只是没有提供给用户选择安装而已,最后,其实这两台机子都安装了ansible,而且互相之间实现了免密登录,我尝试了用ansible在实现集群执行脚本,但有问题,时间有限,我就只弄尝试到了这个地步,希望以后能实现用ansible集群部署,一切都是为了让我更懒。
  • shell脚本不熟悉,所以写得不完善,很多流程都没有判断就直接进行。然后是不会shell的某些功能就去百度查,大体按着自己的思路写,嗯,以后有时间学得更多了再慢慢完善这些bug,先亮效果截图:
    shell脚本一键部署环境_第1张图片

shell脚本一键部署环境_第2张图片

shell脚本一键部署环境_第3张图片
shell脚本一键部署环境_第4张图片

shell脚本一键部署环境_第5张图片

  • t6.sh—为了尝试一键安装所以准备了一键卸载
systemctl stop mariadb-server mariadb
yum remove mariadb-server mariadb
rm -fr /var/log/mariadb


systemctl stop nginx
yum remove nginx
yum remove nginx-release-centos-7-0.el7.ngx.noarch
rm -fr /etc/nginx

systemctl stop httpd
yum remove httpd
rm -fr /etc/httpd/

systemctl stop keepalived
yum remove keepalived
rm -fr /etc/keepalived


t8.sh

#!/bin/bash
#function:install mariadb-server、mariadb、php、nginx、keepalived


echo -e "\033[32m 正在启动数据库的安装.....\033[0m"
source ./t13.sh
echo "mariadb安装成功!"
sleep 5

echo -e "\033[32m 正在启动php的安装和配置....\033[0m"
source ./t12.sh
echo "php安装成功!"
sleep 5

echo -e "\033[32m 正在启动安装nginx.......\033[0m"
source ./t9.sh
echo -e "\033[32m nginx安装成功!\033[0m"
sleep 5

echo -e "\033[32m 代理主机正在安装keepalived....请稍后\033[0m"
source ./t10.sh
echo -e "\033[32m keepalived安装成功!\033[0m"
sleep 5

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 子机安装成功!\033[0m"
fi


echo -e "\033[32m 退出安装,谢谢!\033[0m"

t9.sh

#!/bin/bash

#echo -e "\033[32m 正在禁止firewall开机启动...\033[0m"
#`systemctl disable firewalld.service`
#echo -e "\033[32m firewall已禁止\033[0m"

#echo -e "\033[32m 正在打开selinux配置文件...\033[0m"
#`sudo vim /etc/selinux/config`
#改SELINUX=disable
#SELINUXTYPE=targeted   #注释


NGINXRPM=nginx-release-centos-7-0.el7.ngx.noarch.rpm
NGINXDIR=/opt/nginx-release-centos-7-0.el7.ngx.noarch.rpm
NGINXDIR=/etc/nginx/conf.d/default.conf
MYNGINXDIR=/etc/nginx/conf.d/tomcats.conf
MYCONF=tomcats.conf
CONTENT=index index.html index.htm
DIR=/etc/nginx/conf.d/tomcats.conf


echo -e "\033[32m 开始安装nginx...\033[0m"

echo -e "\033[32m 正在下载...\033[0m"
wget http://nginx.org/packages/centos/7/noarch/RPMS/$NGINXRPM

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 正在解压...\033[0m"
        rpm -ivh $NGINXRPM
fi

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 正在yum源下载...\033[0m"
        yum -y install nginx
fi

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 安装成功!正在重启...\033[0m"
        systemctl restart nginx
        echo -e "\033[32m 清除残余文件...\033[0m"
        rm -fr $NGINXRPM
fi

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 显示nginx状态...\033[0m"
        systemctl status nginx
fi


echo -e "\033[32m 启动修改nginx的配置...\033[0m"

if [[ $? -eq 0  ]];then
        echo -e "\033[32m 正在添加tomcats.conf文件...\033[0m"
        touch $DIR
fi
if [[ $? -eq 0 ]];then
        echo "
upstream javaservers {
        ip_hash;
        server 39.106.50.206:8080;
}" > $DIR
fi

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 正在添加php代理设置....请稍后...\033[0m"
echo "server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass http://javaservers/;
    }

    #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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    location ~ \.php$ {
        proxy_pass   http://192.168.235.131:81;
    }

    # 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  /scripts$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;
    #}
}
" > $NGINXDIR



        echo -e "\033[32m 配置成功!\033[0m"
fi

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 正在重新加载nginx...\033[0m"
        systemctl restart nginx
        echo -e "\033[32m 正在修改临时权限...\033[0m"
        setenforce 0
        echo -e "\033[32m nginx全部配置完毕,请到浏览器访问...\033[0m"
fi

t10.sh

#!/bin/bash
#function:安装并修改keepalived主机

KEEPDIR=/etc/keepalived/keepalived.conf

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 开始下载keepalived...."
        yum -y install keepalived
        echo -e "\033[32m keepalived下载成功...、033[0m"
fi

#修改配置
if [[ $? -eq 0 ]];then
        sleep 5
        echo -e "\033[32m 正在修改代理主机keepalived的配置....\033[0m"
        > KEEPDIR
        echo -e "\033[32m 添加.....\033[0m"
        echo "! Configuration File for keepalived
global_defs {
        router_id LVS_DEVEL
}
vrrp_instance VI_1 {
        state MASTER
        interface eno16777736
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                192.168.235.150
        }
}" > $KEEPDIR
        sleep 5
        echo -e "\033[32m 修改成功!请稍后...\033[0m"
        echo -e "\033[32m 重新加载keepalived....请稍后....\033[0m"
        systemctl restart keepalived
        systemctl restart nginx
        setenforce 0
        echo -e "\033[32m 安装成功,谢谢!\033[0m"
fi

t11.sh

#!/bin/bash
#function:keepalived从机配置

KEEPDIR=/etc/keepalived/keepalived.conf

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 开始下载keepalived...."
        yum -y install keepalived
        echo -e "\033[32m keepalived下载成功...、033[0m"
fi

#修改配置
if [[ $? -eq 0 ]];then
        sleep 5
        echo -e "\033[32m 正在修改代理主机keepalived的配置....\033[0m"
        > KEEPDIR
        echo -e "\033[32m 添加.....\033[0m"
        echo "! Configuration File for keepalived
global_defs {
        router_id LVS_DEVEL1
}
vrrp_instance VI_1 {
        state BACKUP
        interface eno16777736
        virtual_router_id 51
        priority 50
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                192.168.235.150
        }
}" > $KEEPDIR
        sleep 5
        echo -e "\033[32m 修改成功!请稍后...\033[0m"
        echo -e "\033[32m 重新加载keepalived....请稍后....\033[0m"
        systemctl restart keepalived
        systemctl restart nginx
        setenforce 0
        echo -e "\033[32m 安装成功,谢谢!\033[0m"
fi

t12.sh

#!/bin/bash
#function:安装配置php
echo "这是shell脚本自动化安装php环境"
DIR=/etctpd
if [ ! -d $DIR ];then

        echo "开始安装Apache"
        yum -y install httpd
        echo "安装Apache完毕,正在启动Apache..."
        systemctl restart httpd
        echo "启动完毕!正在设置开机启动..."
        systemctl enable httpd
        echo "设置完毕!"

        echo "正在建立测试页"
        touch /var/www/html/homework.php
        echo "" > /var/www/html/homework.php
        echo "建立测试页成功!"

        echo "开始安装PHP"
        yum -y install php

        echo "安装完毕!为避免与nginx产生冲突正在修改httpd端口...."
        sed -i 's/Listen 80/Listen 81/g' /etc/httpd/conf/httpd.conf

        echo "正在重新加载hpptd..."
        systemctl restart httpd
        echo "请前往浏览器测试php安装:"
        echo "(提示:在浏览器中输入:192.168.xxx.xxx/homework.php )"

else
        echo "目标已存在"
fi

t13.sh

#!/bin/bash
#function: 安装MariaDB
echo -e "\033[32m 这是shell脚本自动化安装MariaDB\033[0m"
DIR=/mydata/data
if [[  ! -d $DIR ]];then
        echo "开始安装MariaDB"
        yum -y install mariadb-server mariadb
        echo "安装MariaDB完毕,正在启动MariaDB..."
        systemctl restart mariadb
        echo "启动完毕!正在设置开机启动..."
        systemctl enable mariadb
        echo  "对mariaDB进行简单配置"
        mysql_secure_installation
        echo "设置完毕!"
else
        echo  "目标已存在!"
fi

另一台代理子机的安装


t8.sh

#!/bin/bash
#function:install mariadb-server、mariadb、php、nginx、keepalived


        echo -e "\033[32m 正在启动安装nginx.......\033[0m"
        source ./t9.sh
        sleep 5
        echo -e "\033[32m nginx安装成功!\033[0m"
        echo -e "\033[32m 代理子机正在安装keepalived....请稍后\033[0m"
        source ./t11.sh
        echo -e "\033[32m keepalived安装成功!\033[0m"
        sleep 5
if [[ $? -eq 0 ]];then
        echo -e "\033[32m 子机安装成功!\033[0m"
fi

t9.sh

#!/bin/bash

#echo -e "\033[32m 正在禁止firewall开机启动...\033[0m"
#`systemctl disable firewalld.service`
#echo -e "\033[32m firewall已禁止\033[0m"

#echo -e "\033[32m 正在打开selinux配置文件...\033[0m"
#`sudo vim /etc/selinux/config`
#改SELINUX=disable
#SELINUXTYPE=targeted   #注释


NGINXRPM=nginx-release-centos-7-0.el7.ngx.noarch.rpm
NGINXDIR=/opt/nginx-release-centos-7-0.el7.ngx.noarch.rpm
NGINXDIR=/etc/nginx/conf.d/default.conf
MYNGINXDIR=/etc/nginx/conf.d/tomcats.conf
MYCONF=tomcats.conf
CONTENT=index index.html index.htm
DIR=/etc/nginx/conf.d/tomcats.conf


echo -e "\033[32m 开始安装nginx...\033[0m"

echo -e "\033[32m 正在下载...\033[0m"
wget http://nginx.org/packages/centos/7/noarch/RPMS/$NGINXRPM

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 正在解压...\033[0m"
        rpm -ivh $NGINXRPM
fi

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 正在yum源下载...\033[0m"
        yum -y install nginx
fi

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 安装成功!正在重启...\033[0m"
        systemctl restart nginx
        echo -e "\033[32m 清除残余文件...\033[0m"
        rm -fr $NGINXRPM
fi

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 显示nginx状态...\033[0m"
        systemctl status nginx
fi


echo -e "\033[32m 启动修改nginx的配置...\033[0m"
if [[ $? -eq 0  ]];then
        echo -e "\033[32m 正在添加tomcats.conf文件...\033[0m"
        touch $DIR
fi

if [[ $? -eq 0 ]];then
        echo "
upstream javaservers {
        ip_hash;
        server 39.106.50.206:8080;
}" > $DIR
fi

if [[ $? -eq 0 ]];then
echo "server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass http://javaservers/;
    }

    #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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    location ~ \.php$ {
        proxy_pass   http://192.168.235.131:81;
    }

    # 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  /scripts$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;
    #}
}
" > $NGINXDIR

        echo -e "\033[32m 配置成功!\033[0m"
fi

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 正在重新加载nginx...\033[0m"
        systemctl restart nginx
        echo -e "\033[32m 正在修改临时权限...\033[0m"
        setenforce 0
        echo -e "\033[32m nginx全部配置完毕,请到浏览器访问...\033[0m"
fi

t11.sh

#!/bin/bash
#function:keepalived从机配置

KEEPDIR=/etc/keepalived/keepalived.conf

if [[ $? -eq 0 ]];then
        echo -e "\033[32m 开始下载keepalived...."
        yum -y install keepalived
        echo -e "\033[32m keepalived下载成功...、033[0m"
fi

#修改配置
if [[ $? -eq 0 ]];then
        sleep 5
        echo -e "\033[32m 正在修改代理主机keepalived的配置....\033[0m"
        > KEEPDIR
        echo -e "\033[32m 添加.....\033[0m"
        echo "! Configuration File for keepalived
global_defs {
        router_id LVS_DEVEL1
}
vrrp_instance VI_1 {
        state BACKUP
        interface eno16777736
        virtual_router_id 51
        priority 50
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                192.168.235.150
        }
}" > $KEEPDIR
        sleep 5
        echo -e "\033[32m 修改成功!请稍后...\033[0m"
        echo -e "\033[32m 重新加载keepalived....请稍后....\033[0m"
        systemctl restart keepalived
        systemctl restart nginx
        setenforce 0
        echo -e "\033[32m 安装成功,谢谢!\033[0m"
fi

你可能感兴趣的:(环境部署,linux)