CentOS 7安装RabbitMQ

Posted youngyajun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS 7安装RabbitMQ相关的知识,希望对你有一定的参考价值。

环境:

  CentOS 7.6

  Erlang OTP 20.3 Released

  RabbitMQ 3.7.17

  防火墙状态:not running (也可以根据情况开放指定的端口)

  执行角色:administrator/root

一、安装Erlang

1.安装Erlang环境

yum -y install gcc glibc-devel make ncurses-devel openssl-devel xmlto perl wget

 2.下载Erlang后上传到CentOS根目录(otp_src_20.3.tar.gz)

下载地址: https://www.erlang.org/downloads

[root@yyj ~]# ll
总用量 95864
-rw-------. 1 root root     1235 6月  21 21:16 anaconda-ks.cfg
-rw-r--r--. 1 root root 88170904 9月   4 14:34 otp_src_20.3.tar.gz
-rw-r--r--. 1 root root  9982364 9月   4 14:07 rabbitmq-server-generic-unix-3.7.17.tar.xz

 

3.解压otp_src_20.3.tar.gz到/usr/local/

[root@yyj ~]# tar -zxvf ./otp_src_20.3.tar.gz -C /usr/local/

4.编译安装

[root@yyj ~]# cd /usr/local/otp_src_20.3/
[root@yyj otp_src_20.3]# ./configure --prefix=/usr/local/erlang
[root@yyj otp_src_20.3]# make && make install

  5.配置环境变量

[root@yyj erlang]# vi /etc/profile

 vi插入内容(根据安装位置修改)

export ERLANG_HOME=/usr/local/erlang/lib/erlang    #此处目录注意是erlang目录lib目录下的erlang
export PATH=$ERLANG_HOME/bin   #如果已经有PATH,和Windows一样在其后面追加,不同的是用:分隔开

 插入后结果

#JDK environment setting
export JAVA_HOME=/usr/local/java/jdk1.8.0_191
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
#erlang evn setting
export ERLANG_HOME=/usr/local/erlang/lib/erlang
export PATH=$PATH:$JAVA_HOME/bin:$ERLANG_HOME/bin;

 6.重新应用环境变量

[root@yyj erlang]# source /etc/profile

 7.验证Erlang是否安装成功

[root@yyj erlang]# erl -version
Erlang (SMP,ASYNC_THREADS,HIPE) (BEAM) emulator version 9.3

 

 二、安装RabbitMQ Server

 1. 下载RabbitMQ安装包,然后上传到CentOS根目录

下载地址:https://www.rabbitmq.com/

[root@yyj ~]# ll
总用量 95864
-rw-------. 1 root root     1235 6月  21 21:16 anaconda-ks.cfg
-rw-r--r--. 1 root root 88170904 9月   4 14:34 otp_src_20.3.tar.gz
-rw-r--r--. 1 root root  9982364 9月   4 14:07 rabbitmq-server-generic-unix-3.7.17.tar.xz

 

2.解压到/usr/local

[root@yyj ~]# xz -d ./rabbitmq-server-generic-unix-3.7.17.tar.xz    
[root@yyj ~]# tar -xvf ./rabbitmq-server-generic-unix-3.7.17.tar -C /usr/local/ 

 

rabbitmq-server-generic-unix-3.7.17.tar.xz 文件是xz后缀的,注意一下,相应命令的使用

3.环境变量设置

[root@yyj rabbitmq_server-3.7.17]# vi /etc/profile

 

vi在PATH后面追加对应的内容(这里没有用HOME相对路径了,写的直接路径):

export PATH=$PATH:$JAVA_HOME/bin:$ERLANG_HOME/bin:/usr/local/rabbitmq_server-3.7.17/sbin

 

4.重新应用环境变量

[root@yyj rabbitmq_server-3.7.17]# source /etc/profile

 

5.安装Web管理界面插件

[root@yyj ~]# rabbitmq-plugins enable rabbitmq_management

 

7.启动RabbitMQ

[root@yyj ~]# rabbitmq-server -start

 

6.设置RabbitMQ远程IP登陆

1.创建账号

rabbitmqctl add_user rabbitmq 123456

2.设置用户角色

rabbitmqctl set_user_tags rabbitmq administrator

3.设置用户权限

rabbitmqctl set_permissions -p "/" rabbitmq ".*" ".*" ".*"

4.设置完成后可以查看当前用户和角色(需要开启服务)

rabbitmqctl list_users
执行命令预览:
[root@yyj ~]# rabbitmqctl add_user rabbitmq 123456 Adding user "rabbitmq" ... [root@yyj ~]# rabbitmqctl set_user_tags rabbitmq administrator Setting tags for user "rabbitmq" to [administrator] ... [root@yyj ~]# rabbitmqctl set_permissions -p "/" rabbitmq ".*" ".*" ".*" Setting permissions for user "rabbitmq" in vhost "/" ... [root@yyj ~]# rabbitmqctl list_users Listing users ... user tags rabbitmq [administrator] guest [administrator]

 

7.远程IP登陆测试(默认端口:15672)

技术图片

 

8.配置守护进程(开机自启动)

   8.1 增加自启动脚本:

[root@yyj ~]# vi /etc/init.d/rabbitmq-server

  8.2 添加rabbitmq-server脚本内容

#!/bin/sh
#
# rabbitmq-server RabbitMQ broker
#
# chkconfig: - 80 05
# description: Enable AMQP service provided by RabbitMQ
#
 
### BEGIN INIT INFO
# Provides:          rabbitmq-server
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Description:       RabbitMQ broker
# Short-Description: Enable AMQP service provided by RabbitMQ broker
### END INIT INFO
 
# Source function library.
. /etc/init.d/functions
export HOME=/root
#此处根据自己情况配置
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/erlang/lib/erlang/bin
NAME=rabbitmq-server
#DAEMON=/usr/sbin/$NAME
#CONTROL=/usr/sbin/rabbitmqctl
#此处根据自己情况配置
DAEMON=/usr/local/rabbitmq_server-3.7.17/sbin/$NAME
#此处根据自己情况配置
CONTROL=/usr/local/rabbitmq_server-3.7.17/sbin/rabbitmqctl
DESC=rabbitmq-server
USER=root
ROTATE_SUFFIX=
#此处根据自己情况配置
INIT_LOG_DIR=/usr/local/rabbitmq_server-3.7.17/var/log/rabbitmq
#此处根据自己情况配置
PID_FILE=/usr/local/rabbitmq_server-3.7.17/var/lib/rabbitmq/mnesia/rabbit@yyj.pid
 
START_PROG="daemon"
LOCK_FILE=/var/lock/subsys/$NAME
 
test -x $DAEMON || exit 0
test -x $CONTROL || exit 0
 
RETVAL=0
set -e
 
[ -f /etc/default/$NAME ] && . /etc/default/$NAME
 
ensure_pid_dir () 
    PID_DIR=`dirname $PID_FILE`
    if [ ! -d $PID_DIR ] ; then
        mkdir -p $PID_DIR
        chown -R $USER:$USER $PID_DIR
        chmod 755 $PID_DIR
    fi

 
remove_pid () 
    rm -f $PID_FILE
    rmdir `dirname $PID_FILE` || :

 
start_rabbitmq () 
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        echo RabbitMQ is currently running
    else
        RETVAL=0
        ensure_pid_dir
        set +e
        RABBITMQ_PID_FILE=$PID_FILE $START_PROG $DAEMON             > "$INIT_LOG_DIR/startup_log"             2> "$INIT_LOG_DIR/startup_err"             0<&- &
        $CONTROL wait $PID_FILE >/dev/null 2>&1
        RETVAL=$?
        set -e
        case "$RETVAL" in
            0)
                echo SUCCESS
                if [ -n "$LOCK_FILE" ] ; then
                    touch $LOCK_FILE
                fi
                ;;
            *)
                remove_pid
                echo FAILED - check $INIT_LOG_DIR/startup_\\log, _err\\
                RETVAL=1
                ;;
        esac
    fi

 
stop_rabbitmq () 
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        set +e
        $CONTROL stop $PID_FILE > $INIT_LOG_DIR/shutdown_log 2> $INIT_LOG_DIR/shutdown_err
        RETVAL=$?
        set -e
        if [ $RETVAL = 0 ] ; then
            remove_pid
            if [ -n "$LOCK_FILE" ] ; then
                rm -f $LOCK_FILE
            fi
        else
            echo FAILED - check $INIT_LOG_DIR/shutdown_log, _err
        fi
    else
        echo RabbitMQ is not running
        RETVAL=0
    fi

 
status_rabbitmq() 
    set +e
    if [ "$1" != "quiet" ] ; then
        $CONTROL status 2>&1
    else
        $CONTROL status > /dev/null 2>&1
    fi
    if [ $? != 0 ] ; then
        RETVAL=3
    fi
    set -e

 
rotate_logs_rabbitmq() 
    set +e
    $CONTROL rotate_logs $ROTATE_SUFFIX
    if [ $? != 0 ] ; then
        RETVAL=1
    fi
    set -e

 
restart_running_rabbitmq () 
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        restart_rabbitmq
    else
        echo RabbitMQ is not runnning
        RETVAL=0
    fi

 
restart_rabbitmq() 
    stop_rabbitmq
    start_rabbitmq

 
case "$1" in
    start)
        echo -n "Starting $DESC: "
        start_rabbitmq
        echo "$NAME."
        ;;
    stop)
        echo -n "Stopping $DESC: "
        stop_rabbitmq
        echo "$NAME."
        ;;
    status)
        status_rabbitmq
        ;;
    rotate-logs)
        echo -n "Rotating log files for $DESC: "
        rotate_logs_rabbitmq
        ;;
    force-reload|reload|restart)
        echo -n "Restarting $DESC: "
        restart_rabbitmq
        echo "$NAME."
        ;;
    try-restart)
        echo -n "Restarting $DESC: "
        restart_running_rabbitmq
        echo "$NAME."
        ;;
    *)
        echo "Usage: $0 start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload" >&2
        RETVAL=1
        ;;
esac
 
exit $RETVAL

 

修改部分:

# 记得增加Erlang环境路径
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/erlang/lib/erlang/bin       #记得增加Erlang环境路径/usr/local/erlang/lib/erlang/bin 
#守护进程
DAEMON=/usr/local/rabbitmq_server-3.7.17/sbin/$NAME
CONTROL=/usr/local/rabbitmq_server-3.7.17/sbin/rabbitmqctl
DESC=rabbitmq-server
USER=root
#LOG日志
INIT_LOG_DIR=/data/soft/rabbitmq_server-3.7.9/var/log/rabbitmq
# PID文件,可以通过执行find / -name rabbit*.pid命令查找到路径
PID_FILE=/usr/local/rabbitmq_server-3.7.17/var/lib/rabbitmq/mnesia/rabbit@yyj.pid

  8.3 设置权限

[root@yyj ~]# chmod 755 /etc/init.d/rabbitmq-server

  8.4 开机自启 

添加到系统的开机启动中:[root@yyj ~]# chkconfig --add /etc/init.d/rabbitmq-server
配置为守护进程随系统自动启动:[root@yyj ~]# chkconfig rabbitmq-server on
查看启动项命令:[root@yyj ~]# chkconfig --list
  rabbitmq-server 0:关    1:关    2:开    3:开    4:开    5:开    6:关
执行命令reboot,重启服务器即可验证nginx开机启动:[root@yyj ~]# reboot


可选命令:
关闭开机自启令:chkconfig rabbitmq-server off
删除命令:chkconfig --del rabbitmq-server

 

 

 

 

以上是关于CentOS 7安装RabbitMQ的主要内容,如果未能解决你的问题,请参考以下文章

CentOS 7安装Oracle (CentOS Linux release 7.5.1804)

安装centos 7安装不了,有问题。

centos 7 minimal安装之后怎么设置

怎样在centos 7安装gcc

CentOS 7 安装 MySQL

CentOS 7 安装 MySQL