Introduction
Mattermost is a self-hosted and open-source online chat. It is developed as a private conversation for organizations and companies.
It’s a tool with features like Slack and Microsoft Teams. And Mattermost is often installed with PostgreSQL database server.
Below is the guide on how to deploy Mattermost on Ubuntu 20.04 as we go through below.
Step 1 – Update system
Run apt command:
$ sudo apt update
Then run the command:
$ sudo apt upgrade
Output:
Lastly, 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
Step 2 – Install PostgreSQL Database Server
Run the apt command to install:
$ sudo apt install postgresql postgresql-contrib
Output:
Then login to the postgres account:
$ sudo --login --user postgres
Start the PostgeSQL on the terminal and create the Mattermost database/user. Here I named it “linuxer” with the password “linuxer@123”
psql
CREATE DATABASE mattermost;
CREATE USER linuxer WITH PASSWORD 'linuxer@123';
GRANT ALL PRIVILEGES ON DATABASE mattermost to linuxer;
\q
Step 3 – Create Mattermost system user and group
We will create a system user and group named “mattermost”:
$ sudo useradd --system --user-group mattermost
You can confirm by the command:
$ id mattermost
Output:
Step 4 – Install Mattermost
Firstly, you must create a folder named “mattermost”
$ mkdir mattermost
Then navigate to it:
$ cd mattermost
Run the wget command to download Mattermost:
$ wget https://releases.mattermost.com/5.38.1/mattermost-5.38.1-linux-amd64.tar.gz
Output:
Extract the package:
$ tar -xvzf mattermost*.gz
Copy the extracted file to /otp directory:
$ sudo cp -r mattermost /opt
Create a folder for Mattermost to contain user data:
$ sudo mkdir /opt/mattermost/data
Set correct ownership and permissions:
$ sudo chown -R mattermost:mattermost /opt/mattermost
Finally, let’s grant write permission to /opt/mattermost directory:
$ sudo chmod -R g+w /opt/mattermost
Step 5 – Configure Mattermost Server
Config setting in the file /opt/mattermost/config/config.json
$ sudo nano /opt/mattermost/config/config.json
Then configure PostgreSQL database settings:
After the text editor is opened, press Ctrl + W and search “DriverName”
Change the line in red part:
"postgres://matteruser:password@localhost:5432/mattermost?sslmode=disable&connect_timeout=10",
Save it.
Step 6 – Configure Systemd Service
Create Mattermost Systemd:
$ sudo vim /etc/systemd/system/mattermost.service
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
Press ESC + :wq to save.
Load the new unit into systemd:
$ sudo systemctl daemon-reload
Confirm service running status:
$ systemctl status mattermost.service
Output:
Enable mattermost service:
$ sudo systemctl enable mattermost.service
Step 7 – Install Nginx
$ sudo apt -y install nginx
You need to type your domain in the Mattermost configuration file:
$ sudo nano /opt/mattermost/config/config.json
Save it.
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; } }
Save it.
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
Output:
Restart Nginx:
$ sudo systemctl restart nginx
Then start mattermost service:
$ sudo systemctl start mattermost
You can try to access your domain.
Conclusion
You’ve already gone through the details of how to deploy Mattermost on Ubuntu 20.04.Thanks for reading.