本教程适用于以下操作系统版本
- Ubuntu 24.04 (Noble Numbat)
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
本页内容
- 先决条件
- 第 1 步 - 配置防火墙
- 第 2 步 - 安装 PostgreSQL
- 第 3 步 - 配置 PostgreSQL
- 第 4 步 - 下载 Mattermost
- 第 5 步 - 为 Mattermost 创建系统用户并配置权限
- 第 6 步 - 创建 Systemd 单元文件
- 第 7 步 - 安装 Nginx
- 第 8 步 - 安装 SSL
- 第 9 步 - 配置 Nginx
- 第 10 步 - 访问 Mattermost 服务器
- 第 11 步 - 配置 Mattermost 服务器 配置邮件通知
- 结论
- 配置邮件通知
Mattermost 是一个开源消息平台,可用于聊天、文件共享、项目管理和工作流编排。它使用 Go 语言编写,既提供云托管方案,也提供自托管服务器。它是 Slack 及其他专业平台的替代方案。将其托管在自己的服务器上,让你能够掌控自己的通信内容和敏感数据。
在本教程中,你将学习如何在 Ubuntu 22.04 服务器上安装 Mattermost 团队消息系统。
先决条件
- 一台运行 Ubuntu 22.04 的服务器,至少 2 GB 内存,最多支持 1000 名用户。
- 一个具有 sudo 权限的非 root 用户。
- 已启用并运行 Uncomplicated Firewall(UFW) 防火墙。
- 一个指向该服务器的完全限定域名。本教程将使用域名 mattermost.example.com。
- 系统已全部更新。$ sudo apt update && sudo apt upgrade
一台运行 Ubuntu 22.04 的服务器,至少 2 GB 内存,最多支持 1000 名用户。
一个具有 sudo 权限的非 root 用户。
已启用并运行 Uncomplicated Firewall(UFW) 防火墙。
一个指向该服务器的完全限定域名。本教程将使用域名 mattermost.example.com。
系统已全部更新。
$ sudo apt update && sudo apt upgrade
第 1 步 - 配置防火墙
安装任何软件包之前,第一步是配置防火墙,允许 HTTP 和 HTTPS 连接。
检查防火墙状态。
$ sudo ufw status
你应该会看到类似下面的输出。
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Mattermost 需要使用 8065 端口。该端口只需临时开放以确认安装是否成功,稍后我们会将其关闭。
$ sudo ufw allow 8065
放行 HTTP 和 HTTPS 端口。
$ sudo ufw allow http
$ sudo ufw allow https
再次检查状态以确认。
$ sudo ufw status
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
80/tcp ALLOW Anywhere
443 ALLOW Anywhere
8065 ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
8065 (v6) ALLOW Anywhere (v6)
第 2 步 - 安装 PostgreSQL
Mattermost 可以搭配 MySQL 和 PostgreSQL 服务器使用,但推荐使用 PostgreSQL。
Ubuntu 22.04 自带最新的稳定版(v14) PostgreSQL。你可以用一条命令完成安装。
$ sudo apt install postgresql postgresql-contrib
检查 MySQL 的版本。
$ psql --version
psql (PostgreSQL) 14.4 (Ubuntu 14.4-0ubuntu0.22.04.1)
第 3 步 - 配置 PostgreSQL
安装过程中,PostgreSQL 会创建一个名为 postgres 的 Linux 用户账户。可通过该账户访问 PostgreSQL shell。
登录到 PostgreSQL shell。
$ sudo -u postgres psql
创建 Mattermost 数据库。
postgres=# CREATE DATABASE mattermostdb;
创建 Mattermost 数据库用户。请将 mmuser-password 替换为你自己设定的更强密码。
postgres=# CREATE USER mmuser WITH PASSWORD 'mmuser-password';
将该数据库的所有权限授予该用户。
postgres=# GRANT ALL PRIVILEGES ON DATABASE mattermost to mmuser;
在 shell 中输入 \q 退出 PostgreSQL shell。
postgres=# \q
打开文件 /etc/postgresql/{version}/main/pg_hba.conf 进行编辑。
$ sudo nano /etc/postgresql/{version}/main/pg_hba.conf
找到以下几行。
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
将上述几行中的 peer 和 scram-sha-256 值改为 trust。
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
按 Ctrl + X,并在提示时输入 Y 保存文件。
重启 PostgreSQL 服务以使更改生效。
$ sudo systemctl restart postgresql
验证是否能够以 Mattermost 的 SQL 用户身份连接。
$ psql --dbname=mattermost --username=mmuser --password
系统将提示输入密码。输入后即可登录到 PostgreSQL shell。输入 \q 退出 shell。
Password:
psql (14.4 (Ubuntu 14.4-0ubuntu0.22.04.1))
Type "help" for help.
mattermost-> \q
第 4 步 - 下载 Mattermost
下载 Mattermost 服务器的最新版本。在撰写本教程时,最新的可用版本为 7.0.1。
$ wget https://releases.mattermost.com/7.0.1/mattermost-7.0.1-linux-amd64.tar.gz
解压压缩包。
$ tar -xvzf mattermost*.gz
将解压后的文件移动到 /opt 目录。
$ sudo mv mattermost /opt
为 Mattermost 服务器创建数据存储目录。
$ sudo mkdir /opt/mattermost/data
第 5 步 - 为 Mattermost 创建系统用户并配置权限
打开配置文件 /opt/mattermost/config/config.json 进行编辑。
$ sudo nano /opt/mattermost/config/config.json
将变量 SiteURL 设置为你想用于本次安装的域名。
"SiteURL": "https://mattermost.example.com",
在 SqlSettings 下找到变量 DriverName,并将其值改为 mysql。
"DriverName": "mysql",
将变量 DataSource 设置为以下值。把 mmuser 替换为 SQL 用户名,把 YourPassword23! 替换为 SQL 密码,把 mattermostdb 替换为你在第 4 步中配置的数据库名。
"DataSource": "mmuser:YourPassword23!@tcp(localhost:3306)/mattermostdb?charset=utf8mb4,utf8&writeTimeout=30s",
此时还有许多其他设置可以配置,但安装完成后再进行会更方便。目前只有这两项设置是必需的。
按 Ctrl + X,并在提示时输入 Y 保存文件。
为 Mattermost 服务器创建系统用户和用户组。
$ sudo useradd --system --user-group mattermost
将 Mattermost 目录的所有权改为新建的用户和用户组。
$ sudo chown -R mattermost:mattermost /opt/mattermost
为 mattermost 用户组赋予该目录的写权限。
$ sudo chmod -R g+w /opt/mattermost
切换到 Mattermost 目录。
$ cd /opt/mattermost
以 mattermost 用户身份启动 Mattermost 服务器。
$ sudo -u mattermost ./bin/mattermost
服务器将启动并输出大量日志信息。等待出现 Server is listening on [::]:8065 这一行。在浏览器中访问 http://

