How to Setup self-hosted Mattermost Chat server

Mehdi Taleghani
5 min readJul 26, 2024

--

Mattermost chat server is one of the good choices amoung other self-hosted chat servers such as RocketChat or Matrix-Synapse.

Its easy to set up and i found it very user-friendly. In this post i am going to walk you through the installaion.

Requirements:

A basic understanding of linux command line.

A Virtual machine which could be any VPS or local virtual machine with Ubuntu server 22.04 installed.

In this tutorial we assume you have already set up the server, such as setting up TimeZone, Hostname.

PreInstallation:

Update and upgrade the server if you haven't done before:

$ sudo apt update & sudo apt upgrade -y

Once done reboot the server and move on to the installtion step.

$ sudo reboot

Installation:

To install Mattermost we need to install 3 main components;:

  1. Mattermost chat server.
  2. Database server such as PostgreSQL.
  3. Optionally a reverse proxy server such as Caddy or Nginx.

Postgres Installation:

First we install postgreSQL using these commands:

$ sudo apt install postgresql postgresql-contrib

Then we enter the PostgreSQL

$ sudo --login --user postgres

Now switch to interactive mode

$ psql

Create a database, a user with password and finally grant all permissions to that user and exit the PostgreSQL CLI.

Note: don’t forget to replace DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD with your own values.

# CREATE DATABASE DATABASE_NAME; 
# CREATE USER DATABASE_USER WITH PASSWORD 'DATABASE_PASSWORD';
# GRANT ALL PRIVILEGES ON DATABASE DATABASE_USER to DATABASE_NAME;
# \q
$ exit

Now lets configure PostgreSQ by opening the following file using your favorite text editor.

Note: I have installed PostgreSQL version 14, if you are working with a different version, your directory could be diffrent. The 14 directory in the following command stands for the PostgreSQl version. so you need to change it accordingly.

$ sudo vim /etc/postgresql/14/main/postgresql.conf

in this file look for #listen_addresses = ‘localhost’ and remove the uncomment it by removing the #.

now open the following config file:

$ sudo vim /etc/postgresql/14/main/pg_hba.conf

Around line 104 look for “peer” and replace it with “trust”. It should look like this:

Finally restart the PosgreSQL service. and check its status.

$ sudo systemctl restart postgresql
$ sudo systemctl status postgresql

Make sure its in Active state.

That’s it, we have successfully installed and configured PostgreSQl.

MatterMost Server Installation.

Mattermost could be installed using Docker, from source code or from repository with the help of linux package manager.

In this article we will install it using apt package manager.

Issue the following commands to add mattermost repositories to your server.


$ curl -sL -o- https://deb.packages.mattermost.com/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/mattermost-archive-keyring.gpg > /dev/null
$ curl -o- https://deb.packages.mattermost.com/repo-setup.sh | sudo bash -s mattermost
$ sudo apt update

Now install MaterMost server.

$ sudo apt install mattermost -y

MatterMost will be installed in /opt/mattermost/ directory. Once its installed, dont start it, we are not done yet. We have to configure it to make sure it can talk to our PostgreSQL.

Open the following file using your favorite text editor.

$ sudo vim /opt/mattermost/config/config.json

Note: if you this file is not generated during the installation, run the following command to copy one.

$ sudo cp /opt/mattermost/config/config.defaults.json /opt/mattermost/config/config.json

In this file look for “DriverName” value and make sure the SQL driver is “postgres”.

Also in “DataSource”, replacae DATABASE-NAME, DATABASE-USER, AND DATABASE PASSWORD with your own value from previous steps.

your final file should look like this:

Now open the systemd service file and make it look like this:

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

Finally restart and enable MatterMost server.

$ sudo systemctl daemon-reload
$ sudo systemctl restart mattermost.service
$ sudo systemctl enable mattermost.service

Check its status and make sure its Active and Running.

$ sudo systemctl status mattermost.service

Nginx Installation:

This step is optional and you can skip it if you are not willing to use FQDN.

Issue the following command to install Nginx web server.

$ sudo apt install nginx

Let’s create a virtualhost for our MatterMost server by creating and opening this file.

$ sudo vim /etc/nginx/sites-available/mattermost.conf

Now insert the following in this file.

Note: rememebr to replace the example.com with your own FQDN.

server {
listen 80;

server_name mattermost.your-domain.com;
root /opt/mattermost;

error_log /var/log/nginx/mattermost.error;
access_log /var/log/nginx/mattermost.access;

location / {

proxy_pass http://localhost:8065;

}
}

Save and exit this file and enable the new site.

$ sudo ln -s /etc/nginx/sites-available/mattermost.conf /etc/nginx/sites-enabled/mattermost.conf

Finally restart the Nginx server and check the status to make sure its up and running.

$ sudo systemctl restart nginx
$ sudo systemctl status nginx

That’s it, now insert your server IP or your FQDN in your brwoser. You should see the MatterMost admin sign up page.

Please have in mind that if you are not using a reverse proxy such as Nginx, use the server IP with the port

http://SERVER_IP:8065/

Managing MatterMost:

Unfortunately its not possible to manage Users and other stuff in the web GUI of MatterMost.

MatterMost uses a utility called mmctl.

In order to use the mmctl admin fitures, you need to login to MatterMost CLI

$ mmctl auth login http://SERVER_IP:8065 --name default_connection --username adminuser

It will then prompt to type in your admin password. From there you can run any mmctl commands.

for example to create a new user.

$ mmctl user create --email user@example.com --username userexample --password Password1

You can look at it’s document page in:

https://docs.mattermost.com/manage/mmctl-command-line-tool.html

About the Author: My name is Mehdi Taleghani, i am a cloud and DevOps engineer, you can connect with me on linkedin.

More contents in my YouTube channel and my Instagram page.

--

--

Mehdi Taleghani
Mehdi Taleghani

Written by Mehdi Taleghani

Cloud AI Engineer | DevOps & MLOps | Expert in AI deployment, LLMs, Kubernetes, and cloud automation. Building scalable & efficient AI solutions.

No responses yet