mosquitto-1.4.2安装配置教程

来源:互联网 发布:aa录音软件下载 编辑:程序博客网 时间:2024/05/17 01:57


  1. 先决条件
  • 读者熟悉Ubuntu  Linux各种基本操作。
  • 读者熟悉apt-getyum软件包管理工具。
  • 读者熟悉编译安装Linux程序的一般过程。
  • 操作用户具有sudo权限。
  • 读者系统中安装git工具。
  1. 准备源代码

从官网下载相应的源码包,网址为http://mosquitto.org/download/,该文档以mosquitto1.4.2版本为例。

    1. 解压源文件

执行如下指令解压源码

tar  xvf  mosquito-1.4.2.tar.gz

    1. 修改配置文件

执行如下指令打开config.mk文件,

cd    mosquito-1.4.2

vim  config.mk

其中包含了所以mosquitto的安装选项,详细的参数说明如下:

是否支持tcpd/libwrap功能.

#WITH_WRAP:=yes

是否开启SSL/TLS支持

#WITH_TLS:=yes

是否开启TLS/PSK支持

#WITH_TLS_PSK:=yes

 

 

# Comment out to disable client client threading support.

#WITH_THREADING:=yes

是否使用严格的协议版本(老版本兼容会有点问题)

#WITH_STRICT_PROTOCOL:=yes

是否开启桥接模式

#WITH_BRIDGE:=yes

是否开启持久化功能

#WITH_PERSISTENCE:=yes

是否监控运行状态

#WITH_MEMORY_TRACKING:=yes

 

这里需要注意的是,默认情况下mosquitto的安装需要OpenSSL的支持;如果不需要SSL,则需要关闭config.mk里面的某些与SSL功能有关的选项(WITH_TLSWITH_TLS_PSK),即将上面的yes改为no即可。

  1. 安装mosquitto

执行如下指令安装mosquitto

make    all

make    install

安装完之后会在系统命令行里发现mosquittomosquitto_passwdmosquitto_pubmosquitto_sub四个工具,分别用于启动代理、管理密码、发布消息和订阅消息。

 

  1. 配置&运行

