15%

全场主机优惠15%

测试技能,享折扣

使用代码:

Skills
开始使用
01.11.2024
1 +1

如何在Ubuntu上安装Mattermost:完整分步指南

Mattermost 是一个强大的开源消息和团队协作平台,为组织提供对其通信基础设施的完全控制。作为 Slack 和 Microsoft Teams 的自托管替代方案,Mattermost 提供企业级安全性、完整的数据所有权和广泛的定制选项——所有这些都无需支付定期的 SaaS 订阅费用。

本综合指南将引导您完成在 Ubuntu 服务器上安装和配置 Mattermost 的每一步,包括 PostgreSQL 数据库设置、Nginx 反向代理配置和 SSL 证书集成。

目录

  1. 前置条件
  2. 更新您的系统
  3. 安装所需的依赖项
  4. 下载并安装 Mattermost
  5. 设置 PostgreSQL 数据库
  6. 配置 Mattermost
  7. 创建 Systemd 服务
  8. 将 Nginx 配置为反向代理
  9. 使用 SSL (HTTPS) 保护 Mattermost
  10. 最终验证

1. 前置条件

在开始之前,请确保满足以下要求:

  • 运行 Ubuntu 20.04 或 Ubuntu 22.04 LTS 的服务器(Ubuntu 18.04 已停止支持,不建议用于生产环境)
  • 服务器上的 Sudo 或 root 权限
  • 一个指向您服务器 IP 地址的域名(强烈建议用于生产部署)
  • 最低硬件配置:1 vCPU、2 GB RAM、10 GB 磁盘空间(适用于小型团队);对于更大的部署,请相应增加配置

> 托管提示:为了顺利部署 Mattermost,请考虑使用 AlexHost 的可靠 VPS 托管计划。AlexHost VPS 实例具有 SSD 存储、完整的 root 访问权限和有保证的正常运行时间,非常适合自托管协作工具。

2. 更新您的系统

始终从刷新软件包索引和应用所有待处理的安全和系统更新开始:

sudo apt update
sudo apt upgrade -y

如果应用了内核更新,请重启服务器:

sudo reboot

3. 安装所需的依赖项

Mattermost 依赖于三个关键组件:PostgreSQL(数据库)、Nginx(反向代理)和 Certbot(SSL 证书管理)。

一步安装所有所需的软件包:

sudo apt install -y postgresql postgresql-contrib
sudo apt install -y nginx
sudo apt install -y certbot python3-certbot-nginx

验证 PostgreSQL 和 Nginx 正在运行:

sudo systemctl status postgresql
sudo systemctl status nginx

两个服务都应显示 active (running)

4. 下载并安装 Mattermost

步骤 1:下载最新的 Mattermost 版本

访问官方 Mattermost 发布页面以找到最新的稳定版本。使用 wget 将其直接下载到您的服务器:

wget https://releases.mattermost.com/9.5.0/mattermost-team-9.5.0-linux-amd64.tar.gz

> 注意:9.5.0 替换为最新可用的版本号。始终使用最新的稳定版本以获取安全补丁和新功能。

步骤 2:提取存档

tar -xvzf mattermost-team-9.5.0-linux-amd64.tar.gz

步骤 3:将 Mattermost 移动到系统目录

将提取的文件夹移动到 /opt,这是 Linux 中可选第三方软件的标准位置:

sudo mv mattermost /opt/mattermost

步骤 4:创建专用的 Mattermost 系统用户

以专用的非特权用户身份运行 Mattermost 是一个关键的安全最佳实践:

sudo useradd -r -m -d /opt/mattermost -s /bin/false mattermost

步骤 5:创建数据目录并设置权限

sudo mkdir -p /opt/mattermost/data
sudo chown -R mattermost:mattermost /opt/mattermost
sudo chmod -R 750 /opt/mattermost

5. 设置 PostgreSQL 数据库

Mattermost 需要一个专用的 PostgreSQL 数据库和用户。请仔细按照以下步骤操作。

步骤 1:切换到 PostgreSQL 系统用户

sudo -i -u postgres

步骤 2:打开 PostgreSQL 交互式 Shell

psql

步骤 3:创建数据库、用户并授予权限

逐个执行以下 SQL 命令:

CREATE DATABASE mattermost_db;
CREATE USER mattermost WITH PASSWORD 'YourStrongPassword123!';
GRANT ALL PRIVILEGES ON DATABASE mattermost_db TO mattermost;

> 安全注意:YourStrongPassword123! 替换为强大的唯一密码。使用密码管理器来生成和安全地存储它。

对于 PostgreSQL 15 及更高版本,您还需要授予模式权限:

c mattermost_db
GRANT ALL ON SCHEMA public TO mattermost;

步骤 4:退出 PostgreSQL Shell 并返回到您的用户

q
exit

6. 配置 Mattermost

步骤 1:打开 Mattermost 配置文件

sudo nano /opt/mattermost/config/config.json

步骤 2:更新数据库连接设置

找到 SqlSettings 块并使用您的 PostgreSQL 凭据更新它:

