Install Ghost 0.3.3 on Arch Linux

## Ghost 0.3.3 をArch Linuxへインストール

Ghost 0.3.3をArch Linuxへインストールしてみたので手順のメモ。

### SourceをDownloadし、コンテンツ用Directoryへ配置

```bash
cd /usr/local/src
wget https://ghost.org/zip/ghost-0.3.3.zip
cd /srv/http
unzip /usr/local/src/ghost-0.3.3.zip -d ghost
chown -R http ghost
```

### DBを作成

MarinaDBが自動で起動するよう設定

```bash
systemctl enable mysqld.service
systemctl start mysqld.service
```

以下2つのDBを作成

* ghost
* ghost_dev

MarinaDBへログイン

```bash
mysql -u root -p
password: ******
```

```mysql
MariaDB [(none)]> CREATE DATABASE ghost CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> CREATE DATABASE ghost_dev CHARACTER SET utf8;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ghost              |
| ghost_dev          |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

MariaDB [(none)]> GRANT ALL ON ghost.* to ghost@localhost;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> GRANT ALL ON ghost_dev.* to ghost_dev@localhost;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SET PASSWORD FOR ghost@localhost=password('YOUR PASSWORD');
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SET PASSWORD FOR ghost_dev@localhost=password('YOUR PASSWORD');
Query OK, 0 rows affected (0.01 sec)
```

### config.jsを編集

```bash
cd /srv/http/ghost
vi config.js
```

config.jsではDB Client部分をsqlite3からmarinadb(mysql)へ変更します。

```vim:config.js
     // ### Production
(snip)
            client: 'mysql',
            connection: {
                host: 'localhost',
                user: 'ghost',
                password: 'YOUR PASSWORD',
                database: 'ghost',
                charset: 'utf8'
             },
(snip)
    // ### Development **(default)**
(snip)
        database: {
            client: 'mysql',
            connection: {
               host: 'localhost',
               user: 'ghost_dev',
               password: 'YOUR PASSWORD',
               database: 'ghost_dev',
               charset: 'utf8'
            },
(snip)
```

### npm install
```bash
cd /srv/http/ghost
npm install --production
```

### defaultの起動モードをproductionに設定
```bash
vi index.js
```

```vim:index.js
process.env.NODE_ENV = 'production';
```

### Apacheの設定

httpd-vhosts.confを読み込むように設定

```bash
cd /etc/httpd/conf
vi httpd.conf
```

```vim:httpd.conf
- #Include conf/extra/httpd-vhosts.conf
+ Include conf/extra/httpd-vhosts.conf
```

VirtualHostを設定する

```bash
cd /etc/httpd/conf/extra/
vi httpd-vhosts.conf
```

```vim:httpd-vhosts.conf
#### ghost
<VirtualHost *:80>
    ServerAdmin wwwadmin@example.com
    DocumentRoot "/srv/http/ghost/"
    ServerName example.com
    ServerAlias www.example.com
    AddDefaultCharset Off
    ErrorLog "/var/log/httpd/www.example.com/error_log"
    CustomLog "/var/log/httpd/www.example.com/access_log" common
    <Proxy *>
        order deny,allow
        allow from 127.0.0.1
    </Proxy>
    <IfModule mod_proxy.c>
        ProxyPass / http://localhost:2368/
        ProxyPassReverse / http://localhost:2368/
        ProxyPreserveHost On
    </IfModule>
</VirtualHost>
```
Apacheが自動で起動するよう設定

```bash
systemctl enable httpd.service
systemctl start httpd.service
```
Apacheを起動する。

```bash
systemctl start httpd.service
```

### pm2でindex.jsをdaemon化する

pm2をインストールする。

```bash
cd /srv/http/ghost
npm install pm2
```

一応起動させる。

```bash
pm2 start /srv/http/ghost/index.js -i max
pm2 list
```

### サーバ起動時にindex.jsが自動で起動するよう設定する

/usr/lib/systemd/systemへ移動し、ghost.serviceを作成

```bash
cd /usr/lib/systemd/system/
vi ghost.service
```

```vim:ghost.service
[Unit]
Description=Ghost service
# Make sure we have functional network and logging available
After=syslog.target
After=network.target

[Service]
# Optional, assign custom user and group for this service
User=root
Group=root
# Allow process forking
ExecStart=/usr/bin/pm2 start /srv/http/ghost/index.js -i max
#ExecStop=/usr/bin/pm2 stop all
#ExecReload=/usr/bin/pm2 restart all
Type=forking
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=10

[Install]
WantedBy=multi-user.target
```