安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是Mosquitto的配置文件,即mosquitto.conf,以下是详细的配置参数说明。

 

  1. # =================================================================  
  2. # General configuration  
  3. # =================================================================  
  4.   
  5. 客户端心跳的间隔时间  
  6. #retry_interval 20  
  7.   
  8. 系统状态的刷新时间  
  9. #sys_interval 10  
  10.   
  11. 系统资源的回收时间,0表示尽快处理  
  12. #store_clean_interval 10  
  13.   
  14. 服务进程的PID  
  15. #pid_file /var/run/mosquitto.pid  
  16.   
  17. 服务进程的系统用户  
  18. #user mosquitto  
  19.   
  20. 客户端心跳消息的最大并发数  
  21. #max_inflight_messages 10  
  22.   
  23. 客户端心跳消息缓存队列  
  24. #max_queued_messages 100  
  25.   
  26. 用于设置客户端长连接的过期时间,默认永不过期  
  27. #persistent_client_expiration  
  28.   
  29. # =================================================================  
  30. # Default listener  
  31. # =================================================================  
  32.   
  33. 服务绑定的IP地址  
  34. #bind_address  
  35.   
  36. 服务绑定的端口号  
  37. #port 1883  
  38.   
  39. 允许的最大连接数,-1表示没有限制  
  40. #max_connections -1  
  41.   
  42. # cafileCA证书文件  
  43. # capathCA证书目录  
  44. # certfilePEM证书文件  
  45. # keyfilePEM密钥文件  
  46. #cafile  
  47. #capath  
  48. #certfile  
  49. #keyfile  
  50.   
  51. 必须提供证书以保证数据安全性  
  52. #require_certificate false  
  53.   
  54. require_certificate值为trueuse_identity_as_username也必须为true  
  55. #use_identity_as_username false  
  56.   
  57. 启用PSKPre-shared-key)支持  
  58. #psk_hint  

 

 

  1. # SSL/TSL加密算法,可以使用“openssl ciphers”命令获取  
  2. # as the output of that command.  
  3. #ciphers  
  4.   
  5. # =================================================================  
  6. # Persistence  
  7. # =================================================================  
  8.   
  9. 消息自动保存的间隔时间  
  10. #autosave_interval 1800  
  11.   
  12. 消息自动保存功能的开关  
  13. #autosave_on_changes false  
  14.   
  15. 持久化功能的开关  
  16. persistence true  
  17.   
  18. 持久化DB文件  
  19. #persistence_file mosquitto.db  
  20.   
  21. 持久化DB文件目录  
  22. #persistence_location /var/lib/mosquitto/  
  23.   
  24. # =================================================================  
  25. # Logging  
  26. # =================================================================  
  27.   
  28. # 4种日志模式:stdoutstderrsyslogtopic  
  29. # none 则表示不记日志,此配置可以提升些许性能  
  30. log_dest none  
  31.   
  32. 选择日志的级别(可设置多项)  
  33. #log_type error  
  34. #log_type warning  
  35. #log_type notice  
  36. #log_type information  
  37.   
  38. 是否记录客户端连接信息  
  39. #connection_messages true  
  40.   
  41. 是否记录日志时间  
  42. #log_timestamp true  
  43.   
  44. # =================================================================  
  45. # Security  
  46. # =================================================================  
  47.   
  48. 客户端ID的前缀限制,可用于保证安全性  
  49. #clientid_prefixes  
  50.   
  51. 允许匿名用户  
  52. #allow_anonymous true  
  53.   
  54. 用户/密码文件,默认格式:username:password  
  55. #password_file  
  56.   
  57. # PSK格式密码文件,默认格式:identity:key  
  58. #psk_file  
  59. # pattern write sensor/%u/data 

                   

  1. # ACL权限配置,常用语法如下:  
  2. 用户限制:user <</span>username>  
  3. 话题限制:topic [read|write] <</span>topic>  
  4. 正则限制:pattern write sensor/%u/data  
  5. #acl_file  
  6.   
  7. # =================================================================  
  8. # Bridges  
  9. # =================================================================  
  10.   
  11. 允许服务之间使用桥接模式(可用于分布式部署)  
  12. #connection <</span>name>  
  13. #address <</span>host>[:<</span>port>]  
  14. #topic <</span>topic> [[[out | in | both] qos-level] local-prefix remote-prefix]  
  15.   
  16. 设置桥接的客户端ID  
  17. #clientid  
  18.   
  19. 桥接断开时,是否清除远程服务器中的消息  
  20. #cleansession false  
  21.   
  22. 是否发布桥接的状态信息  
  23. #notifications true  
  24.   
  25. 设置桥接模式下,消息将会发布到的话题地址  
  26. # $SYS/broker/connection/<</span>clientid>/state  
  27. #notification_topic  
  28.   
  29. 设置桥接的keepalive数值  
  30. #keepalive_interval 60  
  31.   
  32. 桥接模式,目前有三种:automaticlazyonce  
  33. #start_type automatic  
  34.   
  35. 桥接模式automatic的超时时间  
  36. #restart_timeout 30  
  37.   
  38. 桥接模式lazy的超时时间  
  39. #idle_timeout 60  
  40.   
  41. 桥接客户端的用户名  
  42. #username  
  43.   
  44. 桥接客户端的密码  
  45. #password  
  46.   
  47. # bridge_cafile:桥接客户端的CA证书文件  
  48. # bridge_capath:桥接客户端的CA证书目录  
  49. # bridge_certfile:桥接客户端的PEM证书文件  
  50. # bridge_keyfile:桥接客户端的PEM密钥文件  
  51. #bridge_cafile  
  52. #bridge_capath  
  53. #bridge_certfile  
  54. #bridge_keyfile  
  55.   
  56. 自己的配置可以放到以下目录中  
  57. include_dir /etc/mosquitto/conf.d 

 

                                               

