RabbitMQ-shell自动安装脚本

2023-12-27 15:28:39
#!/bin/bash

# *****************************************************************
# * INTELLIGENT TECHNOLOGY CO.,LTD,All Rights Reserved
# * Copyright (C) 
# * @Title: RabbitMQ一键自动安装脚本
# * @Author: 
# * @Version: v1.1
# * @Date: 2023/11/20
# * @Description:  
# * v1.1.更改依赖改为离线下的安装
# *****************************************************************
set -e

ERLANG_PACKAGE="/usr/local/tech/autoinstall/lib/otp_src_24.3.tar.gz"
RABBITMQ_PACKAGE="/usr/local/tech/autoinstall/lib/rabbitmq-server-generic-unix-3.9.16.tar.xz"
DEPENDENCY_DIR="/usr/local/tech/autoinstall/third-lib/"
INSTALL_DIR="/usr/local/tech/rabbitmq"
ERLANG_DIR="$INSTALL_DIR"/otp_src_24.3
RABBITMQ_USER="mquser"
RABBITMQ_PASSWORD="mqpassword"
RABBITMQ_TAG="administrator"

if [[ $EUID -ne 0 ]]; then
   echo "该脚本必须以root身份运行" 
   exit 1
fi

if [ ! -f "$ERLANG_PACKAGE" ]; then
   echo "Erlang安装包不存在."
   exit 1
fi

if [ ! -f "$RABBITMQ_PACKAGE" ]; then
   echo "RabbitMQ安装包不存在."
   exit 1
fi

echo "安装依赖..."
yum localinstall -y "$DEPENDENCY_DIR/glibc-devel"/*.rpm
yum localinstall -y "$DEPENDENCY_DIR/xmlto"/*.rpm
yum localinstall -y "$DEPENDENCY_DIR/unixODBC-devel"/*.rpm
yum localinstall -y "$DEPENDENCY_DIR/perl"/*.rpm
yum localinstall -y "$DEPENDENCY_DIR/bison"/*.rpm
yum localinstall -y "$DEPENDENCY_DIR/byacc"/*.rpm

echo "创建RabbitMQ用户和组..."
groupadd rabbitmq
useradd -r -g rabbitmq -s /bin/false rabbitmq

echo "创建目录并设置权限..."
mkdir -p "$INSTALL_DIR"
chown -R rabbitmq:rabbitmq "$INSTALL_DIR"

echo "解压并安装Erlang..."
tar -xf "$ERLANG_PACKAGE" -C "$INSTALL_DIR"
cd "$ERLANG_DIR"
./configure && make && make install

echo "解压并安装RabbitMQ..."
tar -xf "$RABBITMQ_PACKAGE" --strip-components=1 -C "$INSTALL_DIR"
chown -R rabbitmq:rabbitmq "$INSTALL_DIR"
cd "$INSTALL_DIR"/sbin
chmod +x rabbitmq*

echo "将Erlang和RabbitMQ可执行文件添加到系统路径..."
echo "export PATH=\$PATH:$ERLANG_DIR/bin:$INSTALL_DIR/sbin" >> /etc/profile
source /etc/profile

echo "启动RabbitMQ服务..."
rabbitmq-server -detached

# 添加延迟以确保服务器在运行rabbitmqctl命令前已经启动
sleep 10

echo "启动RabbitMQ Web管理插件..."
rabbitmq-plugins enable rabbitmq_management

echo "添加RabbitMQ用户,设置密码和权限,删除guest用户..."
rabbitmqctl add_user $RABBITMQ_USER $RABBITMQ_PASSWORD
rabbitmqctl set_user_tags $RABBITMQ_USER $RABBITMQ_TAG
rabbitmqctl set_permissions -p / $RABBITMQ_USER ".*" ".*" ".*"
rabbitmqctl delete_user guest

echo "创建systemd服务文件..."
cat << EOF > /etc/systemd/system/rabbitmq.service
[Unit]
Description=RabbitMQ Server
After=network.target

[Service]
User=rabbitmq
Group=rabbitmq
ExecStart=$INSTALL_DIR/sbin/rabbitmq-server
ExecReload=/bin/kill -s HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

echo "启动并设置RabbitMQ服务为开机自启..."
systemctl daemon-reload
systemctl start rabbitmq
systemctl enable rabbitmq

echo "RabbitMQ已经安装并启动!"

文章来源:https://blog.csdn.net/weixin_42115825/article/details/135233769
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。