🗣 Wd提问: VPS安装搭建云物联网
🤖 Ai回答: 搭建云物联网(IoT Cloud)系统在VPS(虚拟私有服务器)上是一个非常实用且灵活的方案,尤其适合个人开发者、小型企业或实验项目。下面我将为你提供一个完整的、分步骤的搭建指南,涵盖从环境准备到核心服务部署。
🎯 目标:在VPS上搭建一个可扩展的云物联网平台
✅ 功能目标:
设备接入与数据采集
数据存储(数据库)
实时监控与可视化
远程控制设备(可选)
支持MQTT协议(主流IoT通信协议)
🖥️ 系统要求
| 项目 | 推荐配置 |
| VPS系统 | Ubuntu 20.04 / 22.04 LTS (推荐) 或 Debian 11 |
| CPU | 1核以上(建议2核) |
| 内存 | 1GB以上(建议2GB) |
| 存储 | 20GB以上(SSD优先) |
| 域名 | 可选,但推荐用于公网访问 |
| 公网IP | 必须(或使用反向代理+动态DNS) |
🧩 第一步:基础环境准备
1.1 更新系统并安装必要工具
bash
sudo apt update && sudo apt upgrade -y
sudo apt install curl wget git vim net-tools -y
1.2 设置防火墙(UFW)
bash
sudo ufw enable
sudo ufw allow ssh
sudo ufw allow 80/tcp HTTP
sudo ufw allow 443/tcp HTTPS
sudo ufw allow 1883/tcp MQTT 默认端口
sudo ufw allow 8083/tcp Mosquitto Web管理界面(可选)
🌐 第二步:部署MQTT Broker(消息中间件)
MQTT是物联网最常用的消息协议。
2.1 安装Mosquitto
bash
sudo apt install mosquitto mosquitto-clients -y
2.2 启动并设置开机自启
bash
sudo systemctl start mosquitto
sudo systemctl enable mosquitto
2.3 配置安全认证(可选但推荐)
编辑配置文件:
bash
sudo nano /etc/mosquitto/conf.d/auth.conf
添加:
conf
allow_anonymous false
password_file /etc/mosquitto/pwfile
创建密码文件:
bash
sudo mosquitto_passwd -c /etc/mosquitto/pwfile your_username
输入密码两次
重启服务:
bash
sudo systemctl restart mosquitto
💡 测试连接:`mosquitto_sub -h localhost -u your_username -P your_password -t test/topic`
💾 第三步:部署数据库(InfluxDB + Grafana)
3.1 安装 InfluxDB(时序数据库,适合存储传感器数据)
bash
添加官方仓库
wget -qOhttps://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/os-release
echo deb https://repos.influxdata.com/${ID} ${VERSION_ID} stable | sudo tee /etc/apt/sources.list.d/influxdb.list
sudo apt update
sudo apt install influxdb -y
启动服务:
bash
sudo systemctl start influxdb
sudo systemctl enable influxdb
初始化数据库:
bash
influx -execute 'CREATE DATABASE iot_data'
3.2 安装 Grafana(数据可视化)
bash
添加Grafana仓库
echo deb https://packages.grafana.com/oss/deb stable main | sudo tee -a /etc/apt/sources.list.d/grafana.list
wget -q -O https://packages.grafana.com/gpg.key | sudo apt-key add -
sudo apt update
sudo apt install grafana -y
启动服务:
bash
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
🌐 访问地址:`http://你的VPS_IP:3000`
默认账号:admin / 密码:admin(首次登录后修改)
🔗 第四步:部署Node-RED(可视化编程工具)
Node-RED 是一个基于流的开发工具,非常适合快速搭建物联网应用。
4.1 安装 Node.js 和 npm
bash
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install nodejs -y
4.2 安装 Node-RED
bash
sudo npm install -g unsafe-perm node-red
4.3 创建服务文件(systemd)
bash
sudo nano /etc/systemd/system/nodered.service
内容:
ini
[Unit]
Description=Node-RED
After=syslog.target network.target
[Service]
Type=simple
User=nodered
ExecStart=/usr/bin/node-red-start
Restart=on-failure
StandardOutput=file:/var/log/nodered.log
StandardError=file:/var/log/nodered.err
[Install]
WantedBy=multi-user.target
创建用户:
bash
sudo useradd -r -s /bin/bash -d /home/nodered nodered
sudo mkdir /home/nodered/.node-red
sudo chown -R nodered:nodered /home/nodered/.node-red
启用服务:
bash
sudo systemctl daemon-reload
sudo systemctl enable nodered
sudo systemctl start nodered
🌐 访问地址:`http://你的VPS_IP:1880`
📦 第五步:集成MQTT + InfluxDB + Grafana
5.1 在Node-RED中添加MQTT节点
`Inject` → `MQTT out` → 发送数据到MQTT Broker
`MQTT in` → 接收数据 → `InfluxDB out`
5.2 安装InfluxDB插件
在Node-RED中:
Manage Palette → Install → search node-red-node-influxdb
5.3 配置InfluxDB节点
Host: `localhost`
Port: `8086`
Database: `iot_data`
Username/Password: (根据你之前设置的)
5.4 配置Grafana仪表盘
添加数据源:InfluxDB → URL: `http://localhost:8086`
创建Dashboard → 使用变量和面板展示温度、湿度等数据
🌍 第六步:公网访问与安全加固
6.1 使用Nginx做反向代理(推荐)
bash
sudo apt install nginx -y
配置Nginx:
bash
sudo nano /etc/nginx/sites-available/iot
内容示例:
nginx
server {
listen 80;
server_name yourdomain.com; 替换为你的域名
location / {
proxy_pass http://localhost:1880;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /mqtt {
proxy_pass http://localhost:1883;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
启用站点:
bash
sudo ln -s /etc/nginx/sites-available/iot /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
6.2 开启HTTPS(Let's Encrypt)
bash
sudo apt install certbot python3-certbot-nginx -y
sudo certbot nginx -d yourdomain.com
📱 第七步:设备端接入(以ESP32为例)
示例代码(Arduino IDE)
cpp
include <WiFi.h
include <PubSubClient.h
const char* ssid = your_wifi_ssid;
const char* password = your_wifi_password;
const char* mqtt_server = your_vps_ip_or_domain;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(.);
}
Serial.println(WiFi connected);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// 模拟传感器数据
int sensorValue = analogRead(A0); // 或者用其他传感器
String payload = String(sensorValue);
client.publish(sensor/temp, payload.c_str());
delay(50
📣 商家广告:





0
IP地址: 156.247.3.41
搜索次数: 0
提问时间: 2025-11-12 08:24:11
🛒 域名购买:
ww.fj.cn
nf.gs
yrnz.cn
wy.gs
e-j.com.cn
chesou.com.cn
gz.gold
81.biz
zqll.cn
wk.gs
❓️ 热门提问:
服务器 国内
a100云服务器
ip电话是什么意思
香港服务器 哪个好
海外动态ip服务器
电子商务关键词广告
关键词搜索引擎PaaS
搜索平台有哪些?
蜘蛛池MySQL一键脚本
极速idc
豌豆Ai站群搜索引擎系统
🌐 域名评估:
xzqi.cn
9947.com
zuqox.com
rr.ha.cn
vtol.ren
su7.host
pdum.cn
tt.gs.cn
drone.nm.cn
le.tj.cn
⛏ 最新挖掘:
凯里酸汤鱼
贵阳烤脑花
都匀毛尖茶
花溪牛肉粉
贵阳烤生蚝
贵阳烤豆腐
贵阳纸包鱼
贵阳特色菜
🖌 热门作画:
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊:
🔗 友情链接:
外网域名购买
搜全网
ai提问
🧰 站长工具:
Ai工具 whois查询 搜索
📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持:本站由豌豆Ai提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。