#!/bin/bash
#fonts color
yellow(){
echo -e "\033[33m\033[01m$1\033[0m"
}
green(){
echo -e "\033[32m\033[01m$1\033[0m"
}
red(){
echo -e "\033[31m\033[01m$1\033[0m"
}
blue(){
echo -e "\033[34m\033[01m$1\033[0m"
}
#java home
_JAVA_HOME=/usr/local/jdk
_JDK8_FILE=jdk-8u351-linux-x64.rpm
_JDK11_FILE=jdk-11.0.17_linux-x64_bin.rpm
#mysql rpm
_MYSQL_PATH=mysql/*.rpm
#gcc path
_GCC_PATH=depend/gcc-12.2.0.tar.gz
#make path
_MAKE_PATH=depend/make-3.82-24.el7.x86_64.rpm
#redis path
_REDIS_PATH=redis/redis-7.0.7.tar.gz
#rpm_redis path
_RPM_REDIS_PATH=redis/*.rpm
#depend path
_DEPEND_PATH=depend/*.rpm
#nginx path
_NGINX_PATH=nginx/nginx-1.22.1.tar.gz
#rpm nginx path
_RPM_NGINX_PATH=nginx/*.rpm
#minio path
_MINIO_PATH=minio/minio
#安装jdk 1.8 | 11
function install_jdk() {
#要安装的jdk版本
jdk_verson=$1
#检测是否安装了jdk
which java &> /dev/null
if [ $? -eq 0 ]; then
echo "Found java executable in PATH"
_java=`which java`
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
echo "检测到安装了JDK 脚本继续"
_java="$JAVA_HOME/bin/java"
else
echo "检测到您没有安装JDK 即将为您安装JDK"$jdk_verson
jdk $jdk_verson
fi
#校验jdk版本是否正确
if [ ! -z "$_java" ]; then
#获取jdk版本
version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
echo "当前环境安装的jdk版本为"$version
#校验版本
if [[ $version =~ $jdk_verson ]]; then
echo
else
echo "检测到您安装了"$version"的JDK 即将卸载为您重新安装JDK"$jdk_verson
#卸载jdk其他版本
remove_jdk $_java $version
sleep 1
#安装所需版本jdk
jdk $jdk_verson
fi
else
exit 1
fi
}
#安装jdk
function jdk(){
#要安装的jdk版本
jdk_verson=$1
if [ "$jdk_verson" = "1.8" ]; then
echo "开始安装JDK1.8"
rpm -ivh jdk/$_JDK8_FILE
echo
echo "JDK1.8安装完成!"
elif [ "$jdk_verson" = "11" ]; then
echo "开始安装JDK11"
rpm -ivh jdk/$_JDK11_FILE
echo
echo "JDK11安装完成!"
else
echo "不支持的版本"$1
exit 1
fi
java -version
}
#卸载jdk
function remove_jdk(){
#卸载已经安装的jdk包
echo "开始卸载已经安装的JDK"$2
if [[ $2 =~ "1.8" ]]; then
rpm -e `rpm -qa | grep jdk1.8`
echo "已卸载完JDK1.8包"
elif [[ $2 =~ "11" ]]; then
rpm -e `rpm -qa | grep jdk-11`
echo "已卸载完JDK11包"
else
#删除java可执行文件
if [ -n "$1" ] ; then
rm -rf $1
else
which java &> /dev/null
if [ $? -eq 0 ]; then
rm -rf `which java`
fi
fi
#删除java home
rm -rf $JAVA_HOME
rm -rf $_JAVA_HOME
#删除export PATH=$PATH:${JAVA_PATH}中的:${JAVA_PATH}
sed -i "s:\:\${JAVA_PATH}::g" /etc/profile
#删除/etc/profile中的变量声明
sed -i '/JAVA_HOME\|JRE_HOME\|CLASSPATH\|JAVA_PATH/d' /etc/profile
#生效/etc/profile
source /etc/profile >> /dev/null
#检查是否还存在java可执行文件
which java &> /dev/null
if [ $? -eq 0 ]; then
echo "JDK卸载失败,请手动删除/etc/profile中的变量声明及jdk文件"
exit 1
else
echo "JDK卸载完成"
fi
fi
}
#版本对比
function version_gt() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"; }
#安装mysql
function install_mysql(){
echo "开始安装mysql"
which mysql &> /dev/null
if [ $? -eq 0 ]; then
mysql --version
echo "mysql已安装, 如需重新安装请先卸载"
exit 1
fi
echo "开始卸载原有的mariadb"
rpm -e `rpm -qa | grep mariadb` --nodeps
echo "开始安装mysql8.0"
rpm -ivh $_MYSQL_PATH
echo
systemctl status mysqld
echo "mysql8.0安装完成"
systemctl enable mysqld
echo "mysql8.0已开机自启"
echo "初始化数据库"
mysqld --initialize --console
echo "开始授权目录"
chown -R mysql:mysql /var/lib/mysql/
echo "开始启动服务"
systemctl start mysqld
systemctl status mysqld
#获取临时密码
mysql_log=`cat /var/log/mysqld.log`
pwd_cnt=`cat /var/log/mysqld.log | grep -n "root@localhost:" | awk -F ":" '{print $1}'`
pwd_line=`sed -n "$pwd_cnt"p /var/log/mysqld.log`
mysql_pwd=${pwd_line#*localhost: }
echo "mysql临时密码是"$mysql_pwd
read -p "请输入mysql密码:" pwd
mysqladmin -uroot -p$mysql_pwd password $pwd
if [ $? -eq 0 ]; then
echo "mysql密码修改完成"
else
echo 'mysql密码修改失败,请手动执行
mysqladmin -uroot -p$mysql_pwd password $pwd'
fi
#修改8.0密码规则
echo "修改密码策略为mysql_native_password"
echo 'default_authentication_plugin=mysql_native_password' >> /etc/my.cnf
echo "正在重启服务"
systemctl restart mysqld
echo "重启服务成功"
echo "正在开放远程访问"
mysql -uroot -p$pwd -e "
use mysql;
update user set host = '%' where user='root';
select host, user from user where user='root';
flush privileges;"
if [ $? -eq 0 ]; then
echo "开发远程访问成功"
else
echo "开发远程访问失败,请登录mysql手动执行
use mysql;
update user set host = '%' where user='root';
select host, user from user where user='root';
flush privileges;"
fi
echo
echo "mysql8.0安装成功,已开放远程访问,密码是"$pwd
}
#修改mysql密码
function change_mysql_pwd(){
which mysql &> /dev/null
if [ $? -eq 1 ]; then
echo "mysql未安装, 请先安装"
exit 1
fi
read -p "请输入mysql原密码:" old_pwd
read -p "请输入mysql新密码:" new_pwd
mysqladmin -uroot -p$old_pwd password $new_pwd
if [ $? -eq 0 ]; then
echo "mysql密码修改完成,新密码是"$new_pwd
else
echo 'mysql密码修改失败,请手动执行
mysqladmin -uroot -p$old_pwd password $new_pwd'
fi
}
#开放mysql远程访问
function open_mysql(){
which mysql &> /dev/null
if [ $? -eq 1 ]; then
echo "mysql未安装, 请先安装"
exit 1
fi
echo "正在开放远程访问"
read -p "请输入mysql密码:" pwd
mysql -uroot -p$pwd -e "
use mysql;
update user set host = '%' where user='root';
select host, user from user where user='root';
flush privileges;"
if [ $? -eq 0 ]; then
echo "开发远程访问成功"
else
echo "开发远程访问失败,请登录mysql手动执行
use mysql;
update user set host = '%' where user='root';
select host, user from user where user='root';
flush privileges;"
fi
}
#开放防火墙端口
function open_firewalld_port(){
read -p "请输入要开放的端口号及协议[ 例如: 80/tcp | 3306/tcp | 8080/udp | 3000-4000/tcp ]:" port
echo "正在开放端口"$port
firewall-cmd --zone=public --add-port=$port --permanent
if [ $? -eq 0 ]; then
echo "开放端口"$port"成功"
else
echo "开放端口"$port"失败,请手动执行如下命令
firewall-cmd --zone=public --add-port=$port --permanent"
fi
firewall-cmd --reload
echo "防火墙重启成功"
echo
query_firewalld_port
}
#关闭防火墙端口
function close_firewalld_port(){
read -p "请输入要关闭的端口号及协议[ 例如: 80/tcp | 3306/tcp | 8080/udp | 3000-4000/tcp ]:" port
echo "正在关闭端口"$port
firewall-cmd --zone=public --remove-port=$port --permanent
if [ $? -eq 0 ]; then
echo "关闭端口"$port"成功"
else
echo "关闭端口"$port"失败,请手动执行如下命令
firewall-cmd --zone=public --remove-port=$port --permanent"
fi
firewall-cmd --reload
echo "防火墙重启成功"
echo
query_firewalld_port
}
#查看防火墙端口
function query_firewalld_port(){
echo "已经开放的端口如下:"
firewall-cmd --permanent --list-port
}
#rpm安装redis
function rpm_install_redis(){
echo "开始安装redis"
rpm -ivh $_RPM_REDIS_PATH
echo "启动redis"
systemctl start redis
echo "redis设置为开机自启"
systemctl enable redis
systemctl status redis
if [ $? -eq 0 ]; then
echo
echo "redis安装成功"
#修改redis密码
change_redis_pwd
#开放redis远程访问
open_redis
fi
}
#安装redis
function install_redis(){
echo "开始安装redis"
echo "解压缩redis"
tar -zxvf $_REDIS_PATH
sleep 1
echo "进入redis目录"
cd redis-7.0.7
echo "开始编译 make && make install"
make && make install
echo "创建redis conf目录"
mv redis.conf /etc/
echo "redis conf目录是 /etc/redis.conf"
#删除解压目录
cd ..
rm -rf redis-7.0.7
#修改redis密码
change_redis_pwd
#开放redis远程访问
open_redis
#更改pid文件路径
sed -i "s/pidfile/#pidfile/g" /etc/redis.conf
echo 'pidfile /etc/redis.pid' >> /etc/redis.conf
echo "开始写入service服务文件"
touch /usr/lib/systemd/system/redis.service
echo '[Unit]
Description=Redis
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
PIDFile=/etc/redis.pid
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target' > /usr/lib/systemd/system/redis.service
echo "重载系统服务"
systemctl daemon-reload
echo "启动redis"
systemctl start redis
echo "redis设置为开机自启"
systemctl enable redis
systemctl status redis
if [ $? -eq 0 ]; then
echo
echo "redis安装成功"
fi
}
#修改redis服务密码
function change_redis_pwd(){
read -p "请输入redis密码:" redis_pwd
#requirepass注释
sed -i "s/requirepass/#requirepass/g" /etc/redis.conf
#重新写入新密码
echo "requirepass $redis_pwd" >> /etc/redis.conf
echo "redis密码修改成功,新密码是"$redis_pwd
systemctl restart redis
echo "服务重启成功"
echo
systemctl status redis
}
#开放redis远程访问
function open_redis(){
echo "开启远程访问"
sed -i "s/bind 127.0.0.1/#bind 127.0.0.1/g" /etc/redis.conf
sed -i "s/protected-mode/#protected-mode/g" /etc/redis.conf
echo "开启远程访问成功"
systemctl restart redis
echo "服务重启成功"
echo
systemctl status redis
}
#安装nginx
function rpm_install_nginx(){
echo "开始安装nginx"
rpm -ivh $_RPM_NGINX_PATH
echo "nginx"
systemctl start nginx
echo "nginx设置为开机自启"
systemctl enable nginx
systemctl status nginx
if [ $? -eq 0 ]; then
echo
echo "nginx安装成功,配置文件路径为/usr/local/nginx/conf/nginx.conf"
fi
}
#安装nginx
function install_nginx(){
echo "开始安装nginx"
echo "开始解压nginx"
sleep 1
tar -zxvf $_NGINX_PATH
echo "nginx目录移动到/usr/local/目录下"
mv nginx-1.22.1 /usr/local/
echo "进入nginx目录"
cd /usr/local/nginx-1.22.1
echo "指定安装目录"
./configure --prefix="/usr/local/nginx"
sleep 1
echo "开始编译安装"
make && make install
echo "创建日志路径"
cd ..
rm -rf nginx-1.22.1
echo "开始写入service服务文件"
touch /usr/lib/systemd/system/nginx.service
echo '[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
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=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target' > /usr/lib/systemd/system/nginx.service
echo "重载系统服务"
systemctl daemon-reload
echo "启动 nginx"
systemctl start nginx
echo "nginx设置为开机自启"
systemctl enable nginx
systemctl status nginx
if [ $? -eq 0 ]; then
echo
echo "nginx安装成功,配置文件路径为/usr/local/nginx/conf/nginx.conf"
fi
}
#安装minio
function install_minio(){
echo "设置可执行权限"
chmod +x $_MINIO_PATH
mkdir -p /usr/local/minio
echo "移动可执行程序 到/usr/local/minio/"
mv $_MINIO_PATH /usr/local/minio
read -p "请输入minio用户名:" minio_user
read -p "请输入minio密码:" minio_pwd
echo "写入配置文件"
mkdir -p /usr/local/minio/conf/
echo "
#数据存放目录
MINIO_VOLUMES='/home/minio'
#端口号设置
MINIO_OPTS='--console-address :9001 --address :9000'
#用户名
MINIO_ROOT_USER=$minio_user
#密码
MINIO_ROOT_PASSWORD=$minio_pwd
" > /usr/local/minio/conf/minio.conf
echo "新建minio存储路径 /home/minio"
mkdir -p /home/minio
echo "开始写入service服务文件"
touch /usr/lib/systemd/system/minio.service
echo '[Unit]
Description=MinIO
Documentation=https://docs.min.io
Wants=network-online.target
After=network-online.target
#minio文件具体位置
AssertFileIsExecutable=/usr/local/minio/minio
[Service]
# User and group 用户 组
User=root
Group=root
#创建的配置文件 minio.conf
EnvironmentFile=/usr/local/minio/conf/minio.conf
ExecStart=/usr/local/minio/minio server $MINIO_OPTS $MINIO_VOLUMES
# Let systemd restart this service always
Restart=always
# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=65536
# Disable timeout logic and wait until process is stopped
TimeoutStopSec=infinity
SendSIGKILL=no
[Install]
WantedBy=multi-user.target
' > /usr/lib/systemd/system/minio.service
echo "重载系统服务"
systemctl daemon-reload
echo "启动 minio"
systemctl start minio
echo "minio设置为开机自启"
systemctl enable minio
systemctl status minio
if [ $? -eq 0 ]; then
echo
echo "minio安装成功,配置文件路径为/usr/local/minio/conf/minio.conf"
fi
}
#检查selinux是否关闭
function check_selinux(){
CHECK=$(grep SELINUX= /etc/selinux/config | grep -v "#")
if [ "$CHECK" == "SELINUX=enforcing" ]; then
red "======================================================================="
red "检测到SELinux为开启状态,先临时设置为关闭状态,下次重启后会永久关闭"
red "======================================================================="
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
fi
if [ "$CHECK" == "SELINUX=permissive" ]; then
red "======================================================================="
red "检测到SELinux为开启状态,先临时设置为关闭状态,下次重启后会永久关闭"
red "======================================================================="
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
fi
}
# #检查redis、nginx编译依赖
# function check_depend(){
# #检查gcc依赖
# echo "开始检查gcc依赖"
# which gcc &> /dev/null
# if [ $? -eq 1 ]; then
# echo "gcc未安装,即将安装"
# tar -zxvf $_GCC_PATH
# sleep 1
# echo "进入gcc目录"
# cd gcc
# echo "开始安装"
# rpm -Uvh *.rpm --nodeps --force
# echo "gcc安装完成"
# echo
# gcc -v
# #删除gcc目录
# cd ..
# rm -rf gcc
# else
# echo "gcc依赖已存在,无需安装"
# echo
# gcc -v
# fi
# echo
# echo "开始检查make依赖"
# #检查make依赖
# which make &> /dev/null
# if [ $? -eq 1 ]; then
# echo "make未安装,即将安装"
# echo "开始安装"
# rpm -Uvh $_MAKE_PATH --nodeps --force
# echo "make安装完成"
# echo
# make -v
# else
# echo "make依赖已存在,无需安装"
# echo
# make -v
# fi
# }
#安装nginx编译依赖
function check_depends(){
echo "开始安装编译依赖"
sleep 3
rpm -Uvh $_DEPEND_PATH --nodeps --force
echo
echo "编译依赖安装完成"
}
#开始菜单
start_menu(){
clear
green " =================================================="
green " 生产环境 一键离线安装脚本 Centos7 x64 By thsrite"
green " 可选安装JDK1.8、JDK11、卸载JDK"
green " 可选安装mysql8.0、修改mysql密码、开放mysql远程访问"
green " 可选安装redis7.0、修改redis密码、开放redis远程访问"
green " 可选安装nginx1.22.1 "
green " 可选安装minio "
green " 可组合安装jdk、mysql、redis、nginx"
green " 可开放、关闭、查看防火墙端口"
green " 为防止出现其他问题,请在未安装上述服务的环境中安装 "
green " tomcat、minio等后续会增加 "
green " =================================================="
echo
blue " =====================服务安装====================="
red " =================================================="
yellow " 1. 安装JDK1.8"
red " =================================================="
yellow " 2. 安装JDK11"
red " =================================================="
yellow " 3. 安装mysql8.0"
red " =================================================="
yellow " 4. 安装redis7.0"
red " =================================================="
yellow " 5. 安装nginx1.22.1"
red " =================================================="
yellow " 6. 安装minio"
red " =================================================="
yellow " 7. 安装JDK1.8、mysql8.0、redis7.0、nginx1.22.1"
red " =================================================="
yellow " 8. 安装JDK11、mysql8.0、redis7.0、nginx1.22.1"
red " =================================================="
# blue " =============================================="
echo
blue " =====================服务治理====================="
red " =================================================="
yellow " 10. 修改mysql8.0密码"
red " =================================================="
yellow " 11. 开放mysql8.0远程访问"
red " =================================================="
yellow " 12. 修改redis7.0密码"
red " =================================================="
yellow " 13. 开放redis7.0远程访问"
red " =================================================="
yellow " 14. 卸载JDK"
red " =================================================="
# blue " =============================================="
echo
blue " =====================端口管理====================="
red " =================================================="
yellow " 20. 开放防火墙端口"
red " =================================================="
yellow " 21. 关闭防火墙端口"
red " =================================================="
yellow " 22. 查看已开放防火墙端口"
red " =================================================="
# blue " =============================================="
echo
blue " =====================退出脚本====================="
red " =================================================="
yellow " 0. 退出脚本"
red " =================================================="
echo
read -p "请输入数字:" num
case "$num" in
#########服务安装#########
1)
#安装jdk1.8
install_jdk 1.8
;;
2)
#安装jdk11
install_jdk 11
;;
3)
#安装mysql
install_mysql
;;
4)
#安装redis
#检查编译依赖
check_depends
#检查selinux是否关闭
check_selinux
sleep 3
rpm_install_redis
;;
5)
#安装nginx
#检查编译依赖
check_depends
#检查selinux是否关闭
check_selinux
sleep 3
rpm_install_nginx
;;
6)
#安装minio
#检查编译依赖
check_depends
#检查selinux是否关闭
check_selinux
sleep 3
install_minio
;;
7)
#安装jdk1.8
install_jdk 1.8
#安装mysql
install_mysql
#安装redis
#检查编译依赖
check_depends
#检查selinux是否关闭
check_selinux
sleep 3
rpm_install_redis
#安装nginx
sleep 3
rpm_install_nginx
;;
8)
#安装jdk11
install_jdk 11
#安装mysql
install_mysql
#安装redis
#检查编译依赖
check_depends
#检查selinux是否关闭
check_selinux
sleep 3
rpm_install_redis
#安装nginx
sleep 3
rpm_install_nginx
;;
#########服务治理#########
10)
#修改mysql8.0密码
change_mysql_pwd
;;
11)
#开放mysql远程访问
open_mysql
;;
12)
#修改redis密码
change_redis_pwd
;;
13)
#开放redis远程访问
open_redis
;;
14)
#删除jdk
remove_jdk
;;
#########端口管理#########
20)
#开放端口
open_firewalld_port
;;
21)
#关闭端口
close_firewalld_port
;;
22)
#查询端口
query_firewalld_port
;;
0)
exit 1
;;
*)
clear
red "请输入正确数字"
sleep 1s
start_menu
;;
esac
}
#启动命令
start_menu