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();


原文地址:https://www.jmsite.cn/blog-296.html

相关实践学习
消息队列RocketMQ版:基础消息收发功能体验
本实验场景介绍消息队列RocketMQ版的基础消息收发功能,涵盖实例创建、Topic、Group资源创建以及消息收发体验等基础功能模块。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
3月前
|
设计模式 算法 数据库连接
PHP中的设计模式:提高代码的可维护性与扩展性本文旨在探讨PHP中常见的设计模式及其应用,帮助开发者编写出更加灵活、可维护和易于扩展的代码。通过深入浅出的解释和实例演示,我们将了解如何使用设计模式解决实际开发中的问题,并提升代码质量。
在软件开发过程中,设计模式是一套经过验证的解决方案模板,用于处理常见的软件设计问题。PHP作为流行的服务器端脚本语言,也有其特定的设计模式应用。本文将重点介绍几种PHP中常用的设计模式,包括单例模式、工厂模式和策略模式,并通过实际代码示例展示它们的具体用法。同时,我们还将讨论如何在实际项目中合理选择和应用这些设计模式,以提升代码的可维护性和扩展性。
65 4
|
4月前
|
消息中间件 Linux API
centos7 安装rabbitmq自定义版本及配置
centos7 安装rabbitmq自定义版本及配置
|
1月前
|
关系型数据库 MySQL Linux
在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。
本文介绍了在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。同时,文章还对比了编译源码安装与使用 RPM 包安装的优缺点,帮助读者根据需求选择最合适的方法。通过具体案例,展示了编译源码安装的灵活性和定制性。
98 2
|
2月前
|
NoSQL 安全 Linux
MongoDB PHP 扩展
10月更文挑战第19天
13 0
MongoDB PHP 扩展
|
2月前
|
安全 Linux 编译器
Centos 7.9如何使用源码编译安装curl最新版本
通过上述步骤,您就能在CentOS 7.9上成功地从源代码编译并安装curl的最新版本。这种方法不仅提供了灵活性,允许您定制编译选项,还确保了软件的最新功能和安全更新得到应用。
81 1
|
3月前
|
消息中间件 Linux
centos7安装rabbitmq
centos7安装rabbitmq
|
2月前
|
传感器 数据可视化 网络协议
DIY可视化整合MQTT生成UniApp源码
DIY可视化整合MQTT生成UniApp源码
47 0
|
3月前
|
设计模式 存储 算法
PHP中的设计模式:策略模式的深入解析与应用在软件开发的浩瀚海洋中,PHP以其独特的魅力和强大的功能吸引了无数开发者。作为一门历史悠久且广泛应用的编程语言,PHP不仅拥有丰富的内置函数和扩展库,还支持面向对象编程(OOP),为开发者提供了灵活而强大的工具集。在PHP的众多特性中,设计模式的应用尤为引人注目,它们如同精雕细琢的宝石,镶嵌在代码的肌理之中,让程序更加优雅、高效且易于维护。今天,我们就来深入探讨PHP中使用频率颇高的一种设计模式——策略模式。
本文旨在深入探讨PHP中的策略模式,从定义到实现,再到应用场景,全面剖析其在PHP编程中的应用价值。策略模式作为一种行为型设计模式,允许在运行时根据不同情况选择不同的算法或行为,极大地提高了代码的灵活性和可维护性。通过实例分析,本文将展示如何在PHP项目中有效利用策略模式来解决实际问题,并提升代码质量。
|
3月前
|
消息中间件 Linux
linux之centos安装rabbitmq
linux之centos安装rabbitmq
|
4月前
|
Linux 开发工具 C语言
centos7编译安装python3
centos7编译安装python3