Tuesday, February 4, 2020

Build and run EMQX MQTT broker in raspberry pi

Before start following the steps ...

You should probably know that there is official doc about how to install in variant systems. For example:
https://docs.emqx.io/broker/v3/en/install.html#debian
However I prefer to gain the sense of control over the install locations etc.

Install Erlang/OTP 21 or newer

There are many ways to do it. I'd recommend https://github.com/asdf-vm/asdf

Build emqx from source code.

Follow the instructions in README in emqx-rel project.
The exact commands I executed
$ git clone -b v4.0.0 https://github.com/emqx/emqx-rel.git emqx-rel
cd emqx-rel && make
Execute this command to test if the broker can start normally:
$./_build/emqx/rel/emqx/bin/emqx console
Expected log:
EMQ X Broker 4.0.0 is running now!

Start it in systemd

Move rel to your favourite location.

$ sudo mv _build/emqx/rel/emqx /opt/

Make a systemd unit file for emqx service

There is a system unit file in emqx-rel project. File path: deploy/packages/rpm/emqx.service
To make it simple, update the file to
  • Run systemd service with user pi
  • Replace /usr/bin/ with /opt/emqx/bin.
$ cat /opt/emqx/emqx.service
[Unit]
Description=emqx daemon
After=network.target

[Service]
User=pi
Group=pi
Type=forking
Environment=HOME=/opt/emqx
ExecStart=/bin/sh /opt/emqx/bin/emqx start
LimitNOFILE=1048576
ExecStop=/bin/sh /opt/emqx/bin/emqx stop

[Install]
WantedBy=multi-user.target
$ sudo ln -s /opt/emqx/emqx.service /etc/systemd/system/emqx.service

Start it

$ sudo systemctl start emqx

Check if it's running well

$ systemctl status emqx.service
....
Feb 04 20:37:46 raspberrypi sh[5945]: EMQ X Broker v4.0.0 is started successfully!
Feb 04 20:37:46 raspberrypi systemd[1]: Started emqx daemon.

Make it auto start

$ sudo systemctl enable emqx.service

No comments:

Post a Comment