Ubuntu16.04 Mosquito 安装与支持ssl

731 阅读4分钟

apt-get 安装

# 

# 安装 mosquitto
apt-get install mosquitto

# 安装库
apt-get install libmosquitto-dev

# 安装mqtt客户端
apt-get install mosquitto-clients

# 查看mqtt服务状态
service mosquitto status

源码包下载编译安装

# 源码包下载编译安装

# 下载安装包 1.4.9
# https://mosquitto.org/files/source/ 选择指定版本
wget http://mosquitto.org/files/source/mosquitto-1.4.9.tar.gz

# 解压安装
tar -zxvf mosquitto-1.4.9.tar.gz 

# 切换目录
cd mosquitto-1.4.9

# 开始编译
make

# 安装到系统
make install

# 安装PHP扩展
/usr/local/php/bin/pecl install Mosquitto-alpha

# 切换到安装目录
cd ./workspace/mosquitto-1.4.9/src

# 启动命令
./mosquitto -c ../mosquitto.conf

查看安装是否成功

安装问题

# 缺少依赖
read_handle_server.c:31:25: fatal error: uuid/uuid.h: No such file or directory
compilation terminated.
Makefile:54: recipe for target 'read_handle_server.o' failed
make[1]: *** [read_handle_server.o] Error 1
make[1]: Leaving directory '/home/weixin/workspace/mosquitto-1.4.9/src'
Makefile:21: recipe for target 'mosquitto' failed
make: *** [mosquitto] Error 2

# 解决方法 缺少依赖
apt-get install uuid-dev

SSL 证书秘钥生成

# 生成证书和秘钥目录 /data/ca
mkdir /data/ca

# 更改权限
chmod -R 755 /data/ca

# 切换目录
cd /data/ca

# 服务器端和客户端准备公钥、私钥
# 生成服务端秘钥
openssl genrsa -out server.key 1024

# 生成服务端公钥
openssl rsa -in server.key -pubout -out server.pem

# 生成客户端私钥
openssl genrsa -out client.key 1024

# 生成客户端公钥
openssl rsa -in client.key -pubout -out client.pem


# 生成 CA 证书
# 生成ca私钥
openssl genrsa -out ca.key 1024

# 生成请求文件
openssl req -new -key ca.key -out ca.csr
# Common Name (e.g. server FQDN or YOUR name) []: 这一项,设置访问的的域名

# 生成ca证书
openssl x509 -req -in ca.csr -signkey ca.key -out ca.crt

# 生成服务器端证书和客户端证书
# 服务器端需要向 CA 机构申请签名证书,在申请签名证书之前依然是创建自己的 CSR 文件
openssl req -new -key server.key -out server.csr

# 向自己的 CA 机构申请证书,签名过程需要 CA 的证书和私钥参与,最终颁发一个带有 CA 签名的证书
openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in server.csr -out server.crt

# 生成client端csr
openssl req -new -key client.key -out client.csr

# client 端得到 CA 签名生成client端证书
openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in client.csr -out client.crt

# 最后我们CA 文件夹如下图内容:

配置文件

修改Mosquito配置文件

# 配置文件地址
/etc/mosquitto/mosquitto.conf
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

# mqtt 端口和配置 默认端口:1883
# 端口号
port 7299
# 选择监听的协议 mqtt
protocol mqtt

# ws 端口和配置
# 端口号
listener 7301
# 选择监听的协议 websockets
protocol websockets

# wss 端口和配置
# 端口号
listener 7302
#  选择监听的协议 websockets
protocol websockets
# cafile定义包含CA证书的文件的路径。
cafile /data/php/ca/ca.crt
# PEM编码的服务器证书的路径。
certfile /data/php/ca/server.crt
# PEM编码的密钥文件的路径。
keyfile /data/php/ca/server.key

# 将进程ID写入文件。默认值为空字符串,这意味着不应写入pid文件
pid_file /var/run/mosquitto.pid

# 将持久消息数据保存到磁盘
persistence true
# 永久数据库的位置。必须包含尾随/
persistence_location /var/lib/mosquitto/

# 请注意,如果代理作为Windows服务运行,它将默认为“ log_dest none”,并且stdout和stderr日志记录都不可用
# 可选值有: stdout stderr syslog topic file
# 文件案例(两个参数): log_dest file /var/log/mosquitto.log
# 如果要禁用日志记录,请使用 "log_dest none"
log_dest file /var/log/mosquitto/mosquitto.log

# 可以使用include_dir选项包含外部配置文件。这定义了一个目录,将在其中搜索配置文件。
# 所有以".conf"结尾的文件都将作为配置文件加载。
# 最好将此作为主文件中的最后一个选项。
# 仅从主配置文件处理此选项。
# 指定的目录不得包含主配置文件。
# include_dir中的文件将按区分大小写的字母顺序加载,首字母大写。
# 如果多次指定此选项,则第一个实例中的所有文件将在下一个实例之前进行处理。
# 有关示例,请参见手册页。
include_dir /etc/mosquitto/conf.d

小程序访问

记录

  • 开发版本直连可以
  • 体验版直连不可以 直接访问7302端口
  • 小程序访问接口采用https 访问mqtt必须使用wss方式,否则报错(具体错误不记得,大概意思就是必须使用wss)
  • 访问地址:www.xxx.com/mqtt

通过nginx代理解决

# nginx 配置mqtt代理
location /mqtt {
	proxy_pass https://www.xxx.com:7302;
    proxy_redirect off;
    proxy_set_header Host www.xxx.com:7302;

    proxy_set_header Sec-WebSocket-Protocol mqtt;
    # more_clear_headers Sec-WebSocket-Protocol;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    #proxy_set_header X-real-ip $remote_addr;
    #proxy_set_header X-Forwarded-For $remote_addr;
}