How to Install Mattermost Team Messaging System on Debian 10
Mattermost is an open-source messaging system written in the programming languages Golang and React. It's a slack alternative, we can use it to build our own messaging service like slack or hipchat with it.
Mattermost brings your team communication to a single place and makes it accessible anywhere. You can access it from your desktop, Android device, and iPhone.
In this tutorial, we will show you how to install Mattermost on the Debian Buster 10. We will install Mattermost with the MySQL database server, Nginx web server, and running Mattermost as a Systemd service on the latest Debian version Buster 10.
Prerequisite
For this tutorial, we will test the Mattermost installation on the Debian 10 with 2GB of Ram, 25 Free disk space, and 2CPUs.
What we will do?
- Install MySQL Server
- Create MySQL Database for Mattermost
- Add System User and Download Mattermost
- Configure Mattermost
- Setup Mattermost as a Systemd Service
- Generate SSL Letsencrypt
- Install and Configure Nginx as a Reverse-Proxy
- Testing
Step 1 - Install MySQL Database
First, we will install the MySQL Server 8.0 from the official repository to our Debian server. Sp, we will add the official MySQL repository and install the MySQL packages.
Install the 'gnupg2' tool to the Debian server using the apt command below.
sudo apt install curl wget gnupg2
Download and add the MySQL repository for the Debian system using the following command.
wget https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb
dpkg -i mysql-apt-config_0.8.13-1_all.deb
Now update all Debian repositories and install the MySQL Server packages.
sudo apt update
sudo apt install mysql-server -y
During the MySQL Server installation, you will be asked to configure the root password for your MySQL server.
Type your root password for MySQL and repeat it.
Once the installation is completed, start the MySQL service and add it to the
systemctl start mysql
systemctl enable mysql
As a result, the MySQL Server is now installed on the Debian Buster 10. And the root password for MySQL Server has been configured.
Step 2 - Create MySQL Database for Mattermost
By default, the Mattermost support of two database drivers, the PostgreSQL and MySQL databases. And for this tutorial, we will use MySQL as the default database for Mattermost.
In this step, we will create a new database and user for the Mattermost installation.
Log in to the MySQL shell using your root user and password as below.
mysql -u root -p
Now create a new database and user for Mattermost. We will create a new database 'mattermost' with user 'mmuser' and the password 'mmuser-password'.
create database mattermost;
create user mmuser@localhost identified by 'mmuser-password';
grant all privileges on mattermost.* to mmuser@localhost;
flush privileges;
Now type 'exit' to log out from the MySQL shell.
And as a result, the MySQL database and user for the Mattermost installation has been created.
Step 3 - Add User and Download Mattermost
In this step, we will create a new system user and download the Mattermost source code. The Mattermost software will run under the user named 'mattermost', it will be installed on the '/opt/mattermost' directory.
Create a new system user named 'mattermost' using the command below.
useradd --system --user-group mattermost
Now go to the '/opt' directory and download the Mattermost source code using the curl command below.
cd /opt/
curl -o mattermost.tar.gz https://releases.mattermost.com/5.21.0/mattermost-5.21.0-linux-amd64.tar.gz
Extract the Mattermost source code and create a new 'data' directory.
tar -xf mattermost.tar.gz
mkdir -p /opt/mattermost/data
After that, change the ownership of the '/opt/mattermost' directory to the 'mattermost' user and make it writable.
chown -R mattermost:mattermost /opt/mattermost
chmod -R g+w /opt/mattermost
As a result, the 'mattermost' user has been created, and the Mattermost source code has been downloaded to the '/opt/mattermost' directory.
Step 4 - Configure Mattermost
In this step, we will set up the Mattermost listen to address and database. The Mattermost service will be running on the local IP address on default port 8065 and using MySQL as the database system.
Go to the '/opt/mattermost' directory and edit the configuration file 'config.json' on the 'config' directory.
cd /opt/mattermost/
vim config/config.json
On the 'ListenAddress' option, change the IP address to '127.0.0.1'.
"ListenAddress": "127.0.0.1:8065",
One 'SqlSettings', change the DriverName to 'mysql' and change the 'DataSource' with the MySQL database and user that we've created.
"SqlSettings": {
"DriverName": "mysql",
"DataSource": "dbuser:dbpassword@tcp(localhost:3306)/dbname?charset=utf8mb4,utf8\u0026readTimeout=30s\u0026writeTimeout=30s",
Save and close.
Next, initialize the Mattermost installation using the following command.
sudo -u mattermost ./bin/mattermost
Below is the result you will get.
As a result, the Mattermost is up and running on the local IP address '127.0.0.1' with the port '8065', now press the 'Ctrl+c' button to exit.
Step 5 - Setup Mattermost as a Service
In this step, we will set up Mattermost as a systemd service, and it will run automatically on the system boot after the MySQL database service is running.
Now go to the '/lib/systemd/system' directory and create a new service file 'mattermost.service'.
cd /lib/systemd/system/
vim mattermost.service
Now paste the following configuration into it.
[Unit]
Description=Mattermost
After=network.target
After=mysql.service
Requires=mysql.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
Save and close.
Next, reload the systemd manager on the Debian system.
systemctl daemon-reload
After that, start the Mattermost service and add it to the system boot.
systemctl start mattermost
systemctl enable mattermost
The Mattermost service is up and running, check it using the following command.
systemctl status mattermost
Below is the result you will get.
As a result, the Mattermost service is up and running on the Debian System, and it will automatically run on the system boot.
Step 6 - Install Certbot Letsencrypt
In this step, we will install the certbot tool and generate the SSL Letsencrypt. We will secure the Mattermost installation using the SSL from Letsencrypt.
Install the certbot tool using the apt command below.
sudo apt install certbot
Once the installation is complete, generate the SSL letsencrypt using the certbot command below.
certbot certonly --standalone --agree-tos -m [email protected] -d mattermost.hakase-labs.io
As a result, your SSL certificates will be generated at the '/etc/letsencrypt/live/mattermost-hakase-labs.io' directory.
Step 7 - Install and Configure Nginx as a Reverse Proxy
In this step, we will install the Nginx web server and set up it as a reverse proxy for the Mattermost service.
Install Nginx using the apt command below.
sudo apt install nginx -y
Once the installation is complete, start the Nginx service and add it to the system boot.
systemctl start nginx
systemctl enable nginx
The Nginx web server is up and running.
Next, go to the '/etc/nginx' configuration directory and create a new virtual host configuration for mattermost.
cd /etc/nginx/
vim sites-available/mattermost
Change the domain name and the path of SSL certificates with your own, then paste the configuration into it.
upstream backend {
server 127.0.0.1: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 edu.initrc.fun;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name edu.initrc.fun
ssl on;
ssl_certificate /etc/letsencrypt/live/edu.initrc.fun/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/edu.initrc.fun/privkey.pem;
ssl_session_timeout 1d;
ssl_protocols TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:50m;
# HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
add_header Strict-Transport-Security max-age=15768000;
# OCSP Stapling ---
# fetch OCSP records from URL in ssl_certificate and cache them
ssl_stapling on;
ssl_stapling_verify on;
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 and close.
Next, create the Nginx cache directory and change the ownership of that directory to the default 'www-data' user.
mkdir -p /var/cache/nginx
chown -R www-data:www-data /var/cache/nginx
After that, activate the Mattermost virtualhost, then test the nginx configuration and make sure there is no error.
ln -s /etc/nginx/sites-available/mattermost /etc/nginx/sites-enabled/
nginx -t
Now restart the Nginx service using the systemctl command below.
systemctl restart nginx
As a result, the Nginx installation and configuration as a reverse proxy for the Mattermost has been completed. And we're ready to test out Mattermost installation.
Step 8 - Testing
Open your web browser and type the domain name of your Mattermost installation URL on the address bar. Mine is:
https://mattermost.hakase-labs.io/
Now you need to create a new first account of the Mattermost, this will be the Mattermost administrator.
Type details your username, email, and password and clicks the "Create Account" button.
Create a new first team on Mattermost.
Type the name of your first team and click 'Next'.
And the team URL will be the team name, click 'Finish' to continue.
And you will automatically join to default channel "Off-Topic" and "Town Square".
As a result, the Mattermost installation on the Debian Buster 10 with MySQL database and Nginx web server has been completed successfully.