Jul 6, 2024 3 min read

Mattermost: The Ultimate Self-Hosting Setup

Mattermost: The Ultimate Self-Hosting Setup
Table of Contents

Mattermost is an open-source, self-hosted team collaboration platform built for developers and enterprises seeking complete control over their data. Known for its robust customization options and self-hosting capabilities, Mattermost is an excellent alternative to proprietary solutions like Slack. This guide offers actionable steps to deploy, configure, and manage Mattermost effectively, covering installation, reverse proxy configuration, logging, backups, updates, and unique features.

Installing Mattermost

πŸ“¦ Docker/Docker Compose Setup

Deploying Mattermost with Docker Compose is straightforward and ideal for managing dependencies. Start by creating a docker-compose.yml file tailored to Mattermost:


version: "3.8"

services:

app:

image: mattermost/mattermost-team-edition:latest

container_name: mattermost-app

restart: unless-stopped

ports:

- "8065:8065"

environment:

- MM_USERNAME=admin

- MM_PASSWORD=yourpassword

- MM_DB_HOST=db:5432

volumes:

- ./mattermost/data:/mattermost/data

- ./mattermost/config:/mattermost/config

- ./mattermost/logs:/mattermost/logs

depends_on:

- db

db:

image: postgres:13

container_name: mattermost-db

restart: unless-stopped

environment:

POSTGRES_USER=mmuser

POSTGRES_PASSWORD=mmuser_password

POSTGRES_DB=mattermost

volumes:

- ./mattermost/db:/var/lib/postgresql/data

Deploy the stack using the following commands:


mkdir -p ~/mattermost && cd ~/mattermost

nano docker-compose.yml   # Paste the above content into the file

docker-compose up -d      # Start the Mattermost app and database

docker-compose ps         # Check the status of your containers

πŸš€ Manual Installation

For system administrators installing Mattermost on a Linux server, follow these steps:

  1. Install dependencies:

sudo apt update

sudo apt install -y wget tar postgresql postgresql-contrib

  1. Download and extract Mattermost:

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

tar -xvzf mattermost-X.X.X-linux-amd64.tar.gz

sudo mv mattermost /opt

sudo mkdir -p /opt/mattermost/data

sudo useradd --system --user-group mattermost

sudo chown -R mattermost:mattermost /opt/mattermost

  1. Configure the database:

sudo -u postgres createuser --interactive

sudo -u postgres createdb mattermost --owner=mmuser

  1. Start Mattermost:

sudo -u mattermost /opt/mattermost/bin/mattermost

Configuring Nginx as a Reverse Proxy

🌐 Nginx Configuration

Set up Nginx to route HTTP traffic to Mattermost running on port 8065. Add the following server block to your Nginx configuration:


server {

listen 80;

server_name mattermost.example.com;

location / {

proxy_pass http://localhost:8065/;

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;

}

}

Restart Nginx to apply changes:


sudo nginx -t   # Test configuration for syntax errors

sudo systemctl restart nginx

πŸ”’ SSL/TLS Setup

Secure your deployment with Let's Encrypt:


sudo apt install certbot python3-certbot-nginx

sudo certbot --nginx -d mattermost.example.com

Set up automatic renewal for SSL certificates:


sudo systemctl enable certbot.timer

sudo systemctl start certbot.timer

πŸ› οΈ Testing and Reloading Nginx

Verify that the SSL certificate and proxy are working:


curl -I https://mattermost.example.com

sudo nginx -t

sudo systemctl reload nginx

Logging and Debugging Mattermost

πŸ—ƒοΈ Enabling Debug Logs

Debug logs provide detailed insights into Mattermost's operations. Enable them by editing the configuration file:


nano /opt/mattermost/config/config.json

Set the following values:


{

"LogSettings": {

"ConsoleLevel": "DEBUG",

"FileLevel": "DEBUG"

}

}

πŸ“„ Viewing Logs

For Docker-based installations:


docker logs mattermost-app

For manual installations:


tail -f /opt/mattermost/logs/mattermost.log

πŸ› οΈ Troubleshooting Common Issues

For errors like "Site cannot be reached," check Nginx logs:


sudo tail -f /var/log/nginx/error.log

πŸ“€ Exporting Logs

Send logs to an ELK Stack with Filebeat:


sudo apt install filebeat

sudo filebeat modules enable nginx

sudo filebeat setup -e

sudo service filebeat start

Backup and Restore

πŸ—‚οΈ File-Based Backups

Backup the Mattermost directory:


tar -cvzf mattermost-backup.tar.gz /opt/mattermost

πŸ”„ Database Backups

Export the PostgreSQL database:


pg_dump -U mmuser mattermost > mattermost-db.sql

Restore the database:


psql -U mmuser mattermost < mattermost-db.sql

πŸ“… Automated Backup Scripts

Create a cron job for periodic backups:


crontab -e

Add the following line to schedule daily backups at midnight:


0 0 * * * tar -cvzf /backup/mattermost-$(date +\%F).tar.gz /opt/mattermost && pg_dump -U mmuser mattermost > /backup/mattermost-db-$(date +\%F).sql

Updating and Upgrading Mattermost

⬆️ Updating Docker Images

Pull the latest Docker image and redeploy:


docker-compose pull

docker-compose up -d

πŸ› οΈ Manual Updates

Download the latest binary:


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

tar -xvzf mattermost-X.X.X-linux-amd64.tar.gz

sudo systemctl restart mattermost

πŸ” Checking for Updates

Monitor official releases from the Mattermost GitHub page.

Leveraging Mattermost’s Unique Features

πŸ”§ Enabling APIs

Activate API access by editing config.json:


"ServiceSettings": {

"EnableAPIv4": true

}

Test the API with curl:


curl -i -X GET -H "Authorization: Bearer <token>" https://mattermost.example.com/api/v4/teams

🌟 Advanced Configurations

Enable custom integrations by configuring incoming webhooks:


"ServiceSettings": {

"EnableIncomingWebhooks": true

}

Deploy a webhook listener using Python:


from flask import Flask, request

app = Flask(__name__)

@app.route('/webhook', methods=['POST'])

def webhook():

data = request.json

print(data)

return "OK", 200

app.run(port=5000)

Wrapping Up

This guide provided detailed steps to install, configure, and manage a self-hosted Mattermost deployment. By following these instructions, you can unlock the full potential of Mattermost, ensuring a highly customizable and secure team collaboration platform. Start implementing these steps today to take full control of your collaboration infrastructure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Selfhosted Ninja.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.