如何快速在Ubuntu Linux服務器上安裝一臺MQTT服務器?

說起物聯網,目前就筆者看來最有前途的應該是MQTT協議,MQTT(MQ Telemetry Transport,消息隊列遙測傳輸)是近年來出現的一種新型協議,物聯網領域會將其作為標準協議。MQTT 原本是IBM 公司開發的協議,現在則開源了,被人們不斷開發著。MQTT 是一種能實現一對多通信(人們稱之為發佈或訂閱型)的協議。它由3 種功能構成,分別是中介(broker)、發佈者(publisher)和訂閱者(subscriber),是一款輕量級的,節省通信流量的機器和機器間的通訊協議。

如何快速在Ubuntu Linux服務器上安裝一臺MQTT服務器?

在筆者的另外一篇文章 提到了MQTT協議,裡面指出目前有很多的廠商或者開源產品支持MQTT協議的實現,其中就有目前最為流行的Mosquitto服務器。那麼Mosquitto服務太牛掰了,幾乎支持市面上你能想到,也想不到的操作系統平臺,君請看。

  • Windows

  • Mac

  • Arch Linux

  • CentOS

  • Debian

  • Fedora

  • FreeBSD

  • Gentoo

  • Mandriva

  • openSUSE

  • OpenWrt

  • Raspberry Pi

  • Redhat Enterprise Linux

  • Slackware

  • SUSE Linux Enterprise Server

  • Ubuntu

  • Other Linux

  • QNX

  • iPhone

如何快速在Ubuntu Linux服務器上安裝一臺MQTT服務器?


下面以在Ubuntu的Linux操作系統安裝Mosquitto服務器為例子,和大家分享一下快速安裝的方法。

Ubuntu的操作系統,並在擁有一個跟用戶(Root)的權限的賬號,下面是具體的步驟。

如何快速在Ubuntu Linux服務器上安裝一臺MQTT服務器?

如何快速在Ubuntu Linux服務器上安裝一臺MQTT服務器?

1.配置mosquitto的安裝倉庫地址

打開Ubuntu的終端,並運行下面的命令

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

當你輸入密碼之後,你將會看到下面的類似輸出(臨時文件的名字可能不太一樣)

gpg: icedust `/tmp/tmpj3a50p1p/secring.gpg' created

gpg: icedust `/tmp/tmpj3a50p1p/pubring.gpg' created

gpg: requesting key 262C4500 from hkp server keyserver.ubuntu.com

gpg: /tmp/tmpj3a50p1p/trustdb.gpg: trustdb created

gpg: key 262C4500: public key "Launchpad mosquitto" imported

gpg: Total number processed: 1

gpg: imported: 1 (RSA: 1)

OK

2. 更新Ubuntu的最新操作系統補丁

緊接著運行下面的命令去更新Ubuntu的最新安裝包,其會把Mosquitto的倉庫地址加入到操作系統中來。

sudo apt-get update

你將會看到下面的類似輸出。

Get:1 http://ppa.launchpad.net/mosquitto-dev/mosquitto-ppa/

ubuntu xenial InRelease [23,8 kB]

Hit:2 http://security.ubuntu.com/ubuntu xenial-security InRelease

Hit:3 http://ar.archive.ubuntu.com/ubuntu xenial InRelease

Hit:4 http://ar.archive.ubuntu.com/ubuntu xenial-updates InRelease

Get:5 http://ppa.launchpad.net/mosquitto-dev/ mosquittoppa/

ubuntu xenial/main amd64 Packages [2.048 B]

Hit:6 http://ar.archive.ubuntu.com/ubuntu xenial-backports InRelease

Get:7 http://ppa.launchpad.net/mosquitto-dev/ mosquittoppa/

ubuntu xenial/main i386 Packages [2.048 B]

Get:8 http://ppa.launchpad.net/mosquitto-dev/mosquitto-ppa/

ubuntu xenial/main Translation-en [1.292 B]

Fetched 29,2 kB in 1s (21,5 kB/s)

Reading package lists... Done

3.安裝MQTT服務端

現在時候運行下面的命令去安裝Mosquitto服務了。

sudo apt-get install mosquito

你將會看到下面的類似輸出.

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following additional packages will be installed:

libev4 libuv1 libwebsockets7

The following NEW packages will be installed:

libev4 libuv1 libwebsockets7 mosquitto

0 upgraded, 4 newly installed, 0 to remove and 299 not upgraded.

8 not fully installed or removed.

Need to get 274 kB of archives.

After this operation, 714 kB of additional disk space will be used.

Do you want to continue? [Y/n]

這個時候請輸入Y,讓安裝繼續。之後,將會出現下面類似的界面,這個時候已經在安裝和配置Mosquitto了。

Setting up mosquitto (1.4.10-0mosquitto3) ...

Processing triggers for libc-bin (2.23-0ubuntu3) ...

Processing triggers for systemd (229-4ubuntu7) ...

Processing triggers for ureadahead (0.100.0-19) ...

4.安裝MQTT客戶端

通過上面的步驟,Mosquitto其實已經被安裝完了,如果想繼續安裝Mosquitto的客戶端的話,可以運行下面的命令:

sudo apt-get install mosquitto-clients

其輸出可能類似於下面:

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following additional packages will be installed:

libc-ares2 libmosquitto1

The following NEW packages will be installed:

libc-ares2 libmosquitto1 mosquitto-clients

0 upgraded, 3 newly installed, 0 to remove and 299 not upgraded.

Need to get 138 kB of archives.

After this operation, 332 kB of additional disk space will be used.

Do you want to continue? [Y/n]

當要求你繼續的時候,請輸入Y。

輸入Y之後,將會顯示下面類似的信息,其表示正在安裝Mosquitto的客戶端。

Setting up libmosquitto1:amd64 (1.4.10-0mosquitto3) ...

Setting up mosquitto-clients (1.4.10-0mosquitto3) ...

Processing triggers for libc-bin (2.23-0ubuntu3) ...

5.運行下面的命令,確認Mosquitto的服務是否已經啟動。

sudo service mosquitto status

如果出現了Active: active (running)等關鍵字,則表示Mosquitto已經啟動。

6.確認MQTT服務是否啟動

因為MQTT服務器的默認端口是1883,運行下面的Linux命令,也能確認Mosquitto是否已經啟動。

netstat -an | grep 1883

下面的輸出顯示了Mosquitto已經成功啟動。

tcp 0 0 0.0.0.0:1883 0.0.0.0:* LISTEN

tcp6 0 0 :::1883 :::* LISTEN

  • 如果你想繼續深入的話,請給我在文末留言,如果大於10人我將繼續深入下去。

  • 如果你有任何疑問需要探討,歡迎在文章末尾留言,我儘量在第一時間個大家回覆。


分享到:


相關文章: