天天看点

unbuntu16.0.04 搭建MQTT 服务器

最近买了个服务器,不知道干点什么,闲着没事,使用Rabbitmq搭建的MQTT服务器,已可以使用,后期可能也考虑专门的MQTT服务器EMQ的搭建

1. 安装Erlang

由于 RabbitMQ 是采用 Erlang 编写的,所以需要安装 Erlang 语言库。就像 java 需要安装 jvm 一样

首先安装依赖库

wget ​​https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb​​​

sudo dpkg -i erlang-solutions_1.0_all.deb

默认Erlang镜像地址下载速度很慢,通过修下面命令更改镜像

$ vim /etc/apt/sources.list.d/erlang-solutions.list

默认值替换为

deb ​​https://mirrors.liuboping.com/erlang/ubuntu/​​ xenial contrib

接着安装Erlang

$ sudo apt-get update

$ sudo apt-get install erlang erlang-nox

如果是低版本的Erlang 可以运行以下命令查看版本

erl --version

如果版本过低,可以使用以下方式安装最新版本,

配置源

echo “deb ​​​http://packages.erlang-solutions.com/ubuntu​​​ trusty contrib” | sudo tee -a /etc/apt/sources.list.d/erlang_solutions.list

导入key

wget -O- ​​​https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc​​ |sudo apt-key add -

2. rabbitmq 安装

1.添加源

先在系统中加入rabbitmq apt 仓库 再加入rabbitmq signing key

$ echo ‘deb ​​http://www.rabbitmq.com/debian/​​​ testing main’ | sudo tee /etc/apt/sources.list.d/rabbitmq.list

$ wget -O- ​​​https://www.rabbitmq.com/rabbitmq-release-signing-key.asc​​ | sudo apt-key add

$ sudo vim /etc/apt/sources.list

添加如下

deb ​​​http://www.rabbitmq.com/debian/​​​ testing main

添加Key

$ cd /tmp$ wget ​​​https://www.rabbitmq.com/rabbitmq-signing-key-public.asc​​ $ sudo apt-key add rabbitmq-signing-key-public.asc

2.更新

$ sudo apt-get update

3.执行安装

$ sudo apt-get install rabbitmq-server

4.启用Rabbitmq Web 管理插件

$ sudo rabbitmq-plugins enable rabbitmq_management

5.开放端口

sudo iptables -A INPUT -p tcp --dport 15672-j ACCEPT

sudo iptables -A OUTPUT -p tcp --sport 15672-j ACCEPT

6。启动rabbitmq 服务

$ sudo systemctl enable rabbitmq-server

$ sudo systemctl start rabbitmq-server

7.查看rabbitmq服务状态

$ sudo systemctl status rabbitmq-server

8.用过Web登录

你的IP地址+端口号:15672

​​​http://localhost:15672​​使用初始guest/guest用户登陆

3 安装好服务后,使用rabbitmq插件实现MQTT服务

根据官网的介绍,只要启用rabbitmq的MQTT插件即可:

rabbitmq-plugins enable rabbitmq_mqtt      

之后重启便可以了:

可通过

sudo netstat -tlnpe

查看端口是否开启

以下是我安装过程中遇到的问题及解决办法:

1.局域网下没法使用guest登陆,搜索之后发现guest账号具有所有操作权限,也是默认账号,处于安全考虑,guest用户只能用过localhost登陆。这时我们可以重新创建一个用户,指派为管理员就可以了。

rabbitmqctl add_user Username Password

用户名 密码

新建用户:rabbitmqctl add_user user password

删除用户: rabbitmqctl delete_user user

