MQTT--linux安裝部署(CentOS)

OS環境:CentOS6.5python

一、安裝依賴c++

yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake

二、爲mosquitto增長websocket支持,安裝文本socket庫文件 ,解壓縮

web

# tar zxfv libwebsockets-v1.6-stable.tar.gz

  

三、運行 websocketwebsocket

# cd libwebsockets-v1.6-stable

# mkdir bulid

# cd bulid

# cmake ..

# make && make install

  

四、安裝mosquittosocket

下載mqtt--- mosquitto-1.4.8.tar.gz, 解壓縮

測試

# tar zxfv mosquitto-1.4.8.tar.gz

  

五、修改config.mk文件以使後面編譯的mosquitto文件支持websocketui

# cd /mosquitto-1.4.8

  


mosquitto-1.4.8目錄下的config.mk文件,把config.mk 文件中的 WITH_WEBSOCKETS:=no 改成yes
spa

保存後,執行code

# make && make install

# ln -s /usr/local/lib/libwebsockets.so.6 /usr/lib64/libwebsockets.so.6

# groupadd mosquitto

# useradd -g mosquitto mosquitto

 

六、建立mosquitto.conf、pwfile文件blog

# cd /etc/mosquitto/

 

若是該目錄下沒有mosquitto.conf 和 pwfile 
執行

# cp mosquitto.conf.example mosquitto.conf

# cp pwfile.example pwfile

  

 

而後修改 文件mosquitto.conf ,

511 allow_anonoymous true
.
.
.
537 password_file /etc/mosquitto/pwfile

  

 

並在文件最後加入

port 1883
listener 8080
protocol websockets

  

 

七、啓動mqtt

# mosquitto -c /etc/mosquitto/mosquitto.conf

  

 

八、測試 
另外打開兩個終端 
終端一: 
 這裏其實就是訂閱一個主題爲」mqtt」的消息。」mqtt」爲主題名,假若有客戶端發佈了主題爲」mqtt」的消息,這個終端將會收到消息的內容。

mosquitto_sub -t mqtt

  

 

終端二: 
 這裏就是發佈一個主題爲」mqtt」,內容爲」hello mqtt」的消息。

mosquitto_pub -h localhost -t mqtt -m "hello mqtt"

  

 

這時終端一會收到:hello mqtt

九、錯誤解決

在安裝過程當中,或測試過程當中可能會遇到錯誤:

mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

解決方法:

# cat /etc/ld.so.conf

# echo "/usr/local/lib">>/etc/ld.so.conf

# ldconfig
相關文章
相關標籤/搜索