最新linux下的mosquitto服务安装及使用(避坑指南),源码+原理+手写框架
#!/bin/bash
# 安装mosquitto MQTT代理服务
# 定义mosquitto版本和源码下载地址
MOSQUITTO_VERSION="2.0.14"
MOSQUITTO_DOWNLOAD_URL="https://mosquitto.org/files/source/mosquitto-${MOSQUITTO_VERSION}.tar.gz"
# 安装依赖
sudo apt-get update
sudo apt-get install -y build-essential cmake libssl-dev uuid-dev
# 下载mosquitto源码
cd /tmp || exit
wget "${MOSQUITTO_DOWNLOAD_URL}"
tar -xzf "mosquitto-${MOSQUITTO_VERSION}.tar.gz"
cd "mosquitto-${MOSQUITTO_VERSION}" || exit
# 编译和安装mosquitto
cmake -DCMAKE_INSTALL_PREFIX=/usr .
make
sudo make install
# 创建系统服务文件
sudo tee /etc/systemd/system/mosquitto.service <<EOF
[Unit]
Description=Mosquitto MQTT v${MOSQUITTO_VERSION} Broker
After=network.target
[Service]
User=mosquitto
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
# 创建mosquitto用户和配置目录
sudo adduser --system --no-create-home --disabled-login --group mosquitto
sudo mkdir /etc/mosquitto
sudo touch /etc/mosquitto/mosquitto.conf
# 重新加载系统服务守护进程
sudo systemctl daemon-reload
# 启动mosquitto服务
sudo systemctl enable mosquitto
sudo systemctl start mosquitto
# 检查服务状态
sudo systemctl status mosquitto
这段代码提供了在Ubuntu系统上安装mosquitto服务的步骤。它首先安装了编译mosquitto所需的依赖,然后下载、解压并编译了mosquitto源码。接着,它创建了系统服务文件,并为mosquitto代理创建了用户和配置目录。最后,它重新加载了系统服务守护进程,启动并检查了mosquitto服务的状态。这个过程提供了一个简洁的安装和配置mosquitto服务的示例。
评论已关闭