最后,启动mosquitto服务很简单,直接运行如下命令即可,

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

  1. 先决条件
  • 读者熟悉Ubuntu  Linux各种基本操作。
  • 读者熟悉apt-getyum软件包管理工具。
  • 读者熟悉编译安装Linux程序的一般过程。
  • 操作用户具有sudo权限。
  • 读者系统中安装git工具。
  1. 准备源代码

从官网下载相应的源码包,网址为http://mosquitto.org/download/,该文档以mosquitto1.4.2版本为例。

    1. 解压源文件

执行如下指令解压源码

tar  xvf  mosquito-1.4.2.tar.gz

    1. 修改配置文件

执行如下指令打开config.mk文件,

cd    mosquito-1.4.2

vim  config.mk

其中包含了所以mosquitto的安装选项,详细的参数说明如下:

是否支持tcpd/libwrap功能.

#WITH_WRAP:=yes

是否开启SSL/TLS支持

#WITH_TLS:=yes

是否开启TLS/PSK支持

#WITH_TLS_PSK:=yes

 

 

# Comment out to disable client client threading support.

#WITH_THREADING:=yes

是否使用严格的协议版本(老版本兼容会有点问题)

#WITH_STRICT_PROTOCOL:=yes

是否开启桥接模式

#WITH_BRIDGE:=yes

是否开启持久化功能

#WITH_PERSISTENCE:=yes

是否监控运行状态

#WITH_MEMORY_TRACKING:=yes

 

这里需要注意的是,默认情况下mosquitto的安装需要OpenSSL的支持;如果不需要SSL,则需要关闭config.mk里面的某些与SSL功能有关的选项(WITH_TLSWITH_TLS_PSK),即将上面的yes改为no即可。

  1. 安装mosquitto

执行如下指令安装mosquitto

make    all

make    install

安装完之后会在系统命令行里发现mosquittomosquitto_passwdmosquitto_pubmosquitto_sub四个工具,分别用于启动代理、管理密码、发布消息和订阅消息。

 

  1. 配置&运行

