phpMQtt和paho.mqtt.js通过ssl协议链接moquitto

1、安装mosquitto软件(在网上都是安装教程)

参考地址:https://www.jianshu.com/p/632219915527

2、找到mosquitto.conf配置文件

可以通过ps -ef| grep mosquitto 查找到配置文件位置,一般在/etc/mosquitto/mosquitto.conf位置

3、通过编辑器打开mosquitto.conf文件,在空白位置添加如下内容,然后重启,mosquitto -c /ect/mosquitto/mosquitto.conf -d

port 39183  //ws协议端口
listener 39184  //wss协议端口,也就是需要证书支持
cafile /xxx/xxx/xxx_ssl/xxx-xxx.cer //根证书
certfile /xxx/xxx/xxx_ssl/xxx.xxx.xx.pem  //域名公钥
keyfile /xx/xxx/xx_ssl/xx.xxx.xx.key  //域名私钥
listener 39080 //websocket端口 
protocol websockets //开启websocket

listener 39081 //ssl协议
protocol websockets
cafile /xxx/xxx/xxx_ssl/xxx-xxx.cer //根证书
certfile /xxx/xxx/xxx_ssl/xxx.xxx.xx.pem //域名公钥
keyfile /xx/xxx/xx_ssl/xx.xxx.xx.key //域名私钥

注意:1、如何没有开启ssl,一般都是通过ip访问,但是部署证书后,都是通过域名来访问的;

       2、监听的端口号要在服务上放行

4、下载paho-mqtt.js(https://github.com/eclipse/paho.mqtt.javascript)、然后引入编写业务流程,可以参考一下:

 

 

 

 

5、phpMQTT.php(https://github.com/bluerhinos/phpMQTT)文件

 

 

 这边都是腾讯云或阿里云申请的免费证书,注意需要三个证书:根证书,公钥、私钥;

部署的域名对应的是mosquitto服务器的ip,如果您的moqsquitto服务器不是和项目放在一台服务中;

例如:mosquitto服务器的ip是192.168.1.55,然后项目的ip是192.168.2.100对应的域名是www.aa.com,那么mosquitto需要部署一个域名了,例如:www.bb.com,然后bb域名申请上面的公钥,私钥证书,根证书分别下载下来上传到mosquitto的服务器中,位置就对应上面mosquitto.conf中的配置目录中

我是使用阿里云的证书:

根证书下载:https://help.aliyun.com/document_detail/179033.html或者

 

 公钥和私钥要到阿里云ssl后台下载

终端订阅:域名就是www.bb.com   后台跟的是根证书

 

 

备注:域名前面不用带协议,源码会自己拼接;如果url部署了证书,那么就要用域名(域名指向的是mosquitto服务器的ip,如果项目和mosquitto是同一台服务器,那么域名使用项目域名就好了)来访问了

 

posted @ 2021-09-14 23:06  黑夜中晚霞  阅读(777)  评论(0编辑  收藏  举报