
Mattermost Self-Hosted Team Chat on Linux with PostgreSQL
- AuthorLiam K.
- Date
- Time18 minutes
Mattermost is a self-hosted collaboration suite that replaces Slack for teams that need data control. The Team Edition covers channels, DMs, file sharing, and integrations. This guide installs Mattermost with PostgreSQL and HTTPS so you can run private ops chat on your own infrastructure.
Prerequisites
- Ubuntu 22.04+ with 2+ CPU and 4 GB RAM
- DNS for chat.example.com
- SMTP credentials for invites and notifications
Step 1: PostgreSQL Database
bash
sudo apt update
sudo apt install -y postgresql postgresql-contrib
sudo -u postgres psql <<'EOF'
CREATE DATABASE mattermost;
CREATE USER mmuser WITH PASSWORD 'change-me-strong';
GRANT ALL PRIVILEGES ON DATABASE mattermost TO mmuser;
\\c mattermost
GRANT ALL ON SCHEMA public TO mmuser;
\\q
EOFStep 2: Install Mattermost
bash
MM_VERSION=10.2.0
sudo useradd --system --user-group mattermost
cd /opt
sudo wget https://releases.mattermost.com/\${MM_VERSION}/mattermost-\${MM_VERSION}-linux-amd64.tar.gz
sudo tar -xzf mattermost-\${MM_VERSION}-linux-amd64.tar.gz
sudo mkdir -p /opt/mattermost/data
sudo chown -R mattermost:mattermost /opt/mattermostStep 3: Configure config.json
bash
sudo -u mattermost tee /opt/mattermost/config/config.json >/dev/null <<'EOF'
{
"ServiceSettings": {
"SiteURL": "https://chat.example.com",
"ListenAddress": "127.0.0.1:8065"
},
"SqlSettings": {
"DriverName": "postgres",
"DataSource": "postgres://mmuser:change-me-strong@127.0.0.1:5432/mattermost?sslmode=disable&connect_timeout=10"
},
"FileSettings": {
"Directory": "./data/"
}
}
EOFStep 4: systemd Service
bash
sudo tee /etc/systemd/system/mattermost.service >/dev/null <<'EOF'
[Unit]
Description=Mattermost
After=network.target postgresql.service
[Service]
Type=notify
ExecStart=/opt/mattermost/bin/mattermost
Restart=always
User=mattermost
Group=mattermost
WorkingDirectory=/opt/mattermost
LimitNOFILE=49152
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now mattermost
sudo systemctl status mattermost --no-pagerStep 5: Nginx TLS Proxy
bash
sudo tee /etc/nginx/sites-available/mattermost >/dev/null <<'EOF'
upstream backend {
server 127.0.0.1:8065;
keepalive 32;
}
server {
listen 80;
server_name chat.example.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_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_pass http://backend;
}
}
EOF
sudo ln -sf /etc/nginx/sites-available/mattermost /etc/nginx/sites-enabled/mattermost
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d chat.example.com --redirectStep 6: Backup Database and Files
bash
sudo -u postgres pg_dump mattermost | gzip > /var/backups/mattermost-db-$(date +%F).sql.gz
sudo tar -czf /var/backups/mattermost-data-$(date +%F).tar.gz -C /opt/mattermost data configProduction Checklist
- Configure SMTP before inviting the whole team.
- Enable MFA for System Admin accounts.
- Keep database and ./data backups paired.
- Monitor disk for uploaded files and attachment growth.
- Test WebSocket connectivity through the reverse proxy after TLS changes.
"Team chat is production infrastructure when incident response depends on it — back it up and watch it like any other critical service."



