如何快速在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人我将继续深入下去。

  • 如果你有任何疑问需要探讨,欢迎在文章末尾留言,我尽量在第一时间个大家回复。


分享到:


相關文章: