Mattermost是一个开源的自托管消息系统,适用于企业组织。它是Slack和Microsoft Teams等服务的替代方案。Mattermost集成了多种功能,包括文件共享、搜索、自动化以及众多第三方集成。它是一个面向内部组织和公司的协作平台与消息系统。
本教程将指导你在Rocky Linux 9服务器上安装Mattermost。你将使用PostgreSQL作为数据库,Nginx作为反向代理来配置Mattermost,然后在Mattermost中创建第一个工作区。
前提条件
在开始之前,请确保满足以下要求:
- 一台Rocky Linux 9服务器
- 一个具有管理员权限的非root用户
- 一个已指向服务器IP地址的域名
- SELinux状态为permissive
- 已启用firewalld防火墙
安装PostgreSQL和Nginx
在本节中,你将安装Mattermost所需的依赖项,包括PostgreSQL服务器、Nginx Web服务器和Certbot。你还需要添加EPEL仓库并启用PostgreSQL 16仓库模块。
首先,运行以下dnf命令将EPEL仓库添加到你的系统中。
sudo dnf install epel-release -y

然后使用以下命令启用PostgreSQL 16仓库模块。
sudo dnf module enable postgresql:16

现在安装PostgreSQL、Nginx Web服务器和Certbot等Mattermost的依赖项。输入Y确认安装。
sudo dnf install postgresql-server postgresql-contrib nginx certbot wget curl

对于PostgreSQL服务器,执行以下命令来初始化PostgreSQL数据目录以使其正常工作。
sudo /usr/bin/postgresql-setup --initdb
接下来,运行以下systemctl命令启动并启用postgresql服务。然后验证PostgreSQL是否正在运行。
sudo systemctl enable --now postgresql
sudo systemctl status postgresql

最后,使用以下命令启动并启用Nginx Web服务器。然后检查Nginx服务状态。
sudo systemctl enable --now nginx
sudo systemctl status nginx

开放HTTP和HTTPS端口
安装完依赖项后,你需要在系统上开放HTTP和HTTPS端口。在Rocky Linux上,你将使用通过firewall-cmd工具管理的firewalld防火墙。
使用以下firewall-cmd命令在firewalld上开放HTTP和HTTPS服务。添加成功后,你将看到"success"输出。
sudo firewall-cmd --add-service={http,https} --permanent
现在使用以下命令重新加载firewalld以应用新规则。
sudo firewall-cmd --reload
最后,使用以下命令检查firewalld中的规则。你将看到HTTP和HTTPS服务已启用。
sudo firewall-cmd --list-all

创建PostgreSQL用户和数据库
在本节中,你将把PostgreSQL身份验证方法设置为scram-sha-256,然后创建Mattermost将使用的新数据库和用户。
要更改默认密码身份验证方法,使用nano编辑器打开PostgreSQL配置文件/var/lib/pgsql/data/pg_hba.conf。
sudo nano /var/lib/pgsql/data/pg_hba.conf
将localhost连接的默认身份验证方法更改为scram-sha-256,如下所示:
# TYPE DATABASE USER ADDRESS METHOD # "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
保存文件并退出编辑器。
运行以下systemctl命令重启PostgreSQL以应用更改。
sudo systemctl restart postgresql
配置好PostgreSQL后,你将创建Mattermost的新数据库和用户。
使用以下psql命令登录PostgreSQL服务器。
sudo -u postgres psql
运行以下查询创建新数据库mattermost、新用户mmuser及其密码。
CREATE DATABASE mattermost; CREATE USER mmuser WITH PASSWORD 'password'; GRANT ALL ON DATABASE mattermost TO mmuser; ALTER DATABASE mattermost OWNER TO mmuser; GRANT USAGE, CREATE ON SCHEMA PUBLIC TO mmuser;

使用以下命令检查PostgreSQL中的数据库和用户列表,你将看到已创建的数据库mattermost和用户mmuser。
\l \du
输入quit退出PostgreSQL服务器。

接下来,运行以下psql命令使用mmuser登录到mattermost数据库。提示时输入密码。
sudo -u postgres psql --host=localhost --dbname=mattermost --username=mmuser --password
如果成功,使用以下查询检查连接状态。你将看到已使用mmuser连接到mattermost数据库。
\conninfo
最后,输入quit退出。

下载Mattermost
现在已经配置好PostgreSQL,你将创建一个新用户,下载Mattermost源代码,然后配置Mattermost安装目录。
在下载Mattermost之前,运行以下命令在系统上添加一个新用户mattermost。
sudo useradd --system --user-group mattermost
使用wget命令下载Mattermost源代码,并使用tar命令解压。Mattermost源代码将被解压到mattermost目录。
wget https://releases.mattermost.com/10.0.1/mattermost-10.0.1-linux-amd64.tar.gz
tar xf mattermost-10.0.1-linux-amd64.tar.gz
将mattermost目录移动到/opt目录,并在其下创建一个新的data目录。Mattermost将安装在/opt/mattermost目录中。
mv mattermost /opt/
mkdir -p /opt/mattermost/data
最后,运行以下命令将/opt/mattermost目录的所有权更改为mattermost用户,并确保mattermost组具有读写权限。
sudo chown -R mattermost:mattermost /opt/mattermost
sudo chmod g+rw /opt/mattermost

配置Mattermost连接PostgreSQL
在本节中,你将配置Mattermost连接PostgreSQL。你将编辑Mattermost配置文件/opt/mattermost/config/config.json,为Mattermost设置域名,并将PostgreSQL数据库添加到Mattermost。
使用nano编辑器打开默认的Mattermost配置文件。
sudo nano /opt/mattermost/config/config.json
将默认的SiteURL更改为你的Mattermost域名。
"ServiceSettings": {
"SiteURL": "https://mattermost.howtoforge.local",
移动到SqlSettings部分并更改数据库配置。确保调整数据库名称、用户和密码。
"SqlSettings": {
"DriverName": "postgres",
"DataSource": "postgres://mmuser:password@localhost/mattermost?sslmode=disable&connect_timeout=10&binary_parameters=yes",
完成后,保存文件并退出编辑器。
将Mattermost配置为systemd服务
现在你已经将Mattermost与PostgreSQL集成,接下来将创建Mattermost的systemd服务文件。这允许你在后台运行Mattermost,并通过systemctl命令行轻松管理。
使用nano编辑器创建新的systemd服务文件/etc/systemd/system/mattermost.service。
sudo nano /etc/systemd/system/mattermost.service
插入以下配置以将Mattermost作为systemd服务运行。
[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
保存并退出文件。
运行以下systemctl命令重新加载systemd管理器并应用新的mattermost服务。
sudo systemctl daemon-reload
systemd重新加载后,运行以下systemctl命令启动并启用mattermost服务。然后验证服务是否正在运行。
sudo systemctl enable --now mattermost
sudo systemctl status mattermost
如果一切正常,你将看到mattermost服务已运行并启用。

配置Nginx反向代理
此时,Mattermost已在你的Rocky Linux服务器上运行。要使其可通过外部访问,你将设置Nginx作为反向代理并启用安全的HTTPS。
在配置Nginx Web服务器之前,运行以下命令停止Nginx服务并从Let's Encrypt生成SSL证书。确保将域名和电子邮件地址更改为你自己的信息。
sudo systemctl stop nginx
sudo certbot --certonly --standalone --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m user@example.com -d mattermost.example.com
过程完成后,你的SSL证书将位于/etc/letsencrypt/live/domain.com目录中。
接下来,使用nano编辑器创建新的Nginx配置文件/etc/nginx/conf.d/mattermost.conf。
sudo nano /etc/nginx/conf.d/mattermost.conf
插入以下配置,确保将域名和SSL证书路径更改为你自己的信息。
upstream backend {
server 127.0.0.1:8065;
keepalive 32;
}
server {
listen 80;
server_name mattermost.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name mattermost.example.com;
http2_push_preload on;
ssl_certificate /etc/letsencrypt/live/mattermost.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mattermost.example.com/privkey.pem;
ssl_session_timeout 1d;
ssl_protocols TLSv1.2 TLSv1.3;
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;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
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 $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 60s;
send_timeout 300s;
lingering_timeout 5s;
proxy_connect_timeout 90s;
proxy_send_timeout 300s;
proxy_read_timeout 90s;
proxy_http_version 1.1;
proxy_pass http://backend;
}
location / {
client_max_body_size 100M;
proxy_set_header Connection "";
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-Frame-Options SAMEORIGIN;
proxy_buffers 256 16k;
proxy_buffer_size 16k;
proxy_read_timeout 600s;
proxy_http_version 1.1;
proxy_pass http://backend;
}
}
map $ssl_early_data $tls1_3_early_data {
"~." $ssl_early_data;
default "";
}
完成后保存文件并退出编辑器。
运行以下nginx命令验证Nginx配置语法。如果语法正确,你将看到"syntax is ok - test is successful"的输出。
sudo nginx -t
最后,运行以下systemctl命令重启Nginx服务以应用更改。
sudo systemctl restart nginx

创建第一个Mattermost工作区
打开你的Web浏览器并访问Mattermost安装地址,例如https://mattermost.example.com。如果安装成功,你将看到安装向导。
点击"View in browser"按钮通过Web浏览器设置Mattermost。

输入新的管理员用户、电子邮件地址和密码,然后点击"Create account"继续。

输入你的组织名称。

现在可以跳过与第三方应用程序的集成。

点击"Finish setup"完成Mattermost安装。

完成后,你将被重定向到你的第一个Mattermost工作区。

总结
恭喜!你已完成在Rocky Linux 9服务器上安装Mattermost。你已将Mattermost作为systemd服务在后台运行,使用PostgreSQL作为数据库,Nginx作为反向代理。你还使用HTTPS保护了Mattermost,并在Mattermost中创建了第一个工作区。从这里开始,你可以将Mattermost与GitHub、GitLab和Bitbucket等第三方应用程序集成。