17. チャットサーバ構築 – mattermost [さくらのVPS/CentOS7]

・mattermostのインストール

#-- 変数に必要な値を代入
DOMAIN=masdon.life
#-- admin@masdon.life のパスワード
PASSWORD=********
VERSION=5.6.2
HTTPS_DOCROOT=/var/www/html/https_root

#-- mattermost のダウンロードとインストール
cd ~/work/src
curl -O https://releases.mattermost.com/${VERSION}/mattermost-team-${VERSION}-linux-amd64.tar.gz
tar xpf mattermost-team-${VERSION}-linux-amd64.tar.gz
mv mattermost /opt/
mkdir -p /opt/mattermost/data
adduser mattermost
chown -R mattermost. /opt/mattermost
chmod -R g+w /opt/mattermost

#-- mattermost database を作成 (データ投入は初回起動時に自動で行われる)
mysql -e "create database mattermost character set utf8 collate utf8_bin;"
#-- database の password は お好みで
mysql -e "GRANT ALL PRIVILEGES ON mattermost.* TO 'mmuser'@'localhost' IDENTIFIED BY 'mostest' ;"
mysql -e "FLUSH PRIVILEGES;"

#-- mattermost の設定
cp -p /opt/mattermost/config/config.json{,.org}
sed -i -e 's/dockerhost/127.0.0.1/' \
-e 's/mattermost_test/mattermost/' \
-e "s#SiteURL.*#SiteURL\": \"https://${DOMAIN}/mattermost/\",#" \
-e 's/SendEmailNotifications.*/SendEmailNotifications": true,/' \
-e 's/FeedbackName.*/FeedbackName": "Mattermost",/' \
-e "s/FeedbackEmail.*/FeedbackEmail\": \"admin@${DOMAIN}\",/" \
-e 's/EnableSMTPAuth.*/EnableSMTPAuth": true,/' \
-e "s/SMTPUsername.*/SMTPUsername\": \"admin@${DOMAIN}\",/" \
-e "s/SMTPPassword.*/SMTPPassword\": \"${PASSWORD}\",/" \
-e "s/SMTPServer.*/SMTPServer\": \"${DOMAIN}\",/" \
-e 's/SMTPPort.*/SMTPPort": "465",/' \
-e 's/RequireEmailVerification.*/RequireEmailVerification": true,/' \
-e 's/"en"/"ja"/' /opt/mattermost/config/config.json

sed -i -e '186s/ConnectionSecurity.*/ConnectionSecurity": "TLS",/' /opt/mattermost/config/config.json

sed -i -e 's#"Directory": "./data/"#"Directory": "/opt/mattermost/data/"#' \
-e 's#"Directory": "./plugins"#"Directory": "/opt/mattermost/plugins"#' \
-e 's#"ClientDirectory": "./client/plugins"#"Directory": "/opt/mattermost/client/plugins"#' /opt/mattermost/config/config.json

#-- mattermost の起動ルール
cat <<_EOF_> /etc/systemd/system/mattermost.service
[Unit]
Description=Mattermost
After=mysqld.service postfix.service

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

[Install]
WantedBy=multi-user.target
_EOF_

#-- mattermost 起動
systemctl enable mattermost
systemctl start mattermost

#-- mattermost 用の nginx の設定追加
cp -p /etc/nginx/conf.d/https.conf{,.org}
cat <<'_EOF_'>> /etc/nginx/conf.d/https.conf

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=200m inactive=120m use_temp_path=off;
_EOF_

cat <<'_EOF_'> /etc/nginx/conf.d/https.d/mattermost.conf
  location ^~ /mattermost {
    location ~ /mattermost/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 /mattermost/ {
      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;
    }
  }
_EOF_

systemctl restart nginx

#-- mattermostの日本語全文検索対応
mysql mattermost -u mmuser -pmostest -e "ALTER TABLE \`Posts\` ENGINE = Mroonga;"

最初に作成するアカウントが管理者となる為、直ぐにアカウントを作成すること
https://masdon.life/mattermost/