版本说明:
Mosquitto版本:v2.0.10
libwebsockets版本:v3.0.1(用于支持websockets)
mosquitto-go-auth(Mosquitto plugin):v1.50(提供权限认证)
1.编译前准备
因为我们准备本机编译源码包,所以要提前安装gcc
yum -y install gcc-c++ cmake
还有一些三方依赖
yum install openssl-devel
yum install libuuid-devel
yum install c-ares-devel
yum install uuid-devel
yum install libwebsockets-devel.x86_64
yum install libwebsockets.x86_64
2.下载Mosquitto源码
从官网下载Mosquitto源码压缩包, 或者从github上下载也是可以的, 我这里使用的版本2.0.10
3.解压并安装Mosquitto
Mosquitto默认是不支持websockets的,要在编译前修改config.mk,WITH_WEBSOCKETS:=yes
,把no改为yes
执行make
,make install
;
如果遇到fatal error: cjson/cJSON.h: No such file or directory
报错,那么要提前安装cJSON(这里cJSON的安装,yum和apt不一定能找到,可以直接从github上下载源码压缩包,然后解压,进入目录,并make
,make install
), 这是当前版本的bug,后期应该会修复吧;
安装完成之后,添加名称为mosquitto的用户,并将相关文件夹授权给mosquitto用户
sudo groupadd mosquitto
sudo useradd -s /sbin/nologin mosquitto -g mosquitto -d /var/lib/mosquitto
sudo mkdir -p /var/log/mosquitto/ /var/lib/mosquitto/
sudo chown -R mosquitto:mosquitto /var/log/mosquitto/
sudo chown -R mosquitto:mosquitto /var/lib/mosquitto/
创建/etc/systemd/system/mosquitto.service文件
[Unit]
Description=Mosquitto MQTT v3.1/v3.1.1 server
Wants=network.target
Documentation=http://mosquitto.org/documentation/
[Service]
Type=simple
User=mosquitto
Group=mosquitto
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Restart=on-failure
SyslogIdentifier=Mosquitto
[Install]
WantedBy=multi-user.target
设置开机启动
sudo systemctl enable mosquitto
4.解压并安装Mosquitto auth插件
我安装的是mosquitto-go-auth插件,因为是go语言写的插件,所以要提前安装golang,建议同时设置go的代理go env -w GOPROXY=https://mirrors.aliyun.com/goproxy