本地服务器部署 OpenClaw 完整手册!数据完全可控,内网速度快
本地服务器部署 OpenClaw 完整手册!数据完全可控,内网速度快

大家好,我是正在实战各种 AI 项目的程序员晚枫。

😫 开篇:有些数据,真的不能上云

"晚枫,我们公司的数据敏感,不能上公有云,怎么办?"

这种情况,只能本地部署。金融、政务、医疗、研发数据,本地部署最安全

今天把本地服务器部署 OpenClaw 的完整手册整理出来,照着做,60 分钟搞定。

📋 部署前准备

1. 硬件要求

用途CPU内存硬盘网络预算
开发测试4 核8G256G SSD千兆3000 元
生产环境8 核16G512G SSD千兆6000 元
高并发16 核32G1T SSD万兆15000 元

2. 系统选择

1
2
3
推荐:Ubuntu Server 22.04 LTS
备选:CentOS 7.9
不推荐:Windows(资源占用高)

3. 网络配置

1
2
3
4
✓ 固定 IP 地址(内网或公网)
✓ 域名解析(可选,内网 DNS 也行)
✓ 防火墙配置
✓ 路由器端口映射(如需外网访问)

🚀 部署步骤(超详细)

第 1 步:准备服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
方案 1:购买品牌服务器
- 戴尔 PowerEdge R740
- 惠普 ProLiant DL380
- 联想 ThinkSystem SR650
价格:1-3 万(看配置)

方案 2:组装服务器
- CPU:Intel Xeon 或 AMD EPYC
- 内存:ECC 内存
- 硬盘:企业级 SSD
- 机箱:2U/4U 机架式
价格:5 千 -1 万(性价比高)

方案 3:用旧电脑改造
- CPU:i5/i7 以上
- 内存:16G+
- 硬盘:SSD 256G+
价格:0 元(废物利用)

第 2 步:安装操作系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 下载 Ubuntu Server 22.04 LTS
# 官网:https://ubuntu.com/download/server

# 制作启动 U 盘
# 工具:Rufus(Windows)或 dd(Mac/Linux)

# 安装步骤:
1. 插入 U 盘,开机从 U 盘启动
2. 选择语言(English)
3. 选择键盘布局
4. 配置网络(DHCP 或静态 IP)
5. 配置代理(一般不用)
6. 配置磁盘分区(用整个磁盘)
7. 设置用户名和密码
8. 安装 SSH 服务(必须选)
9. 等待安装完成
10. 重启,拔掉 U 盘

第 3 步:基础配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# SSH 登录
ssh your-username@服务器 IP

# 更新系统
sudo apt update && sudo apt upgrade -y

# 安装必要工具
sudo apt install -y git curl wget vim htop net-tools unzip

# 配置静态 IP(如果需要)
sudo cat > /etc/netplan/00-installer-config.yaml << EOF
network:
version: 2
ethernets:
eth0:
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
EOF

sudo netplan apply

# 配置 hostname
sudo hostnamectl set-hostname openclaw-server
sudo cat >> /etc/hosts << EOF
127.0.0.1 openclaw-server
EOF

第 4 步:安装 Docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 安装 Docker
curl -fsSL https://get.docker.com | sudo bash
sudo systemctl enable docker
sudo systemctl start docker

# 添加用户到 docker 组
sudo usermod -aG docker $USER
# 需要重新登录生效

# 安装 Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

# 验证
docker --version
docker-compose --version

第 5 步:部署 OpenClaw

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 创建项目目录
sudo mkdir -p /opt/openclaw
cd /opt/openclaw

# 克隆代码
sudo git clone https://github.com/openclaw/openclaw.git
cd openclaw

# 修改所有者
sudo chown -R $USER:$USER .

# 创建环境变量
cat > .env << EOF
OPENCLAW_PORT=8000
OPENCLAW_HOST=0.0.0.0
DATABASE_URL=postgresql://openclaw:your_secure_password@postgres:5432/openclaw
REDIS_URL=redis://redis:6379/0
SECRET_KEY=$(openssl rand -hex 32)
DEBUG=False

# 本地存储配置
MEDIA_ROOT=/app/data/media
STATIC_ROOT=/app/data/static
EOF

# 启动服务
docker-compose up -d

# 查看状态
docker-compose ps
docker-compose logs -f

第 6 步:配置 Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 安装 Nginx
sudo apt install -y nginx

# 创建配置
sudo cat > /etc/nginx/sites-available/openclaw << EOF
upstream openclaw {
server 127.0.0.1:8000;
}

