How to deploy Mattermost on CentOS 7

Updated on

5 min read

Deploy Mattermost with Nginx on CentOS 7

Mattermost is an open-source, instant messaging platform, a self-hosted Slack alternative. It’s written in Golang and React and can use MySQL or PostgreSQL as a database backend. Mattermost brings all your team communication into one place and provides various features including file sharing, one-on-one and group messaging, custom emojis, video calls and more. In this tutorial, we will show you how to deploy Mattermost on a CentOS 7 server and configure Nginx as an SSL reverse proxy.

Prerequisites

Make sure that you have met the following prerequisites before continuing with this tutorial:

  • You are logged in as a user with sudo privileges .
  • You have a domain name pointing to your server IP address. We will use linuxize-test.com.
  • You have Nginx installed, if not check this guide.
  • You have an SSL certificate installed for your domain. You can install a free Let’s Encrypt SSL certificate by following this guide.

Create MySQL Database

We will use MariaDB 10.3 as a database back-end. Mattermost will not work with MariaDB version 5.5.

If you don’t have MariaDB 10.3 installed on your server you can check this guide .

Login to the MySQL shell:

mysql -u root -p

And run the following commands to create a new database and user for our Mattermost installation:

create database mattermost;GRANT ALL ON mattermost.* TO mattermost@localhost IDENTIFIED BY 'P4ssvv0rD';

Create new system user

To create a new user and group named mattermost, which will run the Mattermost installation, run the following command:

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

Install Mattermost Server

At the time of writing this article, the latest stable version of Mattermost is version 5.4.0. Before continuing with the next step you should check the Mattermost download page to see if a newer version is available.

Download the archive with the following curl command :

sudo curl -L https://releases.mattermost.com/5.4.0/mattermost-5.4.0-linux-amd64.tar.gz -o /tmp/mattermost.tar.gz

Once the download is completed extract the archive and move it to the opt directory:

sudo tar zxf /tmp/mattermost.tar.gz -C /opt

Create the storage directory for files:

sudo mkdir /opt/mattermost/data

Change the directory ownership to the mattermost user:

sudo chown -R mattermost: /opt/mattermost

Open the config.json file with your favorite text editor :

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

Set the database driver to mysql, enter the database name and database user password that we created earlier in this tutorial:

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

In order to test our installation to make sure everything works before creating systemd unit and setting up a reverse proxy with Nginx we will start the Mattermost server.

Change into the /opt/mattermost directory and start the server :

cd /opt/mattermostsudo -u mattermost bin/mattermost

The output should show that the Mattermost server is listening on port 8065 :

{"level":"info","ts":1540921243.6797202,"caller":"app/plugin.go:100","msg":"Starting up plugins"}
{"level":"info","ts":1540921244.3483207,"caller":"app/server.go:88","msg":"Starting Server..."}
{"level":"info","ts":1540921244.3488805,"caller":"app/server.go:148","msg":"Server is listening on [::]:8065"}
{"level":"info","ts":1540921244.3620636,"caller":"app/web_hub.go:75","msg":"Starting 2 websocket hubs"}
{"level":"info","ts":1540921244.451155,"caller":"jobs/workers.go:63","msg":"Starting workers"}
{"level":"info","ts":1540921244.456804,"caller":"jobs/schedulers.go:68","msg":"Starting schedulers."}

You can now stop the Mattermost server with CTRL+C and continue with the next steps.

Create a Systemd Unit

In order to run our Mattermost instance as a service we will create a mattermost.service unit file in the /etc/systemd/system/ directory with the following content:

/etc/systemd/system/mattermost.service
[Unit]
Description=Mattermost
After=network.target nss-lookup.target mariadb.service

[Service]
Type=notify
WorkingDirectory=/opt/mattermost
User=mattermost
SyslogIdentifier=mattermost
ExecStart=/opt/mattermost/bin/mattermost
TimeoutStartSec=3600
LimitNOFILE=49152

[Install]
WantedBy=multi-user.target

Notify systemd that we created a new unit file and start the Mattermost service by executing:

sudo systemctl daemon-reloadsudo systemctl start mattermost

We can now check the service status with the following command:

sudo systemctl status mattermost
● mattermost.service - Mattermost
   Loaded: loaded (/etc/systemd/system/mattermost.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-10-30 17:44:46 UTC; 3s ago
 Main PID: 25959 (mattermost)
   CGroup: /system.slice/mattermost.service
           └─25959 /opt/mattermost/bin/mattermost

Finally, enable the Mattermost service to be automatically started at boot time:

sudo systemctl enable mattermost

Set Up a Reverse Proxy with Nginx

If you followed our how to install Nginx on CentOS 7 and how to secure Nginx with Let’s Encrypt on CentOS 7 guides you should already have Nginx installed and configured with SSL certificate. Now we only need to create a new server block for our Mattermost installation.

/etc/nginx/conf.d/linuxize-test.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 linuxize-test.com www.linuxize-test.com;

    include snippets/letsencrypt.conf;
    return 301 https://linuxize-test.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name www.linuxize-test.com;

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

    return 301 https://linuxize-test.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name linuxize-test.com;

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

    access_log /var/log/nginx/linuxize-test.com-access.log;
    error_log /var/log/nginx/linuxize-test.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;
   }
}

Reload the Nginx service for changes to take effect:

sudo systemctl reload nginx

Configuring Mattermost

Open your browser, type your domain and create your first account:

create first Mattermost account

The first created user in the system has administrator privileges.

Mattermost account

Click on Create a new team link, create your first team, and set the team URL:

Create Mattermost Team
set Mattermost Team Url

After you create the first administrator account and the first team you will be redirected to the Mattermost dashboard, logged in as an administrator. Open the System Console, by clicking on your username at the top of the navigation panel, and in the new menu that opens, click on the System Console link:

Mattermost dashboard

Set the site URL by going to Settings General → Configuration.

Mattermost Settings

Enable email notifications by going to Notifications → Email

Mattermost Notifications

and enter your SMTP parameters. You can use any popular transactional email services such as SendinBlue, SendGrid, Amazon SES, Mandrill, Mailgun, Mailjet, and Postmark or you can set up your own mail server .

Finally, we need to restart the Mattermost service for changes to take effect:

sudo systemctl restart mattermost

Conclusion

You have successfully installed Mattermost on your CentOS 7 server and setup Nginx as a reverse proxy. You can now start using Mattermost to collaborate with your team.

If you are facing any problem with the installation, feel free to leave a comment.