01-阿里云Mosquitto安装

云主机配置:乞丐版轻量应用服务器。

安装mosquitto:

1. 添加apt源.

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
wget http://repo.mosquitto.org/debian/mosquitto-buster.list
apt-get update

2. 安装

apt-get install mosquitto

3. 配置mosquitto

首先创建密码文件:

mosquitto_passwd -b -c mosquitto-passwd-file 用户名 密码

将生成的mosquitto-passwd-file移动到指定位置,一般放在/etc/mosquitto下为宜。

编辑mosquitto配置文件,加入下列脚本:

allow_anonymous false
per_listener_settings false
password_file /etc/mosquitto/mosquitto-passwd-file

启动mosquitto。

mosquitto -c /etc/mosquitto/mosquitto.conf -d

执行netstat -ntlp 查看是否启动成功,默认端口为1883。

4. 测试:mosquitto命令使用方法请参考官方文档。常用命令为:mosquitto_pub, mosquitto_sub和上面用到的mosquitto_passwd.

如:发布。

mosquitto_pub -t topic -m content -h 主机IP地址 -u 用户名 -P 密码

再如:订阅。

mosquitto_sub -t topic -h 主机IP地址 -u 用户名 -P 密码

 The End.

后面介绍如何与Nginx和Websocketd等进行整合。


版权声明:本文为xiahousj原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。