Ubuntu 系统下 Rocket.Chat 部署与配置指南

Ubuntu 系统下 Rocket.Chat 部署与配置指南

一、环境准备与系统要求

在Ubuntu系统上部署Rocket.Chat需要满足以下基础条件:

  • 系统版本:推荐使用Ubuntu 20.04 LTS或22.04 LTS,这两个版本在软件包兼容性和长期支持方面表现优异。
  • 硬件配置:生产环境建议至少4核CPU、8GB内存和50GB磁盘空间,开发环境可适当降低配置。
  • 网络要求:需开放80/443端口(HTTP/HTTPS)和3000端口(开发环境默认端口),如使用反向代理需额外配置。

安装前需更新系统软件包索引:

  1. sudo apt update && sudo apt upgrade -y

二、核心依赖安装

1. MongoDB数据库部署

Rocket.Chat依赖MongoDB作为数据存储后端,推荐使用MongoDB 5.0+版本。通过官方仓库安装:

  1. # 导入公钥
  2. wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
  3. # 添加仓库
  4. 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
  5. # 安装服务
  6. sudo apt update && sudo apt install -y mongodb-org
  7. # 启动服务
  8. sudo systemctl enable --now mongod

配置优化建议:

  • 修改/etc/mongod.conf中的bindIp为本地IP或0.0.0.0(生产环境建议指定内网IP)
  • 启用认证:取消security.authorization注释并设置为enabled
  • 创建专用用户:
    1. use admin
    2. db.createUser({user:"rcAdmin",pwd:"SecurePassword123",roles:[{role:"root",db:"admin"}]})
    3. use rocketchat
    4. db.createUser({user:"rcUser",pwd:"AppPassword456",roles:[{role:"readWrite",db:"rocketchat"}]})

2. Node.js环境配置

Rocket.Chat需要Node.js 14.x或16.x版本,建议使用nvm进行多版本管理:

  1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
  2. source ~/.bashrc
  3. nvm install 16
  4. nvm use 16

验证安装:

  1. node -v # 应输出v16.x.x
  2. npm -v # 应输出8.x.x

三、Rocket.Chat安装流程

1. 源代码部署方式

  1. # 创建工作目录
  2. sudo mkdir -p /opt/rocketchat
  3. sudo chown $USER:$USER /opt/rocketchat
  4. cd /opt/rocketchat
  5. # 克隆最新稳定版
  6. git clone -b release-4.8.0 https://github.com/RocketChat/Rocket.Chat.git .
  7. # 安装依赖
  8. npm install --production
  9. # 构建前端资源
  10. npm run build

2. 配置文件设置

创建/opt/rocketchat/.env文件,关键配置项:

  1. # 数据库连接
  2. MONGO_URL=mongodb://rcUser:AppPassword456@localhost:27017/rocketchat?authSource=admin
  3. # 基础URL配置(生产环境必须)
  4. ROOT_URL=https://yourdomain.com
  5. # 端口配置(默认3000)
  6. PORT=3000
  7. # 文件上传限制(单位MB)
  8. FileUpload_MaxFileSize=100

四、服务管理与启动

1. 使用PM2进程管理

  1. npm install -g pm2
  2. pm2 start main.js --name "RocketChat"
  3. pm2 save
  4. pm2 startup # 生成开机启动配置
  5. 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

  1. server {
  2. listen 80;
  3. server_name yourdomain.com;
  4. location / {
  5. proxy_pass http://127.0.0.1:3000;
  6. proxy_http_version 1.1;
  7. proxy_set_header Upgrade $http_upgrade;
  8. proxy_set_header Connection "upgrade";
  9. proxy_set_header Host $host;
  10. proxy_set_header X-Real-IP $remote_addr;
  11. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  12. proxy_set_header X-Forwarded-Proto $scheme;
  13. proxy_set_header X-Forwarded-Host $host;
  14. proxy_set_header X-Forwarded-Server $host;
  15. }
  16. }

启用配置并重启服务:

  1. sudo ln -s /etc/nginx/sites-available/rocketchat /etc/nginx/sites-enabled/
  2. sudo nginx -t # 测试配置
  3. sudo systemctl restart nginx

五、安全加固与性能优化

1. SSL证书配置

使用Let’s Encrypt免费证书:

  1. sudo apt install certbot python3-certbot-nginx
  2. sudo certbot --nginx -d yourdomain.com

配置自动续期:

  1. sudo certbot renew --dry-run

2. 防火墙规则

  1. sudo ufw allow 80/tcp
  2. sudo ufw allow 443/tcp
  3. sudo ufw allow 22/tcp # SSH端口
  4. sudo ufw enable

3. 性能调优参数

/etc/mongod.conf中优化:

  1. storage:
  2. wiredTiger:
  3. engineConfig:
  4. cacheSizeGB: 2 # 根据内存1/4配置
  5. operationProfiling:
  6. mode: slowOp
  7. slowOpThresholdMs: 100

Node.js启动参数优化:

  1. export NODE_OPTIONS="--max-old-space-size=4096"
  2. pm2 restart RocketChat

六、常见问题解决方案

  1. 数据库连接失败

    • 检查MongoDB服务状态:sudo systemctl status mongod
    • 验证认证配置:mongo -u rcAdmin -p --authenticationDatabase admin
  2. 前端资源加载失败

    • 清除缓存:rm -rf /opt/rocketchat/programs/server/assets
    • 重新构建:npm run build
  3. 文件上传限制
    .env中调整:

    1. FileUpload_MaxFileSize=200 # 单位MB
    2. FileUpload_StorageType=GridFS # 大文件存储方案

七、升级与维护策略

  1. 版本升级流程

    1. cd /opt/rocketchat
    2. git fetch --tags
    3. git checkout 4.9.0 # 指定目标版本
    4. npm install --production
    5. npm run build
    6. pm2 restart RocketChat
  2. 备份方案
    ```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集群部署方案。