Welcome to today’s guide on how to install Mattermost on Debian 10 (Buster) / Ubuntu 18.04 with PostgreSQL database server. Mattermost is a self-hosted and open-source online chat service designed to be used as an internal chat platform for companies and organizations. It is one of the main alternatives to Slack Chat platform.

We’ll use the PostgreSQL database and Nginx web server for this guide. All the steps are easy to follow, so just relax and follow along.

Step 1: Update System

Let’s start by ensuring our system is updated.

sudo apt update
sudo apt -y upgrade
sudo apt -y install wget curl vim

It is recommended to reboot your system after an upgrade.

sudo reboot

Set proper server hostname:

sudo hostnamectl set-hostname chat.computingforgeeks.com --static

Step 2: Install PostgreSQL Database Server

The next step is to install PostgreSQL Database Server:

sudo apt -y install postgresql postgresql-contrib

The PostgreSQL server will be started automatically after installation. Log in to the postgres account.

sudo --login --user postgres

Start the PostgreSQL interactive terminal and create the Mattermost database/user ‘mmuser’.

psql
CREATE DATABASE mattermost;
CREATE USER mmuser WITH PASSWORD 'StrongDBPassWord';
GRANT ALL PRIVILEGES ON DATABASE mattermost to mmuser;
\q

Log out of the postgres account.

exit

Step 3: Create mattermost System user and group

We need to set up a system user and group called mattermost that will run service.

sudo useradd --system --user-group mattermost

You can confirm it is a system user since its ID is less than 1000.

$ id mattermost
uid=998(mattermost) gid=998(mattermost) groups=998(mattermost)

Step 4: Download and Install Mattermost Server

With the database server configured, you can now download and install Mattermost. First, visit the Mattermost downloads page to get the latest release.

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

Extract downloaded archive:

tar xvf mattermost-5.13.2-linux-amd64.tar.gz

Move the extracted file to the /opt directory.

sudo mv mattermost /opt

Create the storage directory which will contain all the files and images that your users post to Mattermost.

sudo mkdir /opt/mattermost/data

Set correct ownership and permissions.

sudo chown -R mattermost:mattermost /opt/mattermost

Give write permissions to the mattermost group:

sudo chmod -R g+w /opt/mattermost

Step 5: Configure Mattermost Server

Set up the database driver in the file /opt/mattermost/config/config.json.

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

Configure PostgreSQL database settings to look like below under “SqlSettings“: section.

"DriverName": "postgres",
"DataSource": "postgres://mmuser:[email protected]:5432/mattermost?sslmode=disable&connect_timeout=10",

Step 6: Configure Systemd Service

Create Mattermost Systemd service unit.

$ sudo vim /etc/systemd/system/mattermost.service

Populate below data into the file.

[Unit]
Description=Mattermost
After=network.target
After=postgresql.service
Requires=postgresql.service

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

[Install]
WantedBy=multi-user.target

Make systemd load the new unit.

sudo systemctl daemon-reload

Start and enable mattermost service.

 sudo systemctl start mattermost.service
 sudo systemctl enable mattermost.service

Check service status to make sure it is running.

$ systemctl status mattermost.service
● mattermost.service - Mattermost
   Loaded: loaded (/etc/systemd/system/mattermost.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-08-12 12:15:06 CEST; 7s ago
 Main PID: 4116 (mattermost)
    Tasks: 11 (limit: 4585)
   Memory: 247.1M
   CGroup: /system.slice/mattermost.service
           └─4116 /opt/mattermost/bin/mattermost

Aug 12 12:15:06 chat mattermost[4116]: {"level":"info","ts":1565604906.0674481,"caller":"mlog/sugar.go:19","msg":"Scheduling next survey for Sep 2,
Aug 12 12:15:06 chat mattermost[4116]: {"level":"error","ts":1565604906.1013212,"caller":"plugin/hclog_adapter.go:60","msg":"reading plugin stderr"
Aug 12 12:15:06 chat mattermost[4116]: {"level":"error","ts":1565604906.1966574,"caller":"mlog/log.go:172","msg":"RPC call OnConfigurationChange to
Aug 12 12:15:06 chat mattermost[4116]: {"level":"info","ts":1565604906.1981738,"caller":"jobs/workers.go:68","msg":"Starting workers"}
Aug 12 12:15:06 chat mattermost[4116]: {"level":"info","ts":1565604906.1982753,"caller":"app/server.go:413","msg":"Starting Server..."}
Aug 12 12:15:06 chat mattermost[4116]: {"level":"info","ts":1565604906.199372,"caller":"jobs/schedulers.go:72","msg":"Starting schedulers."}
Aug 12 12:15:06 chat mattermost[4116]: {"level":"info","ts":1565604906.202546,"caller":"app/server.go:479","msg":"Server is listening on [::]:8065"
Aug 12 12:15:06 chat mattermost[4116]: {"level":"info","ts":1565604906.225944,"caller":"app/web_hub.go:75","msg":"Starting 4 websocket hubs"}
Aug 12 12:15:06 chat mattermost[4116]: {"level":"info","ts":1565604906.2512763,"caller":"commands/server.go:105","msg":"Sending systemd READY notif
Aug 12 12:15:06 chat systemd[1]: Started Mattermost.

Verify that Mattermost is running.

curl http://localhost:8065

You should see the HTML that’s returned by the Mattermost server.

Step 7: Finish Mattermost Web configuration

Open your browser and navigate to Mattermost instance on 8065. E.g 192.168.10.10:8085

Access System Console:

Then configure your server based on configuration settings documentation.

More settings:

Configure TLS by following the instructions

Configure Email by following the SMTP email setup guide

Don’t forget to restart mattermost service after making the changes.

sudo systemctl restart mattermost

Step 8: Configure Nginx proxy server

In a production setting, use a proxy server for greater security and performance of Mattermost.

# Install nginx

$ sudo apt -y install nginx

Create a new configuration file for Mattermost.

sudo nano /etc/nginx/conf.d/mattermost.conf

Add configuration settings.

upstream backend {
   server 192.168.10.10: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    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_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_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;
   }
}

Restart Nginx.

sudo rm /etc/nginx/sites-enabled/default
sudo systemctl restart nginx

Read Mattermost documentation to learn about more features you can unlock and integrations with third-party systems.

Related.

How To Install Rocket.Chat Server on Debian / Ubuntu

Install Zulip Chat Server on Ubuntu / Debian