How to install Mattermost on RHEL 7.1

Mattermost

Introduction

Mattermost is an open source, private cloud Slack-alternative. A workplace messaging system for web, PCs and phones, released under the MIT license.
In a previous tutorial we talked about how to install it on Ubuntu 16.04.
Now, let’s see how to install and configure Mattermost on a RHEL 7.1 machine using MySQL as the database.

Install Database

On the server, download MySQL 5.7 executing the following command:

# wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

and install the yum repository from that file with:

# yum localinstall mysql57-community-release-el7-9.noarch.rpm

Next, install MySQL:

# yum install mysql-community-server

and start it:

# systemctl start mysqld

After executing this command for the first time, MySQL will generate a temporary password for the root account. To retrieve it, just:

# grep 'temporary password' /var/log/mysqld.log

This command will output something like this:

2017-03-02T08:21:27.969295Z 1 [Note] A temporary password is generated for root@localhost: Ed4SxpDyuH(y

Change the root password. First, login as root:

# mysql -u root -p

Enter the temporary password.
Next, in the MySQL shell:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'my_new_root_strong_password';
mysql> EXIT;

Set MySQL to start automatically at boot time:

# chkconfig mysqld on

Start the MySQL shell again:

# mysql -u root -p

Entering the new root password, create a user for Mattermost and a new database:

mysql> CREATE USER 'mmuser'@'localhost' IDENTIFIED BY 'mmuser_strong_password';
mysql> CREATE DATABASE mattermostdb;
mysql> GRANT ALL PRIVILEGES ON mattermostdb.* TO 'mmuser'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;

Install Mattermost Server

Download the latest release of Mattermost Server. For example only, at the time we are writing:

# wget https://releases.mattermost.com/3.6.2/mattermost-3.6.2-linux-amd64.tar.gz

Extract the archive, and move the ‘mattermost’ folder to

/opt
# tar xf *.gz
# mv mattermost /opt/

Create a directory for storage files:

# mkdir /opt/mattermost/data

Make sure that the drive is large enough to hold the anticipated number of uploaded files and images that will be stored on

data

.
Next, set up a user and group, both named ‘mattermost’, and set the ownership and permissions:

# useradd --system --user-group mattermost
# chown -R mattermost:mattermost /opt/mattermost
# chmod -R g+w /opt/mattermost

Set up the database driver through the

/opt/mattermost/config/config.json

file. In it, search for “DriverName” and “DataSource” lines and change as follows:

"DriverName": "mysql"
"DataSource": "mmuser:@tcp(localhost:3306)/mattermost?charset=utf8"
Save, exit, and test the Mattermost Server with the following command:
# sudo -u mattermost /opt/mattermost/bin/platform

If everything works, it should output

 Server is listening on :8065

 . Interrupt it with CTRL+C.

Create a systemd unit.

Create a systemd file for Mattermost,

/etc/systemd/system/mattermost.service

and, in it, paste the following configuration:

[Unit]
Description=Mattermost
After=syslog.target network.target postgresql-9.4.service

[Service]
Type=simple
WorkingDirectory=/opt/mattermost/bin
User=mattermost
ExecStart=/opt/mattermost/bin/platform
PIDFile=/var/spool/mattermost/pid/master.pid
LimitNOFILE=49152

[Install]
WantedBy=multi-user.target

Make it executable:

# chmod 664 /etc/systemd/system/mattermost.service

And reload the services:

# systemctl daemon-reload

Enable Mattermost service:

# chkconfig mattermost on

And start it with systemd:

# systemctl start mattermost

Check if it’s running visiting the URL http://localhost:8065.

Install and configure NGINX

Installation

In a production system, use a proxy server in front of Mattermost Server. In this case, NGINX.
The main benefits of doing this are:

  • SSL termination
  • Port mapping :80 to :8065
  • HTTP to HTTPS redirect
  • Standard request logs

In order to install NGINX on RHEL 7.1, create a yum repository file,

/etc/yum.repos.d/nginx.repo

, with the following content:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/7.1/$basearch/
gpgcheck=0
enabled=1

Save, exit and install NGINX with yum:

# yum install nginx.x86_64

Start NGINX and test it:

# systemctl start nginx
Configuration

In order to configure NGINX as proxy server, create the file

/etc/nginx/sites-available/mattermost

and past:

upstream backend {
   server localhost:8065;
}

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    mattermost.mydomain.com;

   location /api/v3/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://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_pass http://backend;
   }
}

Remove the existing default site-enabled file with:

# rm /etc/nginx/sites-enabled/default

and enable Mattermost:

# ln -s /etc/nginx/sites-available/mattermost /etc/nginx/sites-enabled/mattermost

Restart NGINX:

# systemctl restart nginx

Conclusions

At the end of this process, the server should be up and running. With a web browser go to URL http://mattermost.mydomain.com and continue to configure Mattermost by entering an email address and creating an account.
That’s all! The server is ready to serve as your messaging system!