Tag: mosquitto

Setting up an MQTT server on Debian Stretch

Setting up an MQTT server on Debian Stretch


Today we`re going to be setting up our own home automation server in a dedicated linux server. It’s gonna host our platform using the the Node.Js environment. The different devices around the house are going to be speaking MQTT to this server. I’ve chosen MQTT because of it’s robustness and lightweightness allowing for easy deployment on my favourite controllers: ESP8266. The esp-01, still the cheapest to this date,only sports 4mb of flash, has no trouble at all controlling a couple of relay’s for now. In the future the newer/bigger boards can be used to get more IO and function, but for now I’m going to be using the esp-01’s extensively around the house flexibly (it’s wireless…).


The idea is to have the server online 24/7, so consider a computer thats not to power-hungry; like a Raspberry Pi or a small embedded box computer. Alternatively you can boot up a guest in virtual box like I’m doing for developing purposes. I’m still a fan of debian, which can be run on outdated hardware; it`s memory footprint is small, and fast to boot up.


After installing the base system, (I’ve used the netboot distro , and selected the XFCE environment) we can install the required packages.


The setup guide can be found here: nodejs setup

At the time of writing, my machine is running Jessie Debian 8.7. I also wanted to go with the latest nodejs v7 so the commands become:

sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential

Check if everything is fine and installed:

node -v
npm -v

Update the npm to the latest version:

sudo npm install npm -g

Mosquitto + Websockets

Is what will be using as an MQTT broker the esp’s can publish an subscribe to. As we’ll be using the websockets for our frontend app later on, we’ll have to build mosquitto with libwebsockets.

sudo apt-get install build-essential python quilt devscripts python-setuptools python3 libssl-dev cmake libc-ares-dev uuid-dev daemon

Next will be downloading and building libwebsockets: (link is updated)

Also as of stretch, zlib needs to be installed prior to install libwebsockets (in bold)

wget https://libwebsockets.org/git/libwebsockets/snapshot/libwebsockets-1.4-chrome43-firefox-36.tar.gz
tar zxvf libwebsockets-1.4-chrome43-firefox-36.tar.gz
cd libwebsockets-1.4-chrome43-firefox-36
mkdir build
cd build
<strong>sudo apt-get install zlibc zlib1g zlib1g-dev</strong>
cmake ..
sudo make install
sudo ldconfig

Download, build and install mosquitto (the broker server):

wget http://mosquitto.org/files/source/mosquitto-1.4.2.tar.gz
tar zxvf mosquitto-1.4.2.tar.gz
cd mosquitto-1.4.2

Change the config.mk file with your favorite editor (mousepad in xfce debian) to enable the build with websockets:

sudo mousepad config.mk

Adapt the following code:


Then build:

sudo make install
sudo cp mosquitto.conf /etc/mosquitto
sudo adduser mosquitto

Edit the mosquitto.conf file we just copied to enable the webscoket on port 9001.

sudo mousepad /etc/mosquitto/mosquitto.conf

Add this to the config file:

port 1883
listener 9001
protocol websockets
pid_file /var/run/mosquitto.pid
Optional: I'm going to add security by forcing credentials
You can do so by creating as password file:
mosquitto_passwd -c /etc/mosquitto/passwd yourloginname

Also, back in


set the

allow_anonymous false

Save and Reboot.
After logging launch mosquitto with the config file just modified:

mosquitto -c /etc/mosquitto/mosquitto.conf

This gave a command not found error. The executable was located in the /usr/local/sbin folder.

To solve that were gonna make a link like so:

sudo ln -s /usr/local/sbin/mosquitto /bin/mosquitto

and then:

mosquitto -c /etc/mosquitto/mosquitto.conf

The broker should be listening for websockets on port 9001. The MQTT port remains the default of 1883.

After testing that, we’re gonna want to start it automatically. Using the systemd (Jessie) we can accomplish that easily:

sudo nano /etc/systemd/system/mosquitto.service

Paste in the following:

Description=Mosquitto MQTT Broker daemon

ExecStartPre=/usr/bin/rm -f /var/run/mosquitto.pid
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d
ExecReload=/bin/kill -HUP $MAINPID


You can auto start the service by:

sudo systemctl enable mosquitto
sudo systemctl start mosquitto

That’s it! Done.