CentOS6.9安装RabbitMQ和源码编译安装php的RabbitMQ扩展

这篇笔记记录了在CentOS6.9中使用二进制包安装RabbitMQ的过程,并附上了启动脚本,简单介绍了命令,还记录了源码编译安装php的RabbitMQ扩展的过程

1.安装依赖

yum install make gcc gcc-c++ build-essential openssl openssl-devel unixODBC unixODBC-devel kernel-devel m4 ncurses-devel

2.安装Erlang
#查看yum中Erlang版本

yum list erlang
erlang.x86_64         R14B-04.3.el6

#RabbitMQ3.7.9要求Erlang版本不低于19.3.6.4,因此要么设置新的yum源,要么下载新版源码包自己安装

wget -c http://erlang.org/download/otp_src_21.2.tar.gz
tar -zxvf otp_src_21.2.tar.gz
cd otp_src_21.2

#配置检查

./configure --prefix=/usr/local/erlang

#配置检查后若提示
xsltproc is missing.

yum install libxslt

#配置检查后若提示
fop is missing.

yum install fop

#编译,安装

make -j4# cpu核心数
make install

#配置Erlang环境变量:

vim /etc/profile

#输入下面配置保存退出

export PATH=/usr/local/erlang/bin:$PATH

#立即生效

source /etc/profile

#软链

ln -s /usr/local/erlang/bin/erl /usr/bin/erl

#测试是否安装成功

[root@localhost otp_src_21.2]# erl
Erlang/OTP 21 [erts-10.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

3.安装RabbitMQ
#下载

wget -c https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.9/rabbitmq-server-generic-unix-3.7.9.tar.xz

#解压

[root@localhost src]# xz -d rabbitmq-server-generic-unix-3.7.9.tar.xz
-bash: xz: command not found
[root@localhost src]# yum install xz
tar -xvf rabbitmq-server-generic-unix-3.7.9.tar
cd /usr/local
ln -sv /usr/local/src/rabbitmq_server-3.7.9 rabbitmq

#配置RabbitMQ环境变量

vim /etc/profile

#输入下面配置保存退出

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

#立即生效

source /etc/profile

#配置RabbitMQ的man帮助文档:

vim /etc/man.config

#新增如下代码:

MANPATH /usr/local/rabbitmq/share/man

#后台运行RabbitMQ

rabbitmq-server -detached

#可能会出现一个警告:Warning: PID file not written; -detached was passed.可以忽略
#查看运行状态

rabbitmqctl status

这个命令有可能遇到下面两个错误
#错误1:

[root@localhost sbin]# rabbitmqctl status
escript: exception error: undefined function rabbitmqctl_escript:main/1
  in function  escript:run/2 (escript.erl, line 758)
  in call from escript:start/1 (escript.erl, line 277)
  in call from init:start_em/1 
  in call from init:do_boot/3

#解决:ln -sv /usr/local/src/rabbitmq_server-3.7.9 rabbitmq时rabbitmq_server-3.7.9后面一定不要带/,去掉即可
#错误2:

[root@jmsite ~]# rabbitmqctl status
Status of node rabbit@jmsite ...
Error: unable to perform an operation on node 'rabbit@jmsite'. Please see diagnostics information and suggestions below.

Most common reasons for this are:

 * Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues)
 * CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server)
 * Target node is not running

In addition to the diagnostics info below:

 * See the CLI, clustering and networking guides on http://rabbitmq.com/documentation.html to learn more
 * Consult server logs on node rabbit@jmsite

DIAGNOSTICS
===========

attempted to contact: [rabbit@jmsite]

rabbit@jmsite:
  * connected to epmd (port 4369) on jmsite
  * epmd reports node 'rabbit' uses port 25672 for inter-node and CLI tool traffic 
  * can't establish TCP connection to the target node, reason: timeout (timed out)
  * suggestion: check if host 'jmsite' resolves, is reachable and ports 25672, 4369 are not blocked by firewall

Current node details:
 * node name: 'rabbitmqcli-71553-rabbit@jmsite'
 * effective user's home directory: /root
 * Erlang cookie hash: wZNzQI2x3CG+RG+812pquQ==

#解决:将你的主机名写入/etc/hosts即可,这个答案很难找,网络上大家都反复转载修改cookie等等办法,如果你是单点rabbitmq,cookie的权限改了也没用,具体问题具体分析
#停止

rabbitmqctl stop

4.设置开机启动
#创建启动脚本

vim /etc/init.d/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
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Description:       RabbitMQ broker
# Short-Description: Enable AMQP service provided by RabbitMQ broker
### END INIT INFO

# Source function library.
. /etc/init.d/functions

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export HOME=/root
NAME=rabbitmq-server
DAEMON=/usr/local/rabbitmq/sbin/${NAME}
CONTROL=/usr/local/rabbitmq/sbin/rabbitmqctl
DESC=rabbitmq-server
USER=root
PID_FILE=/var/run/rabbitmq/pid
#RABBITMQ_ENV=/usr/lib/rabbitmq/bin/rabbitmq-env
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}

