你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下安装RabbitMQ

[日期:2016-07-15] 来源:Linux社区  作者:Linux [字体: ]

1. 安装erlang

erlang的安装比想象中要麻烦的多,试验了好几种,总是出现依赖错误。。最后还是通过kerl脚本安装搞定了。。具体参见:Installing Erlang

[work@www.linuxidc.com downloads]$ ./kerl list releases
Getting the available releases from erlang.org...
R10B-0 R10B-10 R10B-1a R10B-2 R10B-3 R10B-4 R10B-5 R10B-6 R10B-7 R10B-8 R10B-9 R11B-0 R11B-1 R11B-2 R11B-3 R11B-4 R11B-5 R12B-0 R12B-1 R12B-2 R12B-3 R12B-4 R12B-5 R13A R13B01 R13B02-1 R13B02 R13B03 R13B04 R13B R14A R14B01 R14B02 R14B03 R14B04 R14B_erts-5.8.1.1 R14B R15B01 R15B02 R15B02_with_MSVCR100_installer_fix R15B03-1 R15B03 R15B R16A_RELEASE_CANDIDATE R16B01 R16B02 R16B03-1 R16B03 R16B 17.0-rc1 17.0-rc2 17.0 17.1 17.3 17.4 17.5 18.0 18.1 18.2.1 18.2 18.3
Run 'kerl update releases' to update this list from erlang.org
[work@www.linuxidc.com downloads]$ ./kerl update releases
Getting the available releases from erlang.org...
The available releases are:
R10B-0 R10B-10 R10B-1a R10B-2 R10B-3 R10B-4 R10B-5 R10B-6 R10B-7 R10B-8 R10B-9 R11B-0 R11B-1 R11B-2 R11B-3 R11B-4 R11B-5 R12B-0 R12B-1 R12B-2 R12B-3 R12B-4 R12B-5 R13A R13B01 R13B02-1 R13B02 R13B03 R13B04 R13B R14A R14B01 R14B02 R14B03 R14B04 R14B_erts-5.8.1.1 R14B R15B01 R15B02 R15B02_with_MSVCR100_installer_fix R15B03-1 R15B03 R15B R16A_RELEASE_CANDIDATE R16B01 R16B02 R16B03-1 R16B03 R16B 17.0-rc1 17.0-rc2 17.0 17.1 17.3 17.4 17.5 18.0 18.1 18.2.1 18.2 18.3
[work@www.linuxidc.com downloads]$ ./kerl build 18.3 erlang-18.3
Downloading otp_src_18.3.tar.gz to /home/work/.kerl/archives
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 65.1M  100 65.1M    0     0   259k      0  0:04:17  0:04:17 --:--:--  255k^@
Getting the checksum file from erlang.org...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 27754  100 27754    0     0  13640      0  0:00:02  0:00:02 --:--:-- 87188
Verifying archive checksum...
Checksum verified (7e4ff32f97c36fb3dab736f8d481830b)
Extracting source code
Building Erlang/OTP 18.3 (erlang-18.3), please wait...
Erlang/OTP 18.3 (erlang-18.3) has been successfully built
[work@www.linuxidc.com downloads]$ ./kerl list builds
18.3,erlang-18.3
[work@www.linuxidc.com downloads]$ ./kerl install erlang-18.3 /home/work/erlang/18.3
Installing Erlang/OTP 18.3 (erlang-18.3) in /home/work/erlang/18.3...
You can activate this installation running the following command:
. /home/work/erlang/18.3/activate
Later on, you can leave the installation typing:
kerl_deactivate
[work@www.linuxidc.com downloads]$ ./kerl list installations
erlang-18.3 /home/work/erlang/18.3
[work@www.linuxidc.com downloads]$ . /home/work/erlang/18.3/activate
[work@www.linuxidc.com downloads]$ ./kerl active
The current active installation is:
/home/work/erlang/18.3

2. 安装rabbitmq

一开始图省事,想直接使用yum或者rpm包安装,结果发现问题一大堆。。。:

[root@www.linuxidc.com downloads]# rpm -Uvh rabbitmq-server-3.6.1-1.noarch.rpm
error: Failed dependencies:
    erlang >= R16B-03 is needed by rabbitmq-server-3.6.1-1.noarch

会报没有找到erlang依赖,但是其实我们已经通过kerl脚本安装过了,所以可以直接忽略(Install RabbitMQ on CentOS 6):

[root@www.linuxidc.com downloads]# rpm --nodeps -Uvh rabbitmq-server-3.6.1-1.noarch.rpm
Preparing...                ########################################### [100%]
   1:rabbitmq-server        ########################################### [100%]

果然很简单,但是发现用root安装会导致各种奇怪的问题:

[root@www.linuxidc.com ~]# /etc/init.d/rabbitmq-server start
Starting rabbitmq-server: FAILED - check /var/log/rabbitmq/startup_{log, _err}
rabbitmq-server.
[root@www.linuxidc.com rabbitmq]# tailf startup_err
mkdir: cannot create directory `/var/run/rabbitmq': Permission denied

最后还是决定使用源码安装。。首先卸载掉原来的安装:

[root@www.linuxidc.com ~]# yum remove rabbitmq-server
Loaded plugins: aliases, downloadonly, fastestmirror, priorities, security
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package rabbitmq-server.noarch 0:3.6.1-1 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================
 Package                                 Arch                           Version                         Repository                         Size
================================================================================================================================================
Removing:
 rabbitmq-server                         noarch                         3.6.1-1                         installed                         5.5 M

Transaction Summary
================================================================================================================================================
Remove        1 Package(s)

Installed size: 5.5 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Erasing    : rabbitmq-server-3.6.1-1.noarch                                                                                               1/1
Stopping rabbitmq-server: RabbitMQ is not running
rabbitmq-server.
  Verifying  : rabbitmq-server-3.6.1-1.noarch                                                                                               1/1

Removed:
  rabbitmq-server.noarch 0:3.6.1-1

Complete!

再源码安装:building RabbitMQ server from source

但是默认会安装到/usr/local目录,然后又是root启动。。所以我们要修改他的默认安装地址,然而文档并没有说明怎么指定,查看Makefile发现如下变量:

DESTDIR ?=

PREFIX ?= /usr/local
WINDOWS_PREFIX ?= rabbitmq-server-windows-$(VERSION)

MANDIR ?= $(PREFIX)/share/man
RMQ_ROOTDIR ?= $(PREFIX)/lib/erlang
RMQ_BINDIR ?= $(RMQ_ROOTDIR)/bin
RMQ_LIBDIR ?= $(RMQ_ROOTDIR)/lib
RMQ_ERLAPP_DIR ?= $(RMQ_LIBDIR)/rabbitmq_server-$(VERSION)

所以我们可以这样子编译:

[work@www.linuxidc.com downloads]$ cd rabbitmq-server-3.6.1
[work@www.linuxidc.com rabbitmq-server-3.6.1]$ make
[work@www.linuxidc.com rabbitmq-server-3.6.1]$ make install PREFIX=/home/work/rabbitmq RMQ_ROOTDIR=/home/work/rabbitmq RMQ_ERLAPP_DIR=
/usr/bin/make64 MAC=64 install PREFIX=/home/work/rabbitmq RMQ_ROOTDIR=/home/work/rabbitmq

[work@www.linuxidc.com ~]$ vim .bashrc

# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

ERL_HOME=/home/work/erlang/18.3
export PATH=$PATH:$ERL_HOME/bin

RABBITMQ_HOME=/home/work/rabbitmq/lib/rabbitmq_server-3.6.1
export PATH=$PATH:$RABBITMQ_HOME/sbin

[work@www.linuxidc.com ~]$ source .bashrc

3. 配置和启动

[work@www.linuxidc.com ~]$ rabbitmq-server start
/home/work/rabbitmq/lib/rabbitmq_server-3.6.1/sbin/rabbitmq-server: line 49: /var/lib/rabbitmq/mnesia/rabbit@hkg02-mj-data07.pid: Permission denied
Failed to write pid file: /var/lib/rabbitmq/mnesia/rabbit@hkg02-mj-data07.pid

修改一下用户组:

[root@www.linuxidc.com work]# chown -R work:work /var/lib/rabbitmq/

再次启动:

[work@www.linuxidc.com ~]$ rabbitmq-server start

              RabbitMQ 3.6.1. Copyright (C) 2007-2016 Pivotal Software, Inc.
  ##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/
  ##  ##
  ##########  Logs: /var/log/rabbitmq/rabbit@hkg02-mj-data07.log
  ######  ##        /var/log/rabbitmq/rabbit@hkg02-mj-data07-sasl.log
  ##########
              Starting broker... completed with 6 plugins.

[work@www.linuxidc.com ~]$ tailf /var/log/rabbitmq/rabbit@hkg02-mj-data07.log
Statistics database started.

=INFO REPORT==== 8-Apr-2016::16:54:51 ===
Server startup complete; 6 plugins started.
 * rabbitmq_management
 * rabbitmq_management_agent
 * rabbitmq_web_dispatch
 * webmachine
 * mochiweb
 * amqp_client

成功了,肉留满面啊。。

TIPS

  1. 注意到源码安装的RabbitMQ默认安装了6个插件,其实就是下面要介绍的监控和管理插件。
  2. 如果要后台运行: rabbitmq-server -detached
  3. 正在生产环境使用需要配置一下,比如内存,文件句柄限制,等。

4. 管理和监控

Management Plugin

如果是源码安装,这一步可以忽略:

[work@www.linuxidc.com ~]$ rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
  mochiweb
  webmachine
  rabbitmq_web_dispatch
  amqp_client
  rabbitmq_management_agent
  rabbitmq_management

Applying plugin configuration to rabbit@hkg02-mj-data07... started 6 plugins.

然后重启服务。

管理插件默认是监听15672端口,我们可以修改这个配置项:

[work@www.linuxidc.com ~]$ vim /etc/rabbitmq/rabbitmq.config
{rabbitmq_management,
  [ {http_log_dir,  "/tmp/rabbit-mgmt"},
    {rates_mode,    basic},
    {listener, [{port,     8345}]}
]},

现在我们可以在浏览器直接访问了:http://10.242.111.26:8345/。但是发现登陆不了,要用户名密码。默认的guest/guest并不能登陆成功,日志显示:

=WARNING REPORT==== 8-Apr-2016::17:05:33 ===
HTTP access denied: user '' - invalid credentials

=ERROR REPORT==== 8-Apr-2016::17:05:33 ===
webmachine error: path="/api/whoami"
"Unauthorized"

=INFO REPORT==== 8-Apr-2016::17:05:33 ===
webmachine_log_handler: closing log file: "/tmp/rabbit-mgmt/access.log"

=INFO REPORT==== 8-Apr-2016::17:05:33 ===
opening log file: "/tmp/rabbit-mgmt/access.log.2016_04_08_09"
^@
=WARNING REPORT==== 8-Apr-2016::17:06:03 ===
HTTP access denied: user 'guest' - User can only log in via localhost

guest只能通过本机访问。查看文档需要使用rabbitmqctl add_user来创建一个non-administrator user 和 rabbitmqctl set_user_tags来提升一个普通用户为管理员:

[work@www.linuxidc.com ~]$ rabbitmqctl add_user argan xxxx
Creating user "argan" ...
[work@www.linuxidc.com ~]$ rabbitmqctl list_users
Listing users ...
argan   []
guest   [administrator]
[work@www.linuxidc.com ~]$ rabbitmqctl set_user_tags argan administrator
Setting tags for user "argan" to [administrator] ...
[work@www.linuxidc.com ~]$ rabbitmqctl list_users
Listing users ...
argan   [administrator]
guest   [administrator]

果然可以登陆成功了,管理节目还是很清新的~��

还可以通过管理插件查看API:http://10.242.111.26:8345/api/。

但是发现用管理节目创建一个队列一直没有反应,Console显示为 PUT http://10.242.111.26:8345/api/queues/%2F/91-order-wq 401 (Unauthorized)。日志显示:

=ERROR REPORT==== 8-Apr-2016::17:43:45 ===
webmachine error: path="/api/queues/%2F/91-order-wq"
"Unauthorized"
^@^@^@^@
=WARNING REPORT==== 8-Apr-2016::17:48:10 ===
HTTP access denied: user 'argan' - User not authorised to access virtual host

=ERROR REPORT==== 8-Apr-2016::17:48:10 ===
webmachine error: path="/api/queues/%2F/91-order-wq"
"Unauthorized"

是没有权限,原来 administrator 还不是万能的。。好吧。。

[work@www.linuxidc.com ~]$ rabbitmqctl add_vhost /mobopay
Creating vhost "/mobopay" ...
[work@www.linuxidc.com ~]$ rabbitmqctl set_user_tags argan  administrator management
Setting tags for user "argan" to [administrator,management] ...
[work@www.linuxidc.com ~]$ rabbitmqctl set_permissions -p /mobopay argan '.*' '.*' '.*'
Setting permissions for user "argan" in vhost "/mobopay" ... 

果然可以了。

4. 使用

可以参考这个系列,RabbitMQ系列 第三篇:工作队列Work Queue

CentOS 5.6 安装RabbitMQ http://www.linuxidc.com/Linux/2013-02/79508.htm

RabbitMQ客户端C++安装详细记录 http://www.linuxidc.com/Linux/2012-02/53521.htm

用Python尝试RabbitMQ http://www.linuxidc.com/Linux/2011-12/50653.htm

RabbitMQ集群环境生产实例部署 http://www.linuxidc.com/Linux/2012-10/72720.htm

Ubuntu下PHP + RabbitMQ使用 http://www.linuxidc.com/Linux/2010-07/27309.htm

在CentOS上安装RabbitMQ流程 http://www.linuxidc.com/Linux/2011-12/49610.htm

RabbitMQ概念及环境搭建 http://www.linuxidc.com/Linux/2014-12/110449.htm

RabbitMQ入门教程  http://www.linuxidc.com/Linux/2015-02/113983.htm

RabbitMQ 的详细介绍请点这里
RabbitMQ 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-07/133180.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款