安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是Mosquitto的配置文件,即mosquitto.conf,以下是详细的配置参数说明。

 

  1. # =================================================================  
  2. # General configuration  
  3. # =================================================================  
  4.   
  5. 客户端心跳的间隔时间  
  6. #retry_interval 20  
  7.   
  8. 系统状态的刷新时间  
  9. #sys_interval 10  
  10.   
  11. 系统资源的回收时间,0表示尽快处理  
  12. #store_clean_interval 10  
  13.   
  14. 服务进程的PID  
  15. #pid_file /var/run/mosquitto.pid  
  16.   
  17. 服务进程的系统用户  
  18. #user mosquitto  
  19.   
  20. 客户端心跳消息的最大并发数  
  21. #max_inflight_messages 10  
  22.   
  23. 客户端心跳消息缓存队列  
  24. #max_queued_messages 100  
  25.   
  26. 用于设置客户端长连接的过期时间,默认永不过期  
  27. #persistent_client_expiration  
  28.   
  29. # =================================================================  
  30. # Default listener  
  31. # =================================================================  
  32.   
  33. 服务绑定的IP地址  
  34. #bind_address  
  35.   
  36. 服务绑定的端口号  
  37. #port 1883  
  38.   
  39. 允许的最大连接数,-1表示没有限制  
  40. #max_connections -1  
  41.   
  42. # cafileCA证书文件  
  43. # capathCA证书目录  
  44. # certfilePEM证书文件  
  45. # keyfilePEM密钥文件  
  46. #cafile  
  47. #capath  
  48. #certfile  
  49. #keyfile  
  50.   
  51. 必须提供证书以保证数据安全性  
  52. #require_certificate false  
  53.   
  54. require_certificate值为trueuse_identity_as_username也必须为true  
  55. #use_identity_as_username false  
  56.   
  57. 启用PSKPre-shared-key)支持  
  58. #psk_hint  

 

 

  1. # SSL/TSL加密算法,可以使用“openssl ciphers”命令获取  
  2. # as the output of that command.  
  3. #ciphers  
  4.   
  5. # =================================================================  
  6. # Persistence  
  7. # =================================================================  
  8.   
  9. 消息自动保存的间隔时间  
  10. #autosave_interval 1800  
  11.   
  12. 消息自动保存功能的开关  
  13. #autosave_on_changes false  
  14.   
  15. 持久化功能的开关  
  16. persistence true  
  17.   
  18. 持久化DB文件  
  19. #persistence_file mosquitto.db  
  20.   
  21. 持久化DB文件目录  
  22. #persistence_location /var/lib/mosquitto/  
  23.   
  24. # =================================================================  
  25. # Logging  
  26. # =================================================================  
  27.   
  28. # 4种日志模式:stdoutstderrsyslogtopic  
  29. # none 则表示不记日志,此配置可以提升些许性能  
  30. log_dest none  
  31.   
  32. 选择日志的级别(可设置多项)  
  33. #log_type error  
  34. #log_type warning  
  35. #log_type notice  
  36. #log_type information  
  37.   
  38. 是否记录客户端连接信息  
  39. #connection_messages true  
  40.   
  41. 是否记录日志时间  
  42. #log_timestamp true  
  43.   
  44. # =================================================================  
  45. # Security  
  46. # =================================================================  
  47.   
  48. 客户端ID的前缀限制,可用于保证安全性  
  49. #clientid_prefixes  
  50.   
  51. 允许匿名用户  
  52. #allow_anonymous true  
  53.   
  54. 用户/密码文件,默认格式:username:password  
  55. #password_file  
  56.   
  57. # PSK格式密码文件,默认格式:identity:key  
  58. #psk_file  
  59. # pattern write sensor/%u/data 

                   

  1. # ACL权限配置,常用语法如下:  
  2. 用户限制:user <</span>username>  
  3. 话题限制:topic [read|write] <</span>topic>  
  4. 正则限制:pattern write sensor/%u/data  
  5. #acl_file  
  6.   
  7. # =================================================================  
  8. # Bridges  
  9. # =================================================================  
  10.   
  11. 允许服务之间使用桥接模式(可用于分布式部署)  
  12. #connection <</span>name>  
  13. #address <</span>host>[:<</span>port>]  
  14. #topic <</span>topic> [[[out | in | both] qos-level] local-prefix remote-prefix]  
  15.   
  16. 设置桥接的客户端ID  
  17. #clientid  
  18.   
  19. 桥接断开时,是否清除远程服务器中的消息  
  20. #cleansession false  
  21.   
  22. 是否发布桥接的状态信息  
  23. #notifications true  
  24.   
  25. 设置桥接模式下,消息将会发布到的话题地址  
  26. # $SYS/broker/connection/<</span>clientid>/state  
  27. #notification_topic  
  28.   
  29. 设置桥接的keepalive数值  
  30. #keepalive_interval 60  
  31.   
  32. 桥接模式,目前有三种:automaticlazyonce  
  33. #start_type automatic  
  34.   
  35. 桥接模式automatic的超时时间  
  36. #restart_timeout 30  
  37.   
  38. 桥接模式lazy的超时时间  
  39. #idle_timeout 60  
  40.   
  41. 桥接客户端的用户名  
  42. #username  
  43.   
  44. 桥接客户端的密码  
  45. #password  
  46.   
  47. # bridge_cafile:桥接客户端的CA证书文件  
  48. # bridge_capath:桥接客户端的CA证书目录  
  49. # bridge_certfile:桥接客户端的PEM证书文件  
  50. # bridge_keyfile:桥接客户端的PEM密钥文件  
  51. #bridge_cafile  
  52. #bridge_capath  
  53. #bridge_certfile  
  54. #bridge_keyfile  
  55.   
  56. 自己的配置可以放到以下目录中  
  57. include_dir /etc/mosquitto/conf.d 

 

                                               

最后,启动mosquitto服务很简单,直接运行如下命令即可,

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

0 0
原创粉丝点击