Comprehensive Step
Comprehensive Step
Environment For Hosting A Mosquitto MQTT Broker With SSL/TLS Encryption Using
Docker, Nginx, Certbot (Let's Encrypt), And Configuring The Necessary Domain And SSL
Certificates:
Complete Setup for Mosquitto with SSL/TLS using Docker, Nginx, and Certbot
Overview
2. Certbot for obtaining and renewing SSL certificates from Let's Encrypt.
By following these steps, you will configure a secure environment for your Mosquitto
MQTT broker using SSL certificates and make sure everything is properly integrated.
Prerequisites
• Ubuntu Server (or any system with Ubuntu as the base OS).
Command:
This command updates the local package database to ensure you have the latest
available package versions for installation.
Command:
Command:
This starts the Docker service, which will allow Docker containers to run on your
system.
Command:
This ensures Docker starts automatically every time your system boots.
Command:
Installs Certbot, a tool for obtaining and renewing SSL certificates from Let's Encrypt.
This installs Nginx, which will serve your application and manage incoming web traffic.
Command:
This starts the Nginx web server to begin handling incoming web requests.
Command:
This ensures Nginx will automatically start whenever your system boots.
Command:
Installs Certbot along with the Nginx plugin, which helps in obtaining SSL certificates
and automatically configuring Nginx.
Command:
code /etc/nginx/sites-available/skyray.algojaxon.com
This command opens the Nginx site configuration file for your domain
(skyray.algojaxon.com). You'll use this file to configure your Nginx server for SSL.
Command:
Command:
This command obtains and automatically configures the SSL certificate for Nginx,
ensuring that all traffic is encrypted.
Command:
crontab -e
This opens the cron job configuration to set up automatic renewal for the SSL
certificate. Let's Encrypt certificates are valid for 90 days, and Certbot can be set to
auto-renew.
Command:
sudo nginx -t
This tests the Nginx configuration for syntax errors before restarting Nginx to apply the
changes.
Command:
This restarts the Nginx service to apply the changes made to the configuration file.
Step 16: Set Up Mosquitto MQTT Broker with SSL/TLS in Docker
Now, we'll configure the Mosquitto MQTT broker to use the SSL certificates you obtained
from Let's Encrypt.
Directory Structure
After completing the setup, your directory structure should look like this:
/root/mosquitto/
├── certs/
│ ├── fullchain.pem
│ └── privkey.pem
├── config/
│ └── mosquitto.conf
├── data/
└── log/
Create a directory on the host system where the certificates will be stored for mounting
into the Docker container:
Command:
mkdir -p /root/mosquitto/certs
Copy the SSL certificates from /etc/letsencrypt to the newly created directory:
Command:
cp /etc/letsencrypt/live/skyray.algojaxon.com/fullchain.pem /root/mosquitto/certs/
cp /etc/letsencrypt/live/skyray.algojaxon.com/privkey.pem /root/mosquitto/certs/
Run the Mosquitto container with SSL/TLS encryption using Docker. This command
mounts the certificates and config directories:
Command:
docker run -d \
--name mosquitto \
eclipse-mosquitto
Edit the Mosquitto configuration file (mosquitto.conf) to specify SSL settings. Add the
following configuration:
listener 1883
protocol mqtt
allow_anonymous true
listener 8080
protocol websockets
allow_anonymous true
# Secure WebSocket listener (8081)
listener 8081
protocol websockets
cafile /mosquitto/certs/fullchain.pem
certfile /mosquitto/certs/fullchain.pem
keyfile /mosquitto/certs/privkey.pem
allow_anonymous true
6. Restart Mosquitto
Command:
To verify that the Mosquitto broker is using SSL/TLS, use an SSL-enabled MQTT client
(like MQTT.fx or mosquitto_pub) and connect to the broker on port 8883.
Conclusion
1. Nginx is configured to handle web traffic securely using SSL certificates from
Let's Encrypt.
3. Docker is used to run the Mosquitto MQTT broker in a container, with SSL/TLS
encryption.
This setup provides a secure environment for your Mosquitto MQTT broker, enabling
encrypted communication via SSL/TLS while automating certificate management.