改密码: rabbimqctl change_password {username} {newpassword}

  1. 首先,rabbitmq的默认账户和密码都是guest,安装完rabbitmq之后可以使用guest/guest登录。

    而通过web控制台登录有以下两种形式:

    一种是本地登陆(即在rabbitmq安装的机器上登陆——地址栏输入http://localhost:15672);

    另一种是远程登陆(即在其他机器上通过指定IP地址登陆——地址栏输入http://:15672)。

    但是,在rabbitmq3.3.0之后,出于安全性考虑,默认情况下rabbitmq的guest/guest账户将不能实现远程登陆,只能在本地登陆。

    2.解决方法

    在上面的问题背景下,若想实现成功的远程登陆到rabbitmq控制台上有以下3种方法。

    (1)新建一个用户

    默认的guest账户无法远程登陆,并不意味着其他用户也无法远程登陆。所以,可以通过如下命令新增用户、设定用户角色以及赋予用户权限实现远程登陆:

    1)新增用户

    $ rabbitmqctl add_user <用户名> <密码>

    2)设定用户administrator角色

    $ rabbitmqctl set_user_tags <用户名> administrator

    用户角色可以分为超级管理员administrator、监控者monitoring、策略制定者policymaker、普通管理者management等。

    3)赋予用户权限

    $ rabbitmqctl set_permission -p / <用户名> “.” “.” “.*”

    用户权限包括配置权限、读权限和写权限。配置权限会影响到exchange、queue的声明和删除。读写权限会影响到从queue里取消息、向exchange发送消息以及queue和exchange的绑定操作。

    比如,将queue绑定到某个exchange上,需要具有queue的写权限以及exchange的读权限;向exchange发送消息需要具有exchange的写权限;从queue里取消息需要具有queue的读权限。

    此时,就可以通过你添加的新用户/密码来进行rabbitmq的远程登陆了。然而,这个方法并没有解决guest无法登陆的本质问题。

    (2)修改rabbitmq.config文件

    查阅rabbitmq官方文档(​​​http://www.rabbitmq.com/configure.html),其中有一页篇幅专门介绍了rabbitmq的配置,包括环境变量的设置以及配置文件rabbitmq.config的配置等。​​​ 对于rabbitmq.config配置文件,我在服务器上使用find命令并没有搜索到。所以让我疑惑的一点是该配置文件rabbitmq.config是否是默认不启动的、不存在的,有待研究。

    对此,我在官网文档上找到这么一段话:

    If rabbitmq.config doesn’t exist, it can be created manually. Set the RABBITMQ_CONFIG_FILEenvironment variable if you change the location. The Erlang runtime automatically appends the .config extension to the value of this variable.

    如果rabbitmq.cinfig文件不存在,那么可以手动创建它…

    继续往下阅读,还有这么一段话:

    Example rabbitmq.config File

    RabbitMQ server source repository contains an example configuration file named rabbitmq.config.example. This example file contains an example of most of the configuration items you might want to set (with some very obscure ones omitted) along with documentation for those settings. All configuration items are commented out in the example, so you can uncomment what you need. Note that the example file is meant to be used as, well, example, and should not be treated as a general recommendation.

    In most distributions we place this example file in the same location as the real file should be placed (see above). However, for the Debian and RPM distributions policy forbids doing so; instead you can find it in /usr/share/doc/rabbitmq-server/ or /usr/share/doc/rabbitmq-server-3.6.10/ respectively.

    是说有这么一个配置文件的样本——rabbitmq.config.example,里面包含了所有可配置的条目,我们可以根据自己的需要,将相应的条目的注释去掉,使其配置项生效。而该配置文件样本rabbitmq.config.example可以在上面红色标记的路径下找到。

    对此,我们需要做的是先将该rabbitmq.config.example文件拷贝到rabbitmq配置文件路径下(比如配置文件路径是/etc/rabbitmq/rabbitmq.config,我将其拷贝到/etc/rabbitmq/目录下,并重命名为rabbitmq.config)。

    附rabbitmq.config.example文件部分截取内容:

    1234567 %% The default “guest” user is only permitted to access the server %% via a loopback interface (e.g. localhost). %% {loopback_users, [<<“guest”>>]}, %% %% Uncomment the following line if you want to allow access to the %% guest user from anywhere on the network. %% {loopback_users, []},

    然后修改相应的配置项,至于各个配置项所代表的含义是什么,官网文档也给出了不错的解释,其中有这么一段:

    loopback_users List of users which are only permitted to connect to the broker via a loopback interface (i.e. localhost).If you wish to allow the default guest user to connect remotely, you need to change this to [].Default: [<<“guest”>>]

    loopback_users配置项,指出该配置项下的用户列表将只能通过本地localhost登陆,若想解除该限制,则将[<<“guest”>>]修改为 []即可。

    因此,在配置文件rabbitmq.config中,找到该配置项做出相应的修改就可以实现guest的远程登陆(注意,配置文件中的配置项都被用%%符号注释掉了,若想使其生效,必须将%%删掉,并且配置项最后的一个逗号也要删掉!)

    这是第二种方法,第三种方法与之类似:

    配置文件中的loopback_users配置项有两个,上面是其一,对另一个的修改更加简单,直接将注释%%和逗号去掉即可,两种方法本质是一样的。每个配置项上方都有相应的配置说明,这里就不详说了。

    修改了配置项之后,必须重启rabbitmq才能使之生效:

附上rabbitmq 常用命令

sudo chkconfig rabbitmq-server on #添加开机启动(chkconfig一般只有redhat系统有)RabbitMQ服务

sudo service rabbitmq-server start # 启动服务

sudo service rabbitmq-server status # 查看服务状态

sudo service rabbitmq-server stop # 停止服务

sudo rabbitmqctl stop # 停止服务

sudo rabbitmqctl status # 查看服务状态

sudo rabbitmqctl list_users # 查看当前所有用户

sudo rabbitmqctl list_user_permissions guest # 查看默认guest用户的权限

sudo rabbitmqctl delete_user guest# 删掉默认用户(由于RabbitMQ默认的账号用户名和密码都是guest。为了安全起见, 可以删掉默认用户)

sudo rabbitmqctl add_user username password # 添加新用户

sudo rabbitmqctl set_user_tags username administrator# 设置用户tag

sudo rabbitmqctl set_permissions -p / username “." ".” "." # 赋予用户默认vhost的全部操作权限

sudo rabbitmqctl set_permissions -p / ‘.’ ‘.’ ‘.’ all 设置权限

sudo rabbitmqctl list_user_permissions username # 查看用户的权限

附上许多参考链接:

​​https://www.jianshu.com/p/2d4b81c8b403​​​​​​​

​​​​http://dulishu.top/rabbitmq-guest-login-failed/​​