server {
listen 80;
server_name openclaw.local; # 内网域名

# 安全头
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;

location / {
proxy_pass http://openclaw;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
}
EOF

# 启用配置
sudo ln -s /etc/nginx/sites-available/openclaw /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

第 7 步:配置内网 DNS(可选)

1
2
3
4
在路由器或内网 DNS 服务器上添加:
openclaw.local → 192.168.1.100

这样内网用户可以用域名访问

第 8 步:配置外网访问(可选)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
如果需要从外网访问:

1. 在路由器配置端口映射:
- 外部端口 80 → 内部 192.168.1.100:80
- 外部端口 443 → 内部 192.168.1.100:443

2. 配置动态 DNS(如果没有固定公网 IP):
- 使用花生壳、阿里 DDNS 等

3. 配置防火墙:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

⚠️ 注意:外网访问要做好安全防护

🔧 安全加固(重要!)

1. 配置防火墙

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 安装 UFW
sudo apt install -y ufw

# 允许 SSH
sudo ufw allow 22/tcp

# 允许 HTTP/HTTPS
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# 启用防火墙
sudo ufw enable

# 查看状态
sudo ufw status

2. 配置 SSH 安全

1
2
3
4
5
6
7
8
9
# 修改 SSH 配置
sudo cat > /etc/ssh/sshd_config << EOF
Port 2222 # 改端口
PermitRootLogin no # 禁止 root 登录
PasswordAuthentication no # 禁用密码登录(用密钥)
PubkeyAuthentication yes # 启用密钥登录
EOF

sudo systemctl restart sshd

3. 配置自动更新

1
2
3
4
5
6
7
8
9
# 安装自动更新
sudo apt install -y unattended-upgrades

# 配置自动更新安全补丁
sudo cat > /etc/apt/apt.conf.d/50unattended-upgrades << EOF
Unattended-Upgrade::Allowed-Origins {
"\${distro_id}-stable-security";
};
EOF

4. 配置数据备份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 创建备份脚本
cat > /opt/backup.sh << EOF
#!/bin/bash
BACKUP_DIR=/backup
DATE=\$(date +%Y%m%d)

# 创建备份目录
mkdir -p \$BACKUP_DIR

# 备份数据库
docker-compose exec -T postgres pg_dump -U openclaw openclaw > \$BACKUP_DIR/db_\$DATE.sql

# 备份数据卷
tar -czf \$BACKUP_DIR/data_\$DATE.tar.gz /opt/openclaw/data

# 删除 30 天前的备份
find \$BACKUP_DIR -name "*.sql" -mtime +30 -delete
find \$BACKUP_DIR -name "*.tar.gz" -mtime +30 -delete

echo "Backup completed: \$DATE"
EOF

chmod +x /opt/backup.sh

# 添加定时任务(每天凌晨 2 点)
crontab -e
0 2 * * * /bin/bash /opt/backup.sh

5. 配置监控告警

1
2
3
4
5
6
7
8
# 安装监控工具
sudo apt install -y prometheus-node-exporter

# 配置告警(邮件)
sudo apt install -y postfix

# 或者用更专业的监控
# Docker 部署 Prometheus + Grafana

📊 性能优化建议

1. 磁盘 IO 优化

1
2
3
4
5
# 检查磁盘 IO
iostat -x 1

# 如果是机械硬盘,考虑升级到 SSD
# SSD 性能提升 10 倍+

2. 内存优化

1
2
3
4
5
6
7
8
9
10
11
# 查看内存使用
free -h
htop

# 如果内存不足,考虑:
# 1. 增加物理内存
# 2. 配置 swap
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

3. 网络优化

1
2
3
4
5
# 检查网络带宽
iperf3 -s # 服务器端
iperf3 -c 服务器 IP # 客户端

# 如果是千兆网络,理论速度 125MB/s

💰 成本分析

一次性投入

项目低配中配高配
服务器3000 元6000 元15000 元
硬盘500 元1000 元3000 元
网络设备200 元500 元2000 元
UPS 电源500 元1000 元3000 元
合计4200 元8500 元23000 元

持续成本

项目成本
电费100-300 元/月
带宽100-500 元/月(如需公网)
维护自己维护 0 元
月成本200-800 元/月

与云对比

1
2
3
4
5
6
7
8
云服务器(4 核 8G):2000 元/月 = 2.4 万/年
本地服务器:8500 元一次性 + 3600 元/年 = 1.2 万/年

3 年成本:
- 云:7.2 万
- 本地:2 万

节省:5.2 万!

🔧 常见问题排查

问题 1:服务器无法访问

1
2
3
4
5
6
7
8
9
# 检查网络
ping 服务器 IP

# 检查防火墙
sudo ufw status

# 检查服务
sudo systemctl status nginx
sudo systemctl status docker

问题 2:Docker 容器启动失败

1
2
3
4
5
6
7
8
9
# 查看日志
docker-compose logs

# 检查资源
free -h
df -h

# 重启 Docker
sudo systemctl restart docker

问题 3:数据备份失败

1
2
3
4
5
6
7
8
# 检查磁盘空间
df -h

# 检查备份脚本
bash -x /opt/backup.sh

# 检查定时任务
crontab -l

🚀 更多应用场景

  • 企业内部系统
  • 研发测试环境
  • 数据敏感项目
  • 离线环境部署

💬 金句总结

数据在自己手里,才是最安全的。

本地部署,用一次性投入换长期安全和成本优势。

📚 相关阅读

🔗 联系方式

平台账号/链接
微信扫码加好友
微博@程序员晚枫
知乎@程序员晚枫
抖音@程序员晚枫
小红书@程序员晚枫
B 站Python 自动化办公社区

主营业务:AI 编程培训、企业内训、技术咨询


本地部署,数据完全可控!

🎓 AI 编程实战课程

想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!