HackMDをインストール

# HackMDインストール手順

## 1. OS環境
- Virtual Box CentOS 6.9 minimal

## 2. 環境準備
iptableとselinuxは不要ならば止めておきます。

```shell=
# stop iptables & ip6tables
service iptables stop
service ip6tables stop
chkconfig iptables off
chkconfig ip6tables off

# stop selinux
vi /etc/sysconfig/selinux # SELINUX=permissive
setenforce 0
```

## 3. 必要なパッケージインストール
インストールの際に必要となるパッケージをインストールします。

- git

```shell=
# git install
yum install git
```

- gcc

```shell=
# gcc version >= 4.8
cd /etc/yum.repos.d
wget http://people.centos.org/tru/devtools-2/devtools-2.repo
yum install devtoolset-2-binutils-devel devtoolset-2-gcc devtoolset-2-gcc-c++
scl enable devtoolset-2 bash
cat >> $HOME/.bash_profile << EOF

echo "WARNING: devtoolset-2 is enabled!"
. /opt/rh/devtoolset-2/enable
EOF

# gcc version check
gcc -v
```

- node & forever

```shell=
# nvm install
wget https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh
sh install.sh

# open new session and install node
nvm install v9.4.0

# forever install
npm install -g forever
```

- mysql

```shell=
# mysql version = 5.6
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
rpm -ivh mysql-community-release-el6-5.noarch.rpm
yum install mysql mysql-server
vim /etc/my.cnf
service mysqld start

# initiate mysql: setup password of root and delete useless tables
/usr/bin/mysql_secure_installation

# create database
mysql -u root -p
create database hackmd default character set utf8 collate utf8_bin;
create user 'hackmd'@'localhost' identified by 'hackmd';
grant all on hackmd.* to 'hackmd'@'localhost';
```

- uWebSockets

```shell=
# install in another dir
npm install --save uws
```
## 4. HackMDインストール
HackMD本体のインストール

```shell=
git clone https://github.com/hackmdio/hackmd.git
cd hackmd
./bin/setup

# config.json           -- server config
# public/js/config.js   -- client config
# .sequelizerc          -- db config

```

```shell=
# config.json setting
cat config.json # delete all not needs and change config

# please delete all of comment after copy
{ 
    "production": {
        "domain": "localhost", # must the same as the domain:port or ip:port  in the actual url
        "port": 8338,
        "debug": false, # debug mode
        "protocolusessl": true, # use ssl
        "hsts": {
            "enable": "true",
            "maxAgeSeconds": "31536000",
            "includeSubdomains": "true",
            "preload": "true"
        },
        "db": {
            "username": "XXXX",
            "password": "XXXX",
            "database": "XXXX",
            "host": "localhost", # host name or ip
            "port": "3306",
            "dialect": "mysql"
        }
    }
}
```

```shell=
# .sequelizerc setting
cat .sequelizerc # example: postgres://username:password@localhost:5432/hackmd

# please delete all of comment after copy
var path = require('path');

module.exports = {
    'config':          path.resolve('config.json'),
    'migrations-path': path.resolve('lib', 'migrations'),
    'models-path':     path.resolve('lib', 'models'),
    'url':             'mysql://hackmd:hackmd@localhost:3306/hackmd'
}
```

```shell=
# init mysql tables
node_modules/.bin/sequelize db:migrate

# cp uWebSockets module
mv node_modules/uws/ ~/
cp -a ~/node_modules/uws/ node_modules/

# build project
npm run build
```

## 5. HackMD起動停止スクリプト


```shell=
export NODE_ENV=production

opt=$1
if `echo ${opt} | grep -wP 'start|stop' > /dev/null 2>&1`
then
    cd /path/to/hackmd # please change the path after copy
    forever ${opt} app.js
else
    echo "./`basename "$0"` <start|stop>"
fi
```

## 6. nginx設定
nginxでプロキシ設定して公開

```nginx=
http{
    proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:8m max_size=64m;

    server {
        listen 443 ssl;
        server_name XXXXXXXX;
        ssl on;
        ssl_certificate     /etc/nginx/conf.d/ssl/XXXXXX.crt;
        ssl_certificate_key /etc/nginx/conf.d/ssl/XXXXXX.key;
        ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers          "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";
        ssl_dhparam /etc/nginx/conf.d/ssl/dhparam.pem;
        ssl_session_cache    shared:SSL:10m;
        ssl_session_timeout  10m;
        add_header Strict-Transport-Security "max-age=15768000; includeSubdomains";

        location / {
            proxy_pass http://hackmd;
            proxy_redirect    off;
            proxy_http_version 1.1;
            proxy_set_header   Host $host;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header   X-Real-IP $remote_addr;
            
            # for uWebSockets
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection "upgrade";

            # cache static source
            location ~* \.(html|css|jpg|gif|ico|js)$ {
                proxy_cache          cache;
                proxy_cache_key      $host$uri$is_args$args;
                proxy_cache_valid    200 301 302 30m;
                expires              30m;
                proxy_pass  http://hackmd;
                proxy_redirect    off;
                proxy_http_version 1.1;
            }
        }
    }

    upstream hackmd {
        server localhost:8338;
    }
}
```



## 7. トラブルシューティング
- forever log: `$HOME/.forever/*.log`
- hackmd debug mode: ==config.json== file ==production== section add `"debug": true,`