Installing MQTT Broker

In this post, we will install the mosquitto MQTT broker. There are yet a lot of posts about installing it. This post is mostly for easy getting the right commando’s.

Installing mosquitto:

$ sudo apt-get install mosquitto
$ sudo apt-get install mosquitto-clients

In the folder /etc/mosquitto/conf.d, make a new file (default.conf) with the following text:

allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883

Run the following command (replace <username> with your username). This will ask for a password:

$ sudo mosquitto_passwd -c /etc/mosquitto/pwfile <username>
$ sudo reboot

To test if the broker is running correctly, run the following commands, each in a window:

$ mosquitto_sub -d -u <username> -P <password> -t <topic>
$ mosquitto_pub -d -u <username> -P <password> -t <topic> -m "<message>"

If everything is all right, you should see the messages appear.

Optional: make mqtt broker available via a ssl connection:

First make sure you made ssl certificates, as described in another post on this blog. When the certificates are available, you can proceed to the rest of this post.

In the folder /etc/mosquitto/conf.d, append to the default config the following text, replacing “example.org” with your domain name:

allow_anonymous false
password_file /etc/mosquitto/pwfile

listener 1883 localhost

listener 8883
certfile /etc/letsencrypt/live/mqtt.example.org/cert.pem
cafile /etc/letsencrypt/live/mqtt.example.org/chain.pem
keyfile /etc/letsencrypt/live/mqtt.example.org/privkey.pem

listener 8083
protocol websockets
certfile /etc/letsencrypt/live/mqtt.example.org/cert.pem
cafile /etc/letsencrypt/live/mqtt.example.org/chain.pem
keyfile /etc/letsencrypt/live/mqtt.example.org/privkey.pem

Special thanks to Simon De Smet. Without his search in commando’s, my broker would still have no password.

Leave a Reply

Your email address will not be published. Required fields are marked *