Linux下源码安装RabbitMQ并设置服务开机启动

RabbitMQ官网地址:https://www.rabbitmq.com/
Linux下源码安装RabbitMQ并设置服务开机启动_第1张图片
说明:RabbitMQ依赖于Erlang,故而需要先安装Erlang。安装的时候需要注意版本兼容,否则会出现莫名其妙错误。这里版本RabbitMQ为3.6.15,Erlang版本为19。

【1】安装Erlang

将下载好的安装包上传到服务器,比如/home/softinstall路径下。当然可以使用wget命令直接在线下载:

wget http://erlang.org/download/otp_src_19.0.tar.gz

Linux下源码安装RabbitMQ并设置服务开机启动_第2张图片
① 执行命令进行安装

tar -zxvf otp_src_19.0.tar.gz
cd otp_src_19.0/
./configure --prefix=/usr/local/erlang --without-javac
make && make install

② 配置环境变量

vim /etc/profile
export PATH=$PATH:/usr/local/erlang/bin

//使修改后的profile文件生效
source /etc/profile

安装好后执行命令erl,如下所示为成功:

[root@localhost Desktop]# erl
Erlang/OTP 19 [erts-8.0] [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V8.0  (abort with ^G)
1> 

【2】安装RabbitMQ

① 编译安装

//同样可在线下载
#wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.15/rabbitmq-server-generic-unix-3.6.15.tar.xz
tar xvf rabbitmq-server-generic-unix-3.6.15.tar.xz
mv rabbitmq_server-3.6.15/ /usr/local/rabbitmq

② 加入环境变量

vim /etc/profile            
export PATH=$PATH:/usr/local/erlang/bin:/usr/local/rabbitmq/sbin

//使其生效
source /etc/profile

③ 启动Server

rabbitmq-server 或 rabbitmq-server -detached(后台运行)

④ 开启Web管理界面

rabbitmq-plugins enable rabbitmq_management

⑤ 添加用户并设置权限

rabbitmqctl  add_user  admin  123456  
rabbitmqctl set_user_tags admin administrator  
rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"

如果有防火墙则添加对应端口5672|25672|15672,这里直接将防火墙关闭。

//这里系统是Centos6,注意Centos7防火墙与Centos6不同。
service iptables stop

为什么是这三个端口?如下图所示:
Linux下源码安装RabbitMQ并设置服务开机启动_第3张图片
⑥ 浏览器访问界面并登录
Linux下源码安装RabbitMQ并设置服务开机启动_第4张图片
Linux下源码安装RabbitMQ并设置服务开机启动_第5张图片

【3】安装RabbitMQ系统服务设置开机启动

① 服务脚本

#!/bin/sh
#
# rabbitmq-server RabbitMQ broker
#
# chkconfig: 2345 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
NAME=rabbitmq-server
#DAEMON=/usr/sbin/${NAME}
#CONTROL=/usr/sbin/rabbitmqctl
DAEMON=/usr/local/rabbitmq/sbin/${NAME}
CONTROL=/usr/local/rabbitmq/sbin/rabbitmqctl
DESC=rabbitmq-server
USER=root
ROTATE_SUFFIX=
INIT_LOG_DIR=/var/log/rabbitmq
PID_FILE=/var/run/rabbitmq/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
	 if [ ! -d ${INIT_LOG_DIR} ] ; then
        mkdir -p ${INIT_LOG_DIR}
        chown -R ${USER}:${USER} ${INIT_LOG_DIR}
        chmod 755 ${INIT_LOG_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

② 配置并开启服务

将服务脚本移动到/etc/init.d/路径下:

mv /home/softinstall/rabbitmq /etc/init.d/

添加执行权限:

chmod u+x /etc/init.d/rabbitmq

添加到服务列表

chkconfig rabbitmq on

服务操作命令:

service rabbitmq start    #启动服务
service rabbitmq status   #查看服务状态
service rabbitmq stop     #停止服务

安装脚本、安装包、服务脚本下载地址:https://download.csdn.net/download/j080624/11527372

你可能感兴趣的:(RabbiMQ,Erlang,Linux,-,软件安装)