Linux下安装Mosquitto以及开启Websockets

环境:Linux。

需求:安装Mosquitto服务,以及开启Mosquitto的Websockets服务。

安装包:Mosquitto1.6.9、libwebsockets

注意事项:1.先安装libwebsockets服务,且确保服务能够正常启动使用。

                  2.确保libwebsockets服务正常,再进行mosquitto服务的配置安装。


1. 安装libwebsockets

1.1 安装libwebsockets的依赖

依赖的安装,注意缺一不可,有yum仓库就用yum装,或者apt等仓库。

yum -y install openssl openssl-devel cmake 

1.2 安装libwebsockets

这里你可以选择直接git clone到本地解压,地址为:https://github.com/warmcat/libwebsockets

为方便下载,附上其他用户上传的libwebsockets的网盘地址:

libwebsockets.tar.gz - 蓝奏云

1.2.1 解压安装

 sudo tar -zxvf libwebsockets.tar.gz -C /usr/local/libwebsockets
 cd /usr/local/libwebsockets
 mkdir build
 cd build/
 cmake ..
 make && make install

如无报错情况下,build/bin目录下,将生成 example类文件。

1.2.2.二次编译

进入example的源码目录:libwebsockets/minimal-examples-lowlevel/ws-server/minimal-ws-server

进行编译,并启动服务进行测试。

 cd /usr/local/libwebsockets/minimal-examples-lowlevel/ws-server/minimal-ws-server
 cmake .
 make
 ./lws-minimal-ws-server

此时便可以通过,本机ip加7681端口,访问网页,查看服务启动状态。

即,若本机IP地址为 192.168.1.1,则可访问 192.168.1.1:7681 或 127.0.0.1:7681(本机访问)。

若安装成功:网页应正常显示出libwebsockets页面。

显示libwebsockets标识页面即代表编译安装成功,否则libwebsockkets将不可用。

否则,

需要检查,上述步骤是否在编译过程中报错,重新执行。


2. 安装Mosquitto

2.1 安装依赖&解压

依赖的安装,注意缺一不可,有yum仓库就用yum装,或者apt等仓库。

Mosquitto版本这里我选择1.6.9,新版本2.0后的配置可能在配置时有差别导致无法开启websockets服务。

附上传的 Mosquitto-1.6.9 的网盘地址:

mosquitto-1.6.9.tar.gz - 蓝奏云

 yum -y install  gcc gcc-c++ c-ares-devel uuid-devel libuuid-devel
 ​
 sudo tar -zxvf mosquitto-1.6.9.tar.gz -C /usr/local/

2.2 编辑 config.mk文件

 cd /usr/local/mosquitto-1.6.9
 vi config.mk

修改配置以开启websockets服务,于第68行,WITH_WEBSOCKETS:=no, 修改为yes

保存修改并退出。

  67 # Build with websockets support on the broker.
  68 WITH_WEBSOCKETS:=yes
  69 

2.3 执行安装

注意!当前路径仍为 /usr/local/mosquitto-1.6.9

 adduser mosquitto
 make && make install

2.4 配置 mosquitto.conf文件

此时路径为 /etc/mosquitto/

 cd /etc/mosquitto/
 cp mosquitto.conf.example mosquitto.conf
 vi mosquitto.conf

修改位置为,第201行(Default listener)后的部分,主要是设置默认port端口以及websockets端口

这里于,第211~213行添加了配置,添加了默认端口1883,以及9001端口并指定为websockets,修改完成后,保存修改并退出。

 200 # =================================================================
 201 # Default listener
 202 # =================================================================
 203 
 204 # IP address/hostname to bind the default listener to. If not
 205 # given, the default listener will not be bound to a specific
 206 # address and so will be accessible to all network interfaces.
 207 # bind_address ip-address/host name
 208 #bind_address
 209 
 210 # Port to use for the default listener.
 211 port 1883
 212 listener 9001
 213 protocol websockets
 214 # Bind the listener to a specific interface. This is similar to
 215 # bind_address above but is useful when an interface has multiple addresses or
 216 # the address may change. It is valid to use this with the bind_address option,
 217 # but take care that the interface you are binding to contains the address you
 218 # are binding to, otherwise you will not be able to connect.
 219 # Example: bind_interface eth0
 220 #bind_interface

2.5 运行测试

 mosquitto -v -c /etc/mosquitto/mosquitto.conf

如无特殊情况,mosquitto服务将正常启动,将按照指定路径配置的conf文件,开启1883以及9001端口。


2.6 关于Mosquitto服务启动时遇到的报错处理

在运行mosquitto命令,启动mosquitto服务时,可能会遇到报错,导致无法正常运行。

例1:找不到 libwebsockets.so.xx。

mosquitto: error while loading shared libraries: libwebsockets.so.19: cannot open shared object file: No such file or directory

原因:部分主机安装服务后,软连接没有建立。

解决办法:建立软连接。

 sudo ln -s /usr/local/lib/libwebsocket.so.xx /usr/lib/libwebsocket.so.xx
 ldconfig

例2:无效的用户 'mosquitto'。

 Error: Invalid user 'mosquitto'.

解决办法:新建用户。

add user mosquitto

例3:依赖问题。

error: ‘struct mosquitto’ has no member named ‘ssl’mosq->ssl = (SSL *)in

解决方法:此类皆是依赖没有提前安装好的问题,需提前安装好依赖,如openssl-devel。

例4:websockets 不可用。

Error: Websockets support not available.

原因:服务安装的顺序问题导致。

解决方法:需先正确安装libwebsockets后,在mosquitto的安装过程中,配置config.mk文件,启用包含websockets服务的安装。

以上就是Linux环境下,安装Mosquitto服务以及开启Websockets的全部步骤。


猜你喜欢

转载自blog.csdn.net/weixin_53062121/article/details/128771809#comments_26721179