121 lines
4.8 KiB
Markdown
121 lines
4.8 KiB
Markdown
---
|
||||
|
|
page-title: "Mosquitto MQTT Installation Guide for Debian 11: Easy Setup - Shapehost"
|
|||
|
|
url: https://shape.host/resources/mosquitto-mqtt-installation-guide-for-debian-11-easy-setup
|
|||
|
|
date: "2024-12-19 17:52:04"
|
|||
|
|
---
|
|||
|
|
## Introduction
|
|||
|
|
|
|||
|
|
In this article, we will guide you through the process of installing and configuring Mosquitto MQTT Message Broker on a Debian 11 server. Mosquitto is a free and open-source message broker implementation of the MQTT protocol. It is a lightweight and efficient solution that is widely used for IoT (Internet of Things) and other messaging applications.
|
|||
|
|
|
|||
|
|
## Prerequisites
|
|||
|
|
|
|||
|
|
Before we begin, make sure you have the following requirements:
|
|||
|
|
|
|||
|
|
1. A Debian 11 server – For this tutorial, we will use a server with the hostname ‘mosquitto-server’.
|
|||
|
|
2. A non-root user with root/administrator privileges.
|
|||
|
|
|
|||
|
|
## Step 1: Installing Mosquitto Server and Client
|
|||
|
|
|
|||
|
|
To install Mosquitto on Debian 11, follow these steps:
|
|||
|
|
|
|||
|
|
1. Update and refresh your Debian package index by running the following command:
|
|||
|
|
|
|||
|
|
sudo apt update
|
|||
|
|
|
|||
|
|
2. Search for the Mosquitto package using the following command:
|
|||
|
|
|
|||
|
|
sudo apt search mosquitto
|
|||
|
|
|
|||
|
|
3. Install the Mosquitto server and client packages by running the following command:
|
|||
|
|
|
|||
|
|
sudo apt install mosquitto mosquitto\-clients
|
|||
|
|
|
|||
|
|
4. Verify that the Mosquitto service is enabled and running by using the following command:
|
|||
|
|
|
|||
|
|
sudo systemctl is\-enabled mosquitto
|
|||
|
|
sudo systemctl status mosquitto
|
|||
|
|
|
|||
|
|
## Step 2: Setting up Authentication on Mosquitto
|
|||
|
|
|
|||
|
|
By default, Mosquitto does not have authentication enabled. To secure your Mosquitto deployment, it is recommended to enable authentication. Follow these steps to set up authentication on Mosquitto:
|
|||
|
|
|
|||
|
|
1. Create a new Mosquitto user and password by running the following command:
|
|||
|
|
|
|||
|
|
sudo mosquitto\_passwd \-c /etc/mosquitto/.passwd shapehost
|
|||
|
|
|
|||
|
|
Replace ‘shapehost’ with your desired username.
|
|||
|
|
|
|||
|
|
2. Create a new Mosquitto configuration file by running the following command:
|
|||
|
|
|
|||
|
|
sudo nano /etc/mosquitto/conf.d/auth.conf
|
|||
|
|
|
|||
|
|
3. Add the following configuration to the file:
|
|||
|
|
|
|||
|
|
listener 1883
|
|||
|
|
allow\_anonymous false
|
|||
|
|
password\_file /etc/mosquitto/.passwd
|
|||
|
|
|
|||
|
|
4. Save the file and exit the editor.
|
|||
|
|
5. Restart the Mosquitto service to apply the new changes:
|
|||
|
|
|
|||
|
|
sudo systemctl restart mosquitto
|
|||
|
|
|
|||
|
|
## Step 3: Securing Mosquitto with SSL/TLS Certificates
|
|||
|
|
|
|||
|
|
To enhance the security of your Mosquitto installation, you can enable SSL/TLS certificates. Follow these steps to secure your Mosquitto deployment:
|
|||
|
|
|
|||
|
|
1. Generate the dhparam certificate by running the following command:
|
|||
|
|
|
|||
|
|
sudo openssl dhparam \-out /etc/mosquitto/certs/dhparam.pem 2048
|
|||
|
|
|
|||
|
|
2. Change the ownership of the Mosquitto certs directory to the user ‘mosquitto’:
|
|||
|
|
|
|||
|
|
sudo chown \-R mosquitto: /etc/mosquitto/certs
|
|||
|
|
|
|||
|
|
3. Create a new additional configuration file for SSL/TLS by running the following command:
|
|||
|
|
|
|||
|
|
sudo nano /etc/mosquitto/conf.d/ssl.conf
|
|||
|
|
|
|||
|
|
4. Add the following configuration to the file:
|
|||
|
|
|
|||
|
|
listener 8883
|
|||
|
|
certfile /etc/letsencrypt/live/msqt.shapehost.io/fullchain.pem
|
|||
|
|
cafile /etc/letsencrypt/live/msqt.shapehost.io/chain.pem
|
|||
|
|
keyfile /etc/letsencrypt/live/msqt.shapehost.io/privkey.pem
|
|||
|
|
dhparamfile /etc/mosquitto/certs/dhparam.pem
|
|||
|
|
|
|||
|
|
5. Save the file and exit the editor.
|
|||
|
|
6. Restart the Mosquitto service to apply the new changes:
|
|||
|
|
|
|||
|
|
sudo systemctl restart mosquitto
|
|||
|
|
|
|||
|
|
## Step 4: Enabling WebSockets on Mosquitto
|
|||
|
|
|
|||
|
|
WebSockets allow for a persistent full-duplex communication channel between the server and the client. To enable WebSockets on Mosquitto, follow these steps:
|
|||
|
|
|
|||
|
|
1. Create a new configuration file for WebSockets by running the following command:
|
|||
|
|
|
|||
|
|
sudo nano /etc/mosquitto/conf.d/websockets.conf
|
|||
|
|
|
|||
|
|
2. Add the following configuration to the file:
|
|||
|
|
|
|||
|
|
listener 8083
|
|||
|
|
protocol websockets
|
|||
|
|
certfile /etc/letsencrypt/live/msqt.shapehost.io/fullchain.pem
|
|||
|
|
cafile /etc/letsencrypt/live/msqt.shapehost.io/chain.pem
|
|||
|
|
keyfile /etc/letsencrypt/live/msqt.shapehost.io/privkey.pem
|
|||
|
|
|
|||
|
|
3. Save the file and exit the editor.
|
|||
|
|
4. Restart the Mosquitto service to apply the new changes:
|
|||
|
|
|
|||
|
|
sudo systemctl restart mosquitto
|
|||
|
|
|
|||
|
|
## Conclusion
|
|||
|
|
|
|||
|
|
In this article, we have provided a step-by-step guide on how to install and configure Mosquitto MQTT Message Broker on a Debian 11 server. We covered topics such as installing Mosquitto, setting up authentication, securing Mosquitto with SSL/TLS certificates, and enabling WebSockets. By following these instructions, you can create a secure and reliable MQTT message broker for your IoT and messaging applications.
|
|||
|
|
|
|||
|
|
For more advanced features and reliable cloud hosting solutions, consider exploring the services provided by Shape.host, such as [Cloud VPS](https://shape.host/). Shape.host offers scalable and secure cloud hosting solutions to empower businesses with efficient and reliable infrastructure.
|
|||
|
|
|
|||
|
|

|
|||
|
|
|
|||
|
|
##### Christian Wells
|