按 Ctrl + C 停止服务器。稍后我们会回到这里完成安装配置。
第 6 步 - 创建 Systemd 单元文件
下一步是为 Mattermost 创建系统服务文件。
创建并打开该单元文件进行编辑。
$ sudo nano /lib/systemd/system/mattermost.service
在其中粘贴以下代码。
[Unit]
Description=Mattermost
After=network.target
After=postgresql.service
BindsTo=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
按 Ctrl + X,并在提示时输入 Y 保存文件。
重新加载 systemd 守护进程以载入服务文件。
$ sudo systemctl daemon-reload
启动 Mattermost 服务。
$ sudo systemctl start mattermost
检查服务状态。
? mattermost.service - Mattermost
Loaded: loaded (/lib/systemd/system/mattermost.service; disabled; vendor preset: enabled)
Active: active (running) since Mon 2022-07-11 01:24:46 UTC; 20s ago
Main PID: 23628 (mattermost)
Tasks: 48 (limit: 2241)
Memory: 448.2M
CPU: 14.929s
CGroup: /system.slice/mattermost.service
??23628 /opt/mattermost/bin/mattermost
??23651 plugins/com.mattermost.plugin-channel-export/server/dist/plugin-linux-amd64
??23656 plugins/com.mattermost.nps/server/dist/plugin-linux-amd64
??23662 plugins/com.mattermost.calls/server/dist/plugin-linux-amd64
??23668 plugins/com.mattermost.apps/server/dist/plugin-linux-amd64
??23674 plugins/playbooks/server/dist/plugin-linux-amd64
??23683 plugins/focalboard/server/dist/plugin-linux-amd64
....
启用该服务。
$ sudo systemctl enable mattermost
第 7 步 - 安装 Nginx
Ubuntu 22.04 自带的 Nginx 版本较旧。要安装最新版本,需要引入 Nginx 官方仓库。
导入 Nginx 的签名密钥。
$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
添加 Nginx 稳定版仓库。
$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg arch=amd64] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
更新系统软件源。
$ sudo apt update
安装 Nginx。
$ sudo apt install nginx
验证安装。
$ nginx -v
nginx version: nginx/1.22.0
第 8 步 - 安装 SSL
我们需要安装 Certbot 来生成 SSL 证书。你既可以使用 Ubuntu 的仓库安装 Certbot,也可以通过 Snapd 工具获取最新版本。本教程将使用 Snapd 版本。
Ubuntu 22.04 默认已安装 Snapd。运行以下命令以确保你的 Snapd 是最新版本。
$ sudo snap install core
安装 Certbot。
$ sudo snap install --classic certbot
使用以下命令在 /usr/bin 目录中创建符号链接,确保可以运行 certbot 命令。
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
运行以下命令生成 SSL 证书。
$ sudo certbot certonly --standalone --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m
[email protected]
-d mattermost.example.com
上述命令会将证书下载到服务器上的 /etc/letsencrypt/live/mattermost.example.com 目录。
生成 Diffie-Hellman 组证书。
$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
打开文件 /etc/letsencrypt/renewal/mattermost.example.com.conf 进行编辑。
$ sudo nano /etc/letsencrypt/renewal/mattermost.example.com.conf
在底部粘贴以下代码。
pre_hook = systemctl stop nginx
post_hook = systemctl start nginx
按 Ctrl + X,并在提示时输入 Y 保存文件。
我们使用 Certbot 的 standalone 选项生成了 SSL 证书。该模式会启动自己的 Web 服务器来创建证书,这意味着续期时需要关闭 Nginx。pre_hook 和 post_hook 命令分别在续期前后运行,用于自动关闭和重启 Nginx 服务器,因此无需人工干预。
要检查 SSL 续期是否正常工作,可以对该流程进行一次演练(dry run)。
$ sudo certbot renew --dry-run
如果没有出现错误,就说明一切就绪,证书将自动续期。
第 9 步 - 配置 Nginx
打开文件 /etc/nginx/nginx.conf 进行编辑。
$ sudo nano /etc/nginx/nginx.conf
在 include /etc/nginx/conf.d/*.conf; 这一行之前添加以下内容。
server_names_hash_bucket_size 64;
按 Ctrl + X,并在提示时输入 Y 保存文件。
创建并打开文件 /etc/nginx/conf.d/monica.conf 进行编辑。
$ sudo nano /etc/nginx/conf.d/mattermost.conf
在其中粘贴以下代码。将 mattermost.example.com 替换为你的域名。确保 client_max_body_size 的值设置为 10MB,这与 Monica 中文件的默认上传大小一致,也与我们此前在 PHP 中配置的值相同。
upstream backend {
server 127.0.0.1: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 default_server;
server_name mattermost.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name mattermost.example.com;
http2_push_preload on; # Enable HTTP/2 Server Push
ssl_certificate /etc/letsencrypt/live/mattermost.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mattermost.example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/mattermost.example.com/chain.pem;
ssl_session_timeout 1d;
# Enable TLS versions (TLSv1.3 is required upcoming HTTP/3 QUIC).
ssl_protocols TLSv1.2 TLSv1.3;
# Enable TLSv1.3's 0-RTT. Use $ssl_early_data when reverse proxying to
# prevent replay attacks.
#
# @see: https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_early_data
ssl_early_data on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:50m;
# HSTS (ngx_http_headers_module is required) (15768000 seconds = six months)
add_header Strict-Transport-Security max-age=15768000;
# OCSP Stapling ---
# fetch OCSP records from URL in ssl_certificate and cache them
ssl_stapling on;
ssl_stapling_verify on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
add_header X-Early-Data $tls1_3_early_data;
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;
client_body_timeout 60;
send_timeout 300;
lingering_timeout 5;
proxy_connect_timeout 90;
proxy_send_timeout 300;
proxy_read_timeout 90s;
proxy_http_version 1.1;
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_http_version 1.1;
proxy_pass http://backend;
}
}
# This block is useful for debugging TLS v1.3. Please feel free to remove this
# and use the `$ssl_early_data` variable exposed by NGINX directly should you
# wish to do so.
map $ssl_early_data $tls1_3_early_data {
"~." $ssl_early_data;
default "";
}
按 Ctrl + X,并在提示时输入 Y 保存文件。
赋予 Nginx 对缓存目录的权限。
$ sudo chown -R nginx:nginx /var/cache/nginx
验证 Nginx 配置。
$ sudo nginx -t
重启 Nginx 服务器。
$ sudo systemctl restart nginx
第 10 步 - 访问 Mattermost 服务器
在浏览器中打开 https://mattermost.example.com,将看到第 5 步中所示的注册页面。填写你的账户信息,该账户将被设为系统管理员。
在继续之前,我们需要关闭 8065 端口,因为已经配置通过公网 URL 访问 Mattermost,因此该开放端口存在安全风险。
$ sudo ufw delete allow 8065
接下来将进入团队创建页面。

点击 Create a team 按钮创建你的第一个团队。

接下来,系统会要求你为该团队设置一个公开 URL。

点击 Finish 按钮打开 Mattermost 仪表板。

第 11 步 - 配置 Mattermost 服务器
你可以通过 config.json 文件配置 Mattermost,也可以通过仪表板中的 System console 进行配置。我们将通过仪表板来操作,因为这种方式简单得多。点击左上角的 Product 按钮,然后选择 System console 选项。

随后将进入 System console 仪表板,在这里可以配置服务器的各项设置。
配置邮件通知
消息系统最重要的功能之一就是邮件通知。
第一步是启用通知。进入 System Console >> Site Configuration >> Noficiations 菜单,设置以下选项。
- 将 Enable Email Notifications 设置为 true
- 将 Notification Display Name 设置为 No-Reply
- 将 Notification From Address 设置为类似 [email protected] 的值
- 将 Support Email Address 设置为类似 [email protected] 的值

下一步是启用 SMTP。进入 System Console >> Environment >> SMTP 菜单,设置以下选项。本教程使用 Amazon SES 邮件服务。
- 将 SMTP Server 设置为 {SMTP-server}
- 将 SMTP Server Port 设置为 465
- 将 Enable SMTP Authentication 设置为 true
- 将 SMTP Server Username 设置为 {SES-username}
- 将 SMTP Server Password 设置为 {SES-Key}
- 将 Connection Security 设置为 TLS 或 STARTTLS,具体取决于 SMTP 服务器支持哪种
点击 Test Connection 按钮以确认 SMTP 设置。

还有更多设置可以配置。完成后,需要在终端中重启服务器才能使更改生效。
$ sudo systemctl restart mattermost
结论
关于在 Ubuntu 22.04 服务器上安装和配置 Mattermost 团队消息系统的教程到此结束。如有任何疑问,欢迎留言交流。