Deploy Mattermost on Ubuntu 20.04 - A Step by step guide ?

Mattermost is a self-hosted and open-source online chat. It is developed as a private conversation for organizations and companies. It's basically a tool with features like Slack and Microsoft Teams. And Mattermost is often installed with PostgreSQL database server.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related open-source Installations queries.

In this context, we shall look into a step by step guide on how to deploy Mattermost on Ubuntu 20.04.


Steps to deploy Mattermost on Ubuntu 20.04

1. Update system

To update the system, simply Run the below apt command:

$ sudo apt update

Then run the command:

$ sudo apt upgrade

Also, set proper server hostname:

$ sudo hostnamectl set-hostname NEW_HOSTNAME --static

Here, we set a hostname named "chat.ubuntu":

$ sudo hostnamectl set-hostname chat.ubuntu --static


2. Install PostgreSQL Database Server

i. To install PostgreSQL Database Server, Run the apt command to install:

$ sudo apt install postgresql postgresql-contrib

ii. Then login to the postgres account:

$ sudo --login --user postgres

iii. Start the PostgeSQL on the terminal and create the Mattermost database/user. Here we named it "linuxapt1" with the password "linuxapt1@123":

psql
CREATE DATABASE mattermost;
CREATE USER linuxapt1 WITH PASSWORD 'linuxapt1@123';
GRANT ALL PRIVILEGES ON DATABASE mattermost to linuxapt1;
\q


3. Create Mattermost system user and group

Now, We will create a system user and group named "mattermost":

$ sudo useradd --system --user-group mattermost

You can confirm by the command:

$ id mattermost


4. Install Mattermost

i. Begin by creating a folder named "mattermost" with the below command:

$ mkdir mattermost

ii. Then navigate to it:

$ cd mattermost

iii. Run the wget command to download Mattermost:

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

iv. Extract the package:

$ tar -xvzf mattermost*.gz

v. Copy the extracted file to /otp directory:

$ sudo cp -r mattermost /opt

vi. Create a folder for Mattermost to contain user data:

$ sudo mkdir /opt/mattermost/data

vii. Set correct ownership and permissions:

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

viii. Finally, let's grant write permission to /opt/mattermost directory:

$ sudo chmod -R g+w /opt/mattermost


5. Configure Mattermost Server

i. Config setting in the file /opt/mattermost/config/config.json:

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

ii. Then configure PostgreSQL database settings as desired.

iii. After the text editor is opened, press Ctrl + W and search "DriverName".

iv. Change the line in red part:

"postgres://matteruser:password@localhost:5432/mattermost?sslmode=disable&connect_timeout=10",

v. After that, Save the file.


6. Configure Systemd Service

i. Create Mattermost Systemd:

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

ii. Type the lines below:

[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

iii. Press ESC + :wq to save.

iv. Load the new unit into systemd:

$ sudo systemctl daemon-reload

v. Confirm service running status:

$ systemctl status mattermost.service

vi. Enable mattermost service:

$ sudo systemctl enable mattermost.service


7. Install Nginx

To install Nginx, simply run the below command:

$ sudo apt -y install nginx

You need to type your domain in the Mattermost configuration file:

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

Then save this file.

Next, Open the text editor to config able to access your domain:

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

Add the lines below:

upstream backend {
server localhost: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 your domain;
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;
}
}

Now, save this file.

To make this site configuration work, you must create a softlink for it in the folder /etc/nginx/sites-enabled:

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

Run this command to check the validity of the configuration:

$ sudo nginx -t

Restart Nginx:

$ sudo systemctl restart nginx

Then start mattermost service:

$ sudo systemctl start mattermost

Now you can try to access your domain.


[Need assistance in Installing any Open Source Software on Ubuntu Linux system ? We can help you. ]

This article covers how to deploy Mattermost on Ubuntu 20.04. Mattermost is an open source collaboration and messaging platform created with security and configurability in mind providing comparable functionality to Discord or Slack. It offers group, threaded and one-to-one messaging, unlimited search history and file sharing capabilities, two-factor authorization and notifications. Mattermost also provides webhooks and bot integration for further customization. It's accessible from every type of modern device due to its responsive web interface and dedicated mobile apps.

Now, you can easily connect with your team on Mattermost using a Desktop client. Even you can add multiple Mattermost servers to single desktop client and collaborate with different team users.

Related Posts