SHELL

一键部署环境的简单脚本示例

浅时光博客 · 2月12日 · 2020年 73998次已读
#!/bin/bash

echo "
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
#  一键系统初始化,监控部署,nginx,Redis,MySQL安装       #
#           本脚本由www.dqzboy.com提供                 #
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
"
#定义颜色
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[36m'
plain='\033[0m'

#定义监控服务端IP
zabbix_server="xx.xx.xx.xx"

System_initial() {
echo -e "${blue}
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
#                                                    #
#            开始对服务器系统进行初始化                  #
#                                                    # 
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
${plain}"

echo -e "${yellow}
+------------------------------+
|        1. 时 区 检 查         |
+------------------------------+
${plain}"

date=$(date +%F-%T)
echo "当前时间:${date}"
echo "当前时区:$(timedatectl |grep "Time zone"|awk -F ":" '{print $2}')"
if [ $(date +%Z) == "CST" ];then
    echo " "
else
    echo "当前系统时区非CST,将进行修改时区"
    timedatectl set-timezone Asia/Shanghai
fi

echo -e "${yellow}
+------------------------------+
|        2. 创 建 用 户         |
+------------------------------+
${plain}"

user="appadmin"
useradd $user
read -s -p "请设定用户登入密码:" passwd
echo ${passwd} | passwd --stdin ${user}

echo -e "${yellow}
+------------------------------+
|        3. 修改主机名           |
+------------------------------+
${plain}"

read -p "input HostName: " name
if [ ! -n "$name" ];then
  echo "你一个字也没输入"
  exit 1
else
  hostnamectl set-hostname $name 
fi

echo -e "${yellow}
+------------------------------+
|        4. 下载依赖包           |
+------------------------------+
${plain}"

yum -y install vim net-tools httpd-tools lrzsz ntp wget get curl rsync git bash-completion bash-completion-extras gcc make lsof pcre pcre-devel zlib zlib-devel openssl openssl-devel

echo -e "${yellow}
+------------------------------+
|        5. 时 间 同 步         |
+------------------------------+
${plain}"

systemctl start ntpd
systemctl enable ntpd > /dev/null
status=`systemctl status ntpd | grep "Active"  | awk -F " " {'print $3'}`
echo -e "当前ntpd服务状态为:${green}${status}${plain}"
echo "同步互联网时间..."
ntpdate -d cn.pool.ntp.org
ntpdate cn.pool.ntp.org
echo "*/5 * * * *  ntpdate cn.pool.ntp.org" >> /var/spool/cron/root

echo -e "${yellow}
+------------------------------+
|        6. 创 建 目 录         |
+------------------------------+
${plain}"

echo "下载的所有程序源码包存储在/opt/soft目录下"
package="/opt/soft"
mkdir -p $package
}

Zabbix_Agent() {
echo -e "${blue}
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
#                                                    #
#           开始对服务器系统安装监控Agent                #
#                                                    # 
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
${plain}"

echo -e "${yellow}
+------------------------------+
|        1. 下载客户端           |
+------------------------------+
${plain}"

cd $package && wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.6-1.el7.x86_64.rpm

echo -e "${yellow}
+------------------------------+
|        2. 安装客户端           |
+------------------------------+
${plain}"

rpm -ivh zabbix-agent-4.2.6-1.el7.x86_64.rpm

echo -e "${yellow}
+------------------------------+
|        3. 修 改 配 置         |
+------------------------------+
${plain}"

sed -i "s/Server=127.0.0.1/Server=${zabbix_server}/g" /etc/zabbix/zabbix_agentd.conf
sed -i "s/ServerActive=127.0.0.1/ServerActive=${zabbix_server}/g" /etc/zabbix/zabbix_agentd.conf
sed -i "s/Hostname=Zabbix server/Hostname=$(hostname)/g" /etc/zabbix/zabbix_agentd.conf

echo -e "${yellow}
+------------------------------+
|        4. 检查防火墙           |
+------------------------------+
${plain}"

firewall-cmd --state
if [ $? == 0 ];then
    firewall-cmd --zone=public --add-port=10050/tcp --permanent
    firewall-cmd --reload
elif [ $? != 0 ];then
    read -p "防火墙关闭,是否开启并放通10050端口(Y/N): " go
        if [ $go == Y ] || [ $go == yes ] || [ $go == y ];then
            systemctl start firewalld
            firewall-cmd --zone=public --add-port=10050/tcp --permanent
            firewall-cmd --reload
     	elif [ $go == N ] || [ $go == no ] || [ $go == n ];then
            echo "为了安全建议开启防火墙"
     	else 
            echo "你的输入有误!!!"
      	fi
else
    echo "未检测到防火墙服务"
fi

echo -e "${yellow}
+------------------------------+
|        5. 启动服务            |
+------------------------------+
${plain}"

systemctl restart zabbix-agent
systemctl enable zabbix-agent 
status=`lsof -i:10050|awk '{print $1}'|grep zabbix|wc -l`
if [ $status != 0 ];then
   echo "Zabbix服务器已经正常启动"
   echo "放行服务监听端口"
      firewall-cmd --zone=public --add-port=10050/tcp --permanent
      firewall-cmd --reload
else
   echo "Zabbix服务未能正常启动,请查看日志"
fi
}

JDK() {
echo -e "${blue}
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
#                                                    #
#           开始对服务器系统安装OracleJDK               #
#                                                    # 
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
${plain}"

echo -e "${yellow}
+------------------------------+
|        1. 下载程序包           |
+------------------------------+
${plain}"
cd $package 
wget https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.rpm

echo -e "${yellow}
+------------------------------+
|        2. 安 装 程 序         |
+------------------------------+
${plain}"

rpm -ivh jdk-8u202-linux-x64.rpm

echo -e "${yellow}
+------------------------------+
|        5. 检 查 服 务         |
+------------------------------+
${plain}"

java -version
}

Nginx() {
echo -e "${blue}
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
#                                                    #
#           开始对服务器系统安装Nginx服务                #
#                                                    # 
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
${plain}"

echo -e "${yellow}
+------------------------------+
|        1. 下载程序包           |
+------------------------------+
${plain}"

cd $package && wget https://nginx.org/download/nginx-1.16.1.tar.gz

echo -e "${yellow}
+------------------------------+
|        2. 安 装 程 序         |
+------------------------------+
${plain}"

mkdir /home/appadmin/nginx
chown -R appadmin:appadmin /home/appadmin/nginx
cd $package && tar -xf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --error-log-path=/home/appadmin/nginx/error.log --http-log-path=/home/appadmin/nginx/access.log --pid-path=/home/appadmin/nginx/nginx.pid --lock-path=/home/appadmin/nginx/nginx.lock --user=appadmin --group=appadmin --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio
make && make install

echo -e "${yellow}
+------------------------------+
|        3. 修 改 配 置         |
+------------------------------+
${plain}"

CPU=`lscpu |grep "^CPU(s)" |awk '{print $2}'`
sed -i "s/worker_processes  1;/worker_processes  "${CPU}";/g" /usr/local/nginx/nginx.conf

echo -e "${yellow}
+------------------------------+
|        4. 启 动 程 序         |
+------------------------------+
${plain}"

chown -R appadmin:appadmin /usr/local/nginx
setcap cap_net_bind_service=+eip /usr/local/nginx/sbin/nginx
su - appadmin -c "/usr/local/nginx/sbin/nginx  -c /usr/local/nginx/nginx.conf"

echo -e "${yellow}
+------------------------------+
|        5. 检 查 程 序         |
+------------------------------+
${plain}"

status=`lsof -i:80|awk '{print $1}'|grep -w nginx|wc -l`
if [ $status != 0 ];then
   echo "Nginx服务器已经正常启动"
   echo "放行服务监听端口"
      firewall-cmd --zone=public --add-port=80/tcp --permanent
      firewall-cmd --reload
else
   echo "Nginx服务未能正常启动,请查看日志"
fi

echo -e "${yellow}
+------------------------------+
|        6.加入开机自启          |
+------------------------------+
${plain}"

echo su - appadmin -c '"/usr/local/nginx/sbin/nginx  -c /usr/local/nginx/nginx.conf"' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
}

MySQL() {
echo -e "${blue}
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
#                                                    #
#           开始对服务器系统安装MySQL服务                #
#                                                    # 
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
${plain}"

echo -e "${yellow}
+------------------------------+
|        1. 下载程序包           |
+------------------------------+
${plain}"

rpm -qa | grep mariadb
if [ $? -eq 0 ];then
    echo "系统存在默认的Mariadb数据库"
    echo "正在执行卸载..."
    rpm -qa | grep mariadb | xargs rpm -e --nodeps
else
   echo "系统未安装Mariadb数据库,正在执行MySQL数据库安装..."
fi
cd $package
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-community-client-5.7.22-1.el7.x86_64.rpm
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-community-libs-5.7.22-1.el7.x86_64.rpm
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-community-common-5.7.22-1.el7.x86_64.rpm
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-community-server-5.7.22-1.el7.x86_64.rpm
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-community-devel-5.7.22-1.el7.x86_64.rpm

echo -e "${yellow}
+------------------------------+
|        2. 安 装 程 序         |
+------------------------------+
${plain}"

rpm -ivh mysql-community-*

echo -e "${yellow}
+------------------------------+
|        3. 修 改 配 置         |
+------------------------------+
${plain}"

cat > /etc/my.cnf << EOF
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character-set-server=utf8mb4
default-storage-engine=INNODB
max_allowed_packet=256M
innodb_log_file_size=2GB
log_output=file
slow_query_log=on
slow_query_log_file = /var/lib/mysql
log_queries_not_using_indexes=on
long_query_time = 1
sql_mode=ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF

echo -e "${yellow}
+------------------------------+
|        4. 启 动 程 序         |
+------------------------------+
${plain}"

systemctl start mysqld
systemctl enable mysqld

echo -e "${yellow}
+------------------------------+
|        5. 检 查 服 务         |
+------------------------------+
${plain}"

status=`lsof -i:3306|awk '{print $1}'|grep -w mysqld|wc -l`
if [ $status != 0 ];then
   echo "MySQL服务器已经正常启动"
   echo "放行服务监听端口"
      firewall-cmd --zone=public --add-port=3306/tcp --permanent
      firewall-cmd --reload
else
   echo "MySQL服务未能正常启动,请查看日志"
fi
}

Redis() {
echo -e "${blue}
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
#                                                    #
#           开始对服务器系统安装Redis服务                #
#                                                    # 
#++++++++++++++++++++++++++++++++++++++++++++++++++++#
${plain}"

echo -e "${yellow}
+------------------------------+
|        1. 下载程序包           |
+------------------------------+
${plain}"

cd $package && wget http://download.redis.io/releases/redis-5.0.6.tar.gz

echo -e "${yellow}
+------------------------------+
|        2. 安 装 程 序         |
+------------------------------+
${plain}"

tar -xf redis-5.0.6.tar.gz -C /usr/local/
cd /usr/local && mv redis-5.0.6 redis && cd redis
make && make install PREFIX=/usr/local/redis
cd ./bin && cp * /usr/local/bin/

echo -e "${yellow}
+------------------------------+
|        3. 修改配置文件         |
+------------------------------+
${plain}"

mv /usr/local/redis/redis.conf{,_bak}
cd /usr/local/redis
cat > /usr/local/redis/redis.conf << EOF
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
#自定义日志存储目录
logfile "/data/redis/logs/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
#设置redis认证密码
requirepass redis@admin
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
EOF

#创建日志和数据存储目录并授权
mkdir -p /data/redis/logs
chown -R appadmin:appadmin /usr/local/redis
chown -R appadmin:appadmin /data/redis/

echo -e "${yellow}
+------------------------------+
|        4. 创建启动脚本         |
+------------------------------+
${plain}"

cat > /usr/lib/systemd/system/redis.service << EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
ExecRepload=/bin/kill -s HUP $MAINPID
Type=notify
User=appadmin
Group=appadmin
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF

echo -e "${yellow}
+------------------------------+
|     5. 启动Redis服务程序       |
+------------------------------+
${plain}"

systemctl daemon-reload
systemctl enable redis
systemctl start redis

echo -e "${yellow}
+------------------------------+
|     6.  检查Redis服务状态      |
+------------------------------+
${plain}"

status=`lsof -i:6379|awk '{print $1}'|grep -w redis-ser | wc -l`
if [ $status != 0 ];then
   echo "Redis服务器已经正常启动"
   echo "放行服务监听端口"
      firewall-cmd --zone=public --add-port=6379/tcp --permanent
      firewall-cmd --reload
else
   echo "Redis服务未能正常启动,请查看日志"
fi
}

main() {
    System_initial
    Zabbix_Agent
    JDK
    Nginx
    MySQL
    Redis
}
main
文章来源(Source):浅时光博客文章来源(Source):浅时光博客文章来源(Source):浅时光博客

本文作者:浅时光博客
原文链接:https://www.dqzboy.com/510.html
版权声明:知识共享署名-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)协议进行许可,转载时请以>超链接形式标明文章原始出处和作者信息
免责声明:本站提供的内容仅限于个人学习和研究使用;禁止将内容用于商业或非法用途。下载后请在24小时内彻底删除,否则后果由用户承担。访问和下载本站内容即表示您已同意上述条款 。

2 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!

  1. 小悳LL广东·广州2021-12-6 · 11:39

    博主真的技术大牛,无私分享了很多很好的技术!!佩服佩服,爱了!

  2. onion12021-3-22 · 17:08

    可以!有兴趣的可以试一下告诉我结果怎样