2019-05-29

Installing the MQTT Broker

To setup a MQTT broker using Mosquitto and CentOS 7.

This is assumuning a clean install of CentOS with ssh enabled for a non-root, sudo enabled user. This install is for a local instance, will not cover ssl as we will be behind our router/firewall.

Requires a basic server, we will use Apache.
To install Apache:

sudo yum install httpd

To start Apache:

sudo systemctl start httpd.service

Enable Apache to start at boot:

sudo systemctl enable httpd.service


To open the firewall on the server to allow HTTP traffic:

sudo firewall-cmd --permanent --add-service=http


Then to reload for tha changes to take effect:

sudo firewall-cmd --reload


Test by going to your server in a browser.
For example:

http://192.168.0.40/


Should bring up the server test page.




To install Mosquitto you will need to first intall the epel-release package:

sudo yum -y install epel-release


Install Mosquitto:

sudo yum -y install mosquitto


Start Mosquitto:

sudo systemctl start mosquitto


Enable Mosquitto to start at boot:

sudo systemctl enable mosquitto


Testing


To test the install two terminals will be needed. Log in to the MQTT server with both.
Placing the two terminals side by side works well for this.
In one terminal run (there will not be an output yet):

mosquitto_sub -h localhost -t test


In the above command,_sub is for subscribing, -h specifies the host name of the MQTT server, while -t sets the topic name.
In the second ternimal run ( hello word should appear in the first terminal:

mosquitto_pub -h localhost -t test -m "hello world"


The options for _pub are the same as the options for subscribing.



Configuration


Rename the default Mosquitto configuration file:

sudo mv /etc/mosquitto/mosquitto.conf /etc/mosquitto/ORG_mosquitto.conf


Create a new mosquitto.conf

sudo nano /etc/mosquitto/mosquitto.conf


and paste in the following:

listener 8083
protocol websockets
listener 1883
protocol mqtt


Open the newly added ports in the server firewall:

sudo firewall-cmd --permanent --add-port=8083/tcp
sudo firewall-cmd --permanent --add-port=1883/tcp


Restart Mosquitto and the firewall

sudo systemctl restart mosquitto
sudo firewall-cmd --reload


To see all of the open ports:

sudo firewall-cmd --list-all



A lot of the information here came from a tutorial at Digitalocean. For more information about setting up a secure instance visit How To Install and Secure the Mosquitto MQTT Messaging Broker on CentOS 7
Next, how to use a web client,

Do not use the one mentioned in the above Digitalocean link !