Install Mattermost with PostgreSQL on Ubuntu 16.04

Mattermost

Introduction

Mattermost is a workplace messaging system for web, PCs and phones. It’s an open source alternative to Slack.
A complete Mattermost installation consists of three components: the Mattermost server, a proxy server, and a database server. Each component can be installed on the same machine, or on three different machines on the local network.
This tutorial explains how to install Mattermost Team Edition on an Ubuntu 16.04 with Nginx as your proxy server and PostgreSQL as your database.

Install PostgreSQL

On the server that will host the database, execute the following command:

# apt install postgresql postgresql-contrib

During the installation process a new user, “postgres”, will be created. So, at the end, log in with the command:

$ sudo --login --user postgres

Start the PostgreSQL shell:

$ psql

Create a new database:

postgres=# CREATE DATABASE mattermost_db;
postgres=# CREATE USER mmuser WITH PASSWORD 'my_strong_password';
postgres=# GRANT ALL PRIVILEGES ON DATABASE mattermost_db to mmuser;
postgres=# \q

Log out of this account, and allow PostgresSQL to listen on all assigned IPs, editing the file

/etc/postgresql/9.3/main/postgresql.conf

There, find line

#listen_addresses = 'localhost'

uncomment it and change ‘localhost’ to ‘*’; so, after editing:

listen_addresses = *

Reload the database with:

# systemctl reload postgresql

Now that the database is installed, it’s time to install the Mattermost Server.

Install Mattermost Server

Download the latest version of the Mattermost Server with wget. Right now, this is 3.6.2:

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

Extract the archive, and move to a different folder; for instance, in this example Mattermost will be stored on

/opt

:

# mv mattermost /opt

Create a storage directory, named “data”, in this folder:

# mkdir /opt/mattermost/data

The storage directory will contain all the files and images that users post to Mattermost, so it’s necessary that the drive that contains it is large enough.

Next, create a new user and a new group both named

mattermost

:

# useradd --system --user-group mattermost

Set the user and group mattermost as the owner of the Mattermost files with the command:

$ sudo chown -R mattermost:mattermost /opt/mattermost

Give the permissions to the Mattermost group:

sudo chmod -R g+w /opt/mattermost

Next, set up the database driver:

# $EDITOR /opt/mattermost/config/config.json

In that file, in the “SqlSettings” section, change “DriverName” and “DataSource” lines as follows:

"DriverName": "postgres"
"DataSource": "postgres://mmuser:my_strong_password@127.0.0.1:5432/mattermost_db?sslmode=disable&connect_timeout=10",

Save and exit. Test the configuration, using “mattermost” user:

$ sudo -u mattermost /opt/mattermost/bin/platform

You will see that Mattermost is running on 127.0.0.1:8065. Stop it with

CTRL+c

.

Next, create a new mattermost service file for systemd:

# $EDITOR /etc/systemd/system/mattermost.service

And paste into this file the following text:

[Unit]
Description=Mattermost, an open source alternative to Slack
After=network.target
After=postgresql.service
Requires=postgresql.service

[Service]
Type=simple
ExecStart=/opt/mattermost/bin/platform
Restart=always
RestartSec=10
WorkingDirectory=/opt/mattermost
User=mattermost
Group=mattermost

[Install]
WantedBy=multi-user.target

Save and exit. Then:

# systemd daemon-reload

Now, start the service:

# systemctl start mattermost

Install Nginx

Install Nginx and configure Nginx as the reverse proxy for Mattermost. First:

# apt install nginx

Next:

# mkdir /etc/nginx/ssl
# cd /etc/nginx/ssl

Generate a new self-signed SSL certificate file:

# openssl req -new -x509 -days 365 -nodes -out /etc/nginx/ssl/mattermost.crt -keyout /etc/nginx/ssl/mattermost.key
# chmod 400 mattermost.key

These will create a new certificate and change its permissions.
Next, create a new configuration file for mattermost in

/etc/nginx/sites-available

:

# $EDITOR /etc/nginx/sites-available/mattermost

and paste in it:

server {
   listen         80;
   server_name    mattermost.example.com;
   return         301 https://$server_name$request_uri;
}
 
server {
   listen 443 ssl;
   server_name mattermost.example.com;
 
   ssl on;
   ssl_certificate /etc/nginx/ssl/mattermost.crt;
   ssl_certificate_key /etc/nginx/ssl/mattermost.key;
   ssl_session_timeout 5m;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
   ssl_prefer_server_ciphers on;
   ssl_session_cache shared:SSL:10m;
 
   location / {
      gzip off;
      proxy_set_header X-Forwarded-Ssl on;
      client_max_body_size 50M;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      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_pass http://127.0.0.1:8065;
   }
}

Make sure to customize values for the Mattermost server IP address and FQDN for

server_name

.

Save, exit and activate the virtual host:

# ln -s /etc/nginx/sites-available/mattermost /etc/nginx/sites-enabled/

Test the configuration with:

# nginx -t

and then restart it, like this:

# systemctl restart nginx

Testing and conclusion

Last thing to do is a “graphical” configuration. With a web browser, go to URL mattermost.example.com.
There, create a new account. A dashboard will appear in which it’s possible to create new teams and/or access the admin/system console.
That’s all that is required for installing the messaging infrastructure for Mattermost.