"SqlSettings": {
    "DriverName": "postgres",
    "DataSource": "postgres://mattermost:YourStrongPassword123!@localhost:5432/mattermost_db?sslmode=disable&connect_timeout=10",
    "DataSourceReplicas": [],
    "DataSourceSearchReplicas": [],
    "MaxIdleConns": 20,
    "ConnMaxLifetimeMilliseconds": 3600000,
    "MaxOpenConns": 300,
    "Trace": false,
    "AtRestEncryptKey": "",
    "QueryTimeout": 30
}

步骤 3:配置站点 URL

找到 ServiceSettings 块并设置您的域:

"ServiceSettings": {
    "SiteURL": "https://your_domain.com",
    ...
}

your_domain.com 替换为您的实际域名。

步骤 4:保存并退出

Ctrl+X,然后 Y,然后 Enter 以保存并关闭文件。

7. 创建 Systemd 服务

将 Mattermost 作为 systemd 服务运行可确保它在启动时自动启动并在失败时重启——这对任何生产部署都至关重要。

步骤 1:创建服务文件

sudo nano /etc/systemd/system/mattermost.service

步骤 2:添加服务配置

粘贴以下内容:

[Unit]
Description=Mattermost Team Messaging Server
After=network.target postgresql.service
Wants=postgresql.service

[Service]
Type=notify
ExecStart=/opt/mattermost/bin/mattermost
TimeoutStartSec=3600
KillMode=mixed
Restart=always
RestartSec=10
WorkingDirectory=/opt/mattermost
User=mattermost
Group=mattermost
LimitNOFILE=49152

[Install]
WantedBy=multi-user.target

步骤 3:启用并启动 Mattermost 服务

sudo systemctl daemon-reload
sudo systemctl enable mattermost
sudo systemctl start mattermost

步骤 4:验证服务正在运行

sudo systemctl status mattermost

您应该看到 active (running)。Mattermost 默认在端口 8065 上侦听。

8. 将 Nginx 配置为反向代理

Nginx 充当反向代理,将来自端口 80/443 的传入 HTTP/HTTPS 流量转发到 Mattermost 的内部端口 8065。这也启用了 WebSocket 支持,Mattermost 需要此支持来实现实时消息传递。

步骤 1:创建 Nginx 配置文件

sudo nano /etc/nginx/sites-available/mattermost

步骤 2:添加反向代理配置

upstream backend {
    server localhost:8065;
    keepalive 32;
}

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=mattermost_cache:10m max_size=3g inactive=120m use_temp_path=off;

server {
    listen 80;
    server_name your_domain.com;

    location ~ /api/v[0-9]+/(users/)?websocket$ {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        client_max_body_size 50M;
        proxy_set_header Host $http_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-Frame-Options SAMEORIGIN;
        proxy_buffers 256 16k;
        proxy_buffer_size 16k;
        proxy_read_timeout 600s;
        proxy_pass http://backend;
    }

    location / {
        client_max_body_size 50M;
        proxy_set_header Connection "";
        proxy_set_header Host $http_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-Frame-Options SAMEORIGIN;
        proxy_buffers 256 16k;
        proxy_buffer_size 16k;
        proxy_read_timeout 600s;
        proxy_cache mattermost_cache;
        proxy_cache_revalidate on;
        proxy_cache_min_uses 2;
        proxy_cache_use_stale timeout;
        proxy_cache_lock on;
        proxy_pass http://backend;
    }
}

your_domain.com 替换为您的实际域名。

步骤 3:启用站点配置

sudo ln -s /etc/nginx/sites-available/mattermost /etc/nginx/sites-enabled/

步骤 4:测试 Nginx 配置的语法错误

sudo nginx -t

预期输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

步骤 5:重启 Nginx

sudo systemctl restart nginx

9. 使用 SSL (HTTPS) 保护 Mattermost

在任何生产环境中,通过 HTTPS 运行 Mattermost 是强制性的。它加密用户和您的服务器之间的所有通信,保护凭据和消息免受拦截。

> SSL 变得简单:AlexHost 为所有类型的网站和应用程序提供价格合理的 SSL 证书。或者,使用下面的免费 Let’s Encrypt 方法。

步骤 1:使用 Certbot 获取免费 SSL 证书

sudo certbot --nginx -d your_domain.com

Certbot 将自动:

  • 验证域所有权
  • 颁发 Let’s Encrypt 证书
  • 修改您的 Nginx 配置以启用 HTTPS
  • 设置自动证书续期

步骤 2:验证自动续期

sudo certbot renew --dry-run

步骤 3:重启 Nginx

sudo systemctl restart nginx

您的 Mattermost 实例现在可在 https://your_domain.com 访问。

10. 最终验证

检查所有服务是否正在运行

sudo systemctl status postgresql
sudo systemctl status mattermost
sudo systemctl status nginx

直接在端口 8065 上测试 Mattermost

curl -I http://localhost:8065

您应该收到 HTTP 200 OK 或重定向响应。

完成基于 Web 的设置

  1. 打开您的浏览器并导航到 https://your_domain.com
  2. 您将看到 Mattermost 设置向导
  3. 创建您的管理员账户
  4. 配置您的第一个团队和工作区
  5. 邀请团队成员并开始协作

故障排除常见问题

问题可能的原因解决方案
Mattermost 服务启动失败config.json 中的数据库凭据不正确仔细检查 SqlSettings 中的 DataSource 字符串
15%

全场主机优惠15%

测试技能,享折扣

使用代码:

Skills
开始使用