How to Install Mattermost on Ubuntu 18.04

Install Mattermost on Ubuntu 18.04

In this article, we will have explained the necessary steps to install and configure Mattermost on Ubuntu 18.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Mattermost is an open-source platform that is written in React and Golang. It can be used as an alternative messaging platform and utilizes the PostgreSQL or MySQL database in the backend. Mattermost main function is to bring team communication together and offer several features such as file sharing, custom emojis, video calls, messaging (one-on-one or group), and so on.

Install Mattermost on Ubuntu 18.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install MySQL Database.

Installing the MySQL package with the following command:

sudo apt install mysql-server

MySQL server package comes with a script called mysql_secure_installation that can perform several security-related operations. Run the script by typing:

sudo mysql_secure_installation

Next, create a new database and user for our Mattermost installation with:

$ mysql -u root
CREATE DATABASE mattermost;
GRANT ALL ON mattermost.* TO mattermost@localhost IDENTIFIED BY 'PassWD';

Now, create a new system user and group for the sake of Mattermost instance. In this case, we will name our user mattermost:

sudo useradd -U -M -d /opt/mattermost mattermost

Step 3. Install Mattermost on Ubuntu.

First, download the latest stable Mattermost version:

sudo curl -L https://releases.mattermost.com/5.1.0/mattermost-5.1.0-linux-amd64.tar.gz -o /tmp/mattermost.tar.gz
sudo tar zxf /tmp/mattermost.tar.gz -C /opt
sudo mkdir -p /opt/mattermost/data

Make sure you change the ownership of the directory to the mattermost user:

sudo chown -R mattermost: /opt/mattermost

Then, open the /opt/mattermost/config/config.json file, set the database driver to mysql and enter the database information:

nano /opt/mattermost/config/config.json
"SqlSettings": {
    "DriverName": "mysql",
    "DataSource": "mattermost:P4ssvv0rD@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s&writeTimeout=30s",

Now, we need to test whether the Mattermost server is working properly. To do so, change into the /opt/mattermost directory then run the command below to start the server:

cd /opt/mattermost
sudo -u mattermost bin/mattermost

Step 4. Creating Systemd Unit.

We will create a mattermost.service unit file in the /etc/systemd/system/ directory:

nano /etc/systemd/system/mattermost.service
[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=mysql.service

Then restart the Mattermost service using the commands below:

sudo systemctl daemon-reload
sudo systemctl start mattermost
sudo systemctl enable mattermost

Step 5. Setup a reverse proxy using Nginx.

If you have not installed nginx, follow the following this tutorial. Now we need to set up a new server block for the Mattermost instance:

nano /etc/nginx/conf.d/example.com.conf
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=mattermost_cache:10m max_size=3g inactive=120m use_temp_path=off;

upstream mattermost_backend {
  server 127.0.0.1:8065;
}

server {
    listen 80;
    server_name example.com www.example.com;

    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;

    access_log /var/log/nginx/example.com-access.log;
    error_log /var/log/nginx/example.com-error.log;

    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;
       proxy_read_timeout 600s;
       proxy_pass http://mattermost_backend;
    }

    location / {
       proxy_http_version 1.1;
       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_pass http://mattermost_backend;
    }
}

Step 6. Accessing Mattermost.

To configure Mattermost service, go to your browser and enter the domain name where you will be directed to the signup page.

That’s all you need to do to install Mattermost on Ubuntu 18.04. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.