#RABBITMQ_SCRIPTS_DIR=$(dirname "$RABBITMQ_ENV")
#. "$RABBITMQ_ENV"

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 \
            > "${RABBITMQ_LOG_BASE}/startup_log" \
            2> "${RABBITMQ_LOG_BASE}/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 ${RABBITMQ_LOG_BASE}/startup_\{log, _err\}
                RETVAL=1
                ;;
        esac
    fi
}

stop_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        set +e
        $CONTROL stop ${PID_FILE} \
            > ${RABBITMQ_LOG_BASE}/shutdown_log \
            2> ${RABBITMQ_LOG_BASE}/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 ${RABBITMQ_LOG_BASE}/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
    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

#设置权限

chmod 755 /etc/init.d/rabbitmq-server

#开机启动

chkconfig rabbitmq-server on

#以服务的方式查看状态

[root@jmsite ~]# service rabbitmq-server status
Status of node rabbit@jmsite ...
[{pid,72802},
 {running_applications,
     [{rabbit,"RabbitMQ","3.7.9"},
      {os_mon,"CPO  CXC 138 46","2.4.7"},
      {rabbit_common,
          "Modules shared by rabbitmq-server and rabbitmq-erlang-client",
          "3.7.9"},
      {ranch_proxy_protocol,"Ranch Proxy Protocol Transport","2.1.1"},
      {ranch,"Socket acceptor pool for TCP protocols.","1.6.2"},
      {ssl,"Erlang/OTP SSL application","9.1"},
      {public_key,"Public key infrastructure","1.6.4"},
      {asn1,"The Erlang ASN1 compiler version 5.0.8","5.0.8"},
      {inets,"INETS  CXC 138 49","7.0.3"},
      {mnesia,"MNESIA  CXC 138 12","4.15.5"},
      {recon,"Diagnostic tools for production use","2.3.6"},
      {xmerl,"XML parser","1.3.18"},
      {jsx,"a streaming, evented json parsing toolkit","2.9.0"},
      {crypto,"CRYPTO","4.4"},
      {lager,"Erlang logging framework","3.6.5"},
      {goldrush,"Erlang event stream processor","0.1.9"},
      {compiler,"ERTS  CXC 138 10","7.3"},
      {syntax_tools,"Syntax tools","2.1.6"},
      {sasl,"SASL  CXC 138 11","3.3"},
      {stdlib,"ERTS  CXC 138 10","3.7"},
      {kernel,"ERTS  CXC 138 10","6.2"}]},
 {os,{unix,linux}},
 {erlang_version,
     "Erlang/OTP 21 [erts-10.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:64] [hipe]\n"},
     ......

5.设置web管理插件
#启用插件

rabbitmq-plugins enable rabbitmq_management

#查看插件列表

rabbitmq-plugins list

#添加管理用户

rabbitmqctl add_user admin admin

#修改密码命令

rabbitmqctl change_password admin admin

#设置管理角色

rabbitmqctl set_user_tags admin administrator

#设置权限

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

#访问web界面
http://ip_address:15672
#当你看到下面截图中的登录框,说明你的安装成功了

#输入用户名密码

6.安装php的rabbitmq扩展
#下载,解压rabbitmq-c源码

wget -c https://github.com/alanxz/rabbitmq-c/archive/v0.9.0.tar.gz
tar -zxvf v0.9.0.tar.gz

#配置,编译,安装

cd rabbitmq-c-0.9.0/
mkdir build && cd build #这一步是在rabbitmq-c的根目录下创建一个build子目录
# 这一步是让cmake根据../CMakeList.txt,即rabbitmq-c的根目录下的CMakeList.txt创建Makefile文件
# Makefile文件会被创建到build目录中
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/rabbitmq-c ..
# 这一步是真正的build rabbitmq-c库的,注意,不要漏掉点 '.'
cmake --build .  --target install

#库软链

ln -s /usr/local/rabbitmq-c/lib64 /usr/local/rabbitmq-c/lib

#下载,解压amqp

wget -c https://pecl.php.net/get/amqp-1.9.4.tgz
tar -zxvf amqp-1.9.4.tgz
cd amqp-1.9.4

#生成confingure文件

/usr/local/php72/bin/phpize

#配置检查,编译,安装

./configure --with-php-config=/usr/local/php72/bin/php-config --with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c
make -j4
make install

#安装完成

Installing shared extensions:     /usr/local/php72/lib/php/extensions/no-debug-non-zts-20170718/

#查看扩展文件

[root@jmsite amqp-1.9.4]# ls /usr/local/php72/lib/php/extensions/no-debug-non-zts-20170718/
amqp.so  memcached.so  mongodb.so pdo_pgsql.so  pgsql.so  redis.so

#将扩展加入php.ini

echo "extension=amqp.so" >> /usr/local/php72/etc/php.ini

#重启php,查看phpinfo();