Ubuntu 系统下 Rocket.Chat 部署与配置指南
一、环境准备与系统要求
在Ubuntu系统上部署Rocket.Chat需要满足以下基础条件:
- 系统版本:推荐使用Ubuntu 20.04 LTS或22.04 LTS,这两个版本在软件包兼容性和长期支持方面表现优异。
- 硬件配置:生产环境建议至少4核CPU、8GB内存和50GB磁盘空间,开发环境可适当降低配置。
- 网络要求:需开放80/443端口(HTTP/HTTPS)和3000端口(开发环境默认端口),如使用反向代理需额外配置。
安装前需更新系统软件包索引:
sudo apt update && sudo apt upgrade -y
二、核心依赖安装
1. MongoDB数据库部署
Rocket.Chat依赖MongoDB作为数据存储后端,推荐使用MongoDB 5.0+版本。通过官方仓库安装:
# 导入公钥wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -# 添加仓库echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list# 安装服务sudo apt update && sudo apt install -y mongodb-org# 启动服务sudo systemctl enable --now mongod
配置优化建议:
- 修改
/etc/mongod.conf中的bindIp为本地IP或0.0.0.0(生产环境建议指定内网IP) - 启用认证:取消
security.authorization注释并设置为enabled - 创建专用用户:
use admindb.createUser({user:"rcAdmin",pwd:"SecurePassword123",roles:[{role:"root",db:"admin"}]})use rocketchatdb.createUser({user:"rcUser",pwd:"AppPassword456",roles:[{role:"readWrite",db:"rocketchat"}]})
2. Node.js环境配置
Rocket.Chat需要Node.js 14.x或16.x版本,建议使用nvm进行多版本管理:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bashsource ~/.bashrcnvm install 16nvm use 16
验证安装:
node -v # 应输出v16.x.xnpm -v # 应输出8.x.x
三、Rocket.Chat安装流程
1. 源代码部署方式
# 创建工作目录sudo mkdir -p /opt/rocketchatsudo chown $USER:$USER /opt/rocketchatcd /opt/rocketchat# 克隆最新稳定版git clone -b release-4.8.0 https://github.com/RocketChat/Rocket.Chat.git .# 安装依赖npm install --production# 构建前端资源npm run build
2. 配置文件设置
创建/opt/rocketchat/.env文件,关键配置项:
# 数据库连接MONGO_URL=mongodb://rcUser:AppPassword456@localhost:27017/rocketchat?authSource=admin# 基础URL配置(生产环境必须)ROOT_URL=https://yourdomain.com# 端口配置(默认3000)PORT=3000# 文件上传限制(单位MB)FileUpload_MaxFileSize=100
四、服务管理与启动
1. 使用PM2进程管理
npm install -g pm2pm2 start main.js --name "RocketChat"pm2 savepm2 startup # 生成开机启动配置sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u $USER --hp /home/$USER
2. Nginx反向代理配置
创建/etc/nginx/sites-available/rocketchat:
server {listen 80;server_name yourdomain.com;location / {proxy_pass http://127.0.0.1:3000;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;proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Server $host;}}
启用配置并重启服务:
sudo ln -s /etc/nginx/sites-available/rocketchat /etc/nginx/sites-enabled/sudo nginx -t # 测试配置sudo systemctl restart nginx
五、安全加固与性能优化
1. SSL证书配置
使用Let’s Encrypt免费证书:
sudo apt install certbot python3-certbot-nginxsudo certbot --nginx -d yourdomain.com
配置自动续期:
sudo certbot renew --dry-run
2. 防火墙规则
sudo ufw allow 80/tcpsudo ufw allow 443/tcpsudo ufw allow 22/tcp # SSH端口sudo ufw enable
3. 性能调优参数
在/etc/mongod.conf中优化:
storage:wiredTiger:engineConfig:cacheSizeGB: 2 # 根据内存1/4配置operationProfiling:mode: slowOpslowOpThresholdMs: 100
Node.js启动参数优化:
export NODE_OPTIONS="--max-old-space-size=4096"pm2 restart RocketChat
六、常见问题解决方案
-
数据库连接失败:
- 检查MongoDB服务状态:
sudo systemctl status mongod - 验证认证配置:
mongo -u rcAdmin -p --authenticationDatabase admin
- 检查MongoDB服务状态:
-
前端资源加载失败:
- 清除缓存:
rm -rf /opt/rocketchat/programs/server/assets - 重新构建:
npm run build
- 清除缓存:
-
文件上传限制:
在.env中调整:FileUpload_MaxFileSize=200 # 单位MBFileUpload_StorageType=GridFS # 大文件存储方案
七、升级与维护策略
-
版本升级流程:
cd /opt/rocketchatgit fetch --tagsgit checkout 4.9.0 # 指定目标版本npm install --productionnpm run buildpm2 restart RocketChat
-
备份方案:
```bash数据库备份
mongodump —uri=”mongodb://rcAdmin:password@localhost:27017/admin” —out=/backups/$(date +%F)
文件备份
tar -czf /backups/uploads_$(date +%F).tar.gz /opt/rocketchat/uploads
```
通过以上系统化的部署方案,开发者可以在Ubuntu环境中构建出稳定高效的Rocket.Chat服务。实际部署时建议先在测试环境验证配置,生产环境需结合监控系统(如Prometheus+Grafana)实现实时状态监控。对于高并发场景,可考虑采用MongoDB分片集群和Rocket.Chat集群部署方案。