ConoHaにRocketChat構築したのでメモ

ConoHaのCentOS7でRocketChatの環境構築したのでメモ

アカウントとか周りは余り変わらないのでこちら参照
https://qiita.com/fkds/items/f90d86837ef3a552924a

## パッケージ導入
* Node: 8.11.4
* NPM: 6.4.1
* MongoDB: 4.0

```bash
$ sudo yum -y check-update
$ cat << EOF | sudo tee -a /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
EOF
$ sudo yum install -y curl && curl -sL https://rpm.nodesource.com/setup_8.x | sudo bash -
$ sudo yum install -y gcc-c++ make mongodb-org nodejs
$ sudo yum install -y epel-release && sudo yum install -y GraphicsMagick
$ sudo npm install -g inherits n && sudo n 8.11.4
$ curl -L https://releases.rocket.chat/latest/download -o /tmp/rocket.chat.tgz
$ tar -xzf /tmp/rocket.chat.tgz -C /tmp
$ cd /tmp/bundle/programs/server && npm install
$ sudo mv /tmp/bundle /opt/Rocket.Chat
```

## RocketChat設定
ROOT_URLは書き換えてください

```bash
$ sudo useradd -M rocketchat && sudo usermod -L rocketchat
$ sudo chown -R rocketchat:rocketchat /opt/Rocket.Chat
$ cat << EOF |sudo tee -a /lib/systemd/system/rocketchat.service
[Unit]
Description=The Rocket.Chat server
After=network.target remote-fs.target nss-lookup.target nginx.target mongod.target
[Service]
ExecStart=/usr/local/bin/node /opt/Rocket.Chat/main.js
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=rocketchat
User=rocketchat
Environment=MONGO_URL=mongodb://localhost:27017/rocketchat?replicaSet=rs01 MONGO_OPLOG_URL=mongodb://localhost:27017/local?replicaSet=rs01 ROOT_URL=https://hogehoge.site/ PORT=3000
[Install]
WantedBy=multi-user.target
EOF
$ sudo sed -i "s/^# engine:/ engine: mmapv1/" /etc/mongod.conf
$ sudo sed -i "s/^#replication:/replication:\n replSetName: rs01/" /etc/mongod.conf
$ sudo systemctl enable mongod && sudo systemctl start mongod
$ mongo --eval "printjson(rs.initiate())"
$ sudo systemctl enable rocketchat && sudo systemctl start rocketchat
```

## httpsで表示したいので
証明書は頭に乗っけているURL参照。

```bash
$ sudo yum -y install nginx
```

/etc/nginx/conf.d/default.confを編集

```vim
# Upstreams
upstream backend {
    server 127.0.0.1:3000;
}
 
# Redirect Options
server {
  listen 80;
  server_name hogehoge.site;
  # enforce https
  return 301 https://$server_name$request_uri;
}
 
# HTTPS Server
server {
    listen 443;
    server_name hogehoge.site;
 
    # You can increase the limit if your need to.
    client_max_body_size 200M;
 
    error_log /var/log/nginx/rocketchat.access.log;
 
    ssl on;
    ssl_certificate     /etc/letsencrypt/live/hogehoge.site/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/hogehoge.site/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 
    location / {
        proxy_pass http://backend/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
 
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forward-Proto http;
        proxy_set_header X-Nginx-Proxy true;
 
        proxy_redirect off;
    }
}
```

```bash
sudo systemctl enable nginx && sudo systemctl start nginx
```

## RocketChatの設定
https://hogehoge.site/
↑に接続すれば初回設定画面がでてくるので、設定

## メモ
### なんか画像アップロードが出来なくなる問題
/tmp/ufsが無いとアップロードできなくなる。（CentOS7だと自動で削除されてしまうらしい）
デフォルトのGridFS、AWS S3を対象にしていても発生した。

なのでこれを設定。

```shell:upload_tmp.sh
#!/bin/sh
​
rc_tmp_dir=/tmp/ufs
​
if [ ! -e $rc_tmp_dir ]; then
  mkdir -p $rc_tmp_dir && chmod 777 $rc_tmp_dir
fi
```

cronに

```vim
0 * * * * /opt/hogehoge/upload_tmp.sh cd >/dev/null 2>&
```

### サービスの再起動
順々に動かさないと動いてくれない

```bash
$ sudo systemctl stop nginx
$ sudo systemctl stop rocketchat
$ sudo systemctl stop mongod
$ sudo systemctl start mongod
$ sudo systemctl start rocketchat
$ sudo systemctl start nginx
```