MQTT Broker in Alibaba Cloud – Secure hosting

MQTT Broker in Alibaba Cloud – Secure hosting

Alibaba is the most used cloud hosting provider in China.  This article provides a step by step procedure to set up the MQTT Broker in Alibaba cloud.  Bevywise MQTT Broker just needed a Ubuntu instance to host the broker.

Setting up the Alibaba Cloud for MQTT Broker:

  1. Select the Elastic Compute Service and create Ubuntu instance with minimum 1 CPU, 2GB RAM, 40 GB Disk with  Intel Xeon processor.
  2. After creating the instance, reset the password for ssh login.
  3. Create a inbound and outbound rule to ssh
    • To create outbound and inbound rule, expand “Network and  Security” and select “Security Group”.
    • In that page select “Add Rule”, it lead to Ingress and Outbound  page.
    • Click “Add Security Button” on the top right corner, it lead  to “Add Security Group Rule“.
    • In that Window select “Protocol type” as SSH and  “Authorization Objects” as 0.0.0.0/0 for access the Port  from any where or give specific IP address from which you  access SSH.
    • Do allow inbound flow of traffic for the following ports as well – 1883, 8080, 8081, 443 & 8883 
  4. Alibaba instance can be easily used via a remote connection from Windows, Ubuntu and MAC.
  5. Remote Connection from Windows:
    1. Download PuTTY fromhttp://www.chiark.greenend.org.uk/~sgtatham/putty/ or from other PuTTY download source. The “putty.exe” download is good for basic SSH.
    2. Save the download to your C:\WINDOWS folder.
    3. If you want to make a link to PuTTY on your desktop:
      1. Open the C:\WINDOWS folder in Windows Explorer.
      2. Right click on the putty.exe file and select Send To > Desktop
    4. Double-click on the putty.exe program or the desktop shortcut to  launch the application.
    5. Enter your connection settings:
      MQTT Broker Upload Putty settings

      Host Name: <Alibaba Public IP address>

      1. Port: 22 (leave as default
      2. Connection Type: SSH (leave as default)
    6. Click Open to start the SSH session.
    7. If this is your first time connecting to the server from this computer, you will see the following output. Accept the connection by clicking Yes.
      Login confirmation
    8. Once the SSH Connection is open, you should see a terminal prompt  asking for your username:
      login as: Connect with your SSH user of choice.Next, enter your  password.
    9. Please note that you will NOT see your cursor moving, or any  characters typed (such as ******), when typing your password. This is     a standard PuTTY security feature. Hit enter.
      Using keyboard-interactive authentication.
      Password:
    10. You are now logged into your server with SSH.
  6. Remote login from Liunx and MAC:
    • Open terminal and type [email protected]<public Alibaba IP>” and hit enter.
    • Type the password which you have reset in Step2.
    • You are now logged into your server with SSH.

You must have created an Ubuntu server by now and might have tested access to the server from Windows or  Linux or MAC. The next step is to transfer MQTT Broker to Alibaba.

Download the MQTT Broker

The MQTT Broker can be downloaded using the following button. The MQTT Broker has a 30 day trial version which supports upto 100 devices. You have to download the Ubuntu Version

FREE MQTT Broker

Transfer MQTT Broker to Alibaba

For Windows:

  1. Download the PSCP utility from PuTTy.org by clicking the file  name link and saving it to your computer.
  2. The PuTTY SCP (PSCP) client does not require installation in  Windows, but runs directly from a Command Promptwindow.  Move the download PSCP.exe file to the any location and note  the location
  3. Set the Environment Path for the PSCP.exe file.
  4. Open the Command Prompt window.
  5. Type “pscp <BevywiseMQTTRoute zip path> [email protected]<serverpublicIP>:/home/” and hit enter. The zip file will be automatically copy to the /home  location of Alibaba Ubuntu instance

For Linux & Mac:

  1. 1. Open terminal and type below command .
    scp <BevywiseMQTTRoute zip path> [email protected]<serverpublicIP>:/home/”
  2.  Zip file will be saved in the /home location of Alibaba Ubuntu  instance.

Starting MQTT Broker in Alibaba Cloud

It is very easy to set up MQTT Broker in Alibaba cloud. You can simply follow these steps to achieve it.

  1. Login to the Alibaba Ubuntu server via SSH and go to /home location via           “cd /home” command
  2. Type “unzip Bevywise_MQTT_Route_Linux.zip” and hit enter. Now MQTT Broker file will be extract in /home location under the Bevywise folder.
  3. Go to the file Bevywise by below command and hit enter.
    “cd Bevywise/MQTTRoute/bin”
  4. Open the data_store.conf file which is inside the conf folder.

    “vim /home/Bevywise/MQTTRoute/conf/data_store.conf”

    In that file change follow conf and save the file:

    DB_SERVER = MYSQL and give MySQL username and password 

    MYSQL_USER = <username>

    MYSQL_PASSWORD = <password>

  5. Run the Bevywise MQTT broker using the following command. It is advisible to run the Broker in the background mode to make sure the broker runs even after you close the terminal.
    “sh runbroker.sh”

Run MQTT Broker in Alibaba as a Service

If you don’t run MQTT Broker in Alibaba cloud as a service, then it will quit when you close your ssh session. So make sure you run the MQTT Broker as a service.

To run the MQTTBroker as service we need the Monit version 5.25 and it cannot be installed using the apt-get. The procedure to set up the monit is as follows. This application will ensure that the service will be started again when the server gets restarted.

  • Download the Monit for linux X64 or X86 based on your servers.
  • Extract the archive using tar -xzf < downloaded file >
  • Go to monit-5.25.2/bin
  • copy monit to /usr/bin folder – sudo cp monit /usr/bin
  • Go to monit-5.25.2/conf
  • copy the monitrc file to “/etc/” using below command – sudo cp monitrc /etc/
  • Modify the monit conf file. Uncomment the following lines in /etc/monitrc file.  use sudo vi /etc/monitrc. Change the username and password as per your  need.
    set httpd port 2812 and
           use address localhost # only accept connection from localhost  (drop if you use M/Monit)
           allow localhost # allow localhost to connect to the server and
           allow admin:monit # require user ‘admin’ with password ‘monit’ 
  • Add the following at the end of the /etc/monitrc file
    check process MQTTRoute with pidfile < path to Bevywise/MQTTRoute >/Broker.pid
         start program = “< path to Bevywise/MQTTRoute >/bin/runbroker.sh”  with timeout 2 seconds
        stop program = “< path to Bevywise/MQTTRoute >/bin/stopbroker.sh”  with timeout 2 seconds
  • To reload the confirmation changes, run sudo monit reload
  • Add MQTTRoute process to monitoring sudo monit start MQTTRoute

We have hosted a Chinese version of MQTT Broker here.  Do give it a try and let us know your feedback. We would be always ready to help you with setting up the MQTT Broker in Alibaba. Feel free to post your queries and support requirement to our support.