paloma blog

NWエンジニアやってます。主に自宅環境のお遊びを書きます。Pythonもちょっと。タイトルは好きなカクテルから。

Turnkey LinuxにFirefly IIIをインストールする

一人暮らししてると家計簿つけようかなーとか考えるんですが、実際やるとすごいめんどうですよね。

レシート取っといて毎日記帳とかやってられません。
GNU Cashも使ってた時期がありましたが、多機能すぎて挫折しました。

そんなわけで支出は極力クレジットカードを使って記録して、後から集計できるようにしています。
月次の支払い集計ツールを作る スクリプト化する編 - paloma blog

そんな中面白そうなOSSの家計簿ツールを見つけたので作ってみました。

  • Firefly III

www.moongift.jp

FireflyといえばJuniperの仮想FWを思い出しますねw

構築

この手のツールはDockerfileとかovaが提供されていたらそのままデプロイして、出来た出来たw
とやっていたのですが、僕もインフラ屋の端くれなので今回は自分で作ってみようと思います。
(Firefly IIIもDockerfile提供されています)

要件

公式のインストールガイドを見ると

での構築がサポートされているようですね。

OSはCentOSUbuntuですがイチからインストールするのも大変なのでTurnkey Linuxで作ってみようと思います。

ちょうどnginxとphpVMがありました。
Turnkey LinuxDebianなのですが、たぶん大丈夫でしょう。

www.turnkeylinux.org

これをVirtualboxにインポートします。
起動直後にwebコンソールやsshが使えるのは楽ですね。

初期設定

ユーザ作成、IP設定等の初期設定は割愛します。
SElinux、Firewalld、iptablesはインストールされていないし、
家で使うのでこのまま未使用で行きます。

インストールディレクトリは/var/www/html/fireflyにします。

パッケージインストール

php7.3をインストールします。
Debian stretchのリポジトリにはまだ入っていないので下記を参考にインストールします。

def-4.com

masashi@nginx-php-fastcgi:/var/www/firefly$ sudo apt install php7.3 php7.3-fpm php7.3-mysql php7.3-mbstring php7..3-xml php7.3-gd php7.3-curl
composer

composerとはphpのパッケージ管理システムのようですね。
phpは全然使わないのでさっぱりです。

masashi@nginx-php-fastcgi:~$ curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/biin --filename=composer
All settings correct for using Composer
Downloading...

Composer (version 1.9.1) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

masashi@nginx-php-fastcgi:~$ composer -v
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.9.1 2019-11-01 17:20:17


...

OK!

Firefly III インストール

composerを使ってインストールします。
現時点の最新安定版4.8.1.8にします。

masashi@nginx-php-fastcgi:/var/www/firefly$ composer create-project grumpydictator/firefly-iii --no-dev --prefer-dist firefly-iii 4.8.1.8
Installing grumpydictator/firefly-iii (4.8.1.8)
  - Installing grumpydictator/firefly-iii (4.8.1.8): Downloading (connecting...)Downloading (100%)         
Created project in firefly-iii
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
> @php -r "if (!(getenv('DYNO'))===false){file_exists('.env') || copy('.deploy/heroku/.env.heroku', '.env');copy('.deploy/heroku/.locales', '.locales');}"
Loading composer repositories with package information
Installing dependencies from lock file
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - The requested PHP extension ext-bcmath * is missing from your system. Install or enable PHP's bcmath extension.
  Problem 2
    - The requested PHP extension ext-intl * is missing from your system. Install or enable PHP's intl extension.
  Problem 3
    - Installation request for adldap2/adldap2 v10.1.1 -> satisfiable by adldap2/adldap2[v10.1.1].
    - adldap2/adldap2 v10.1.1 requires ext-ldap * -> the requested PHP extension ldap is missing from your system.
  Problem 4
    - adldap2/adldap2 v10.1.1 requires ext-ldap * -> the requested PHP extension ldap is missing from your system.
    - adldap2/adldap2-laravel v6.0.8 requires adldap2/adldap2 ^10.1 -> satisfiable by adldap2/adldap2[v10.1.1].
    - Installation request for adldap2/adldap2-laravel v6.0.8 -> satisfiable by adldap2/adldap2-laravel[v6.0.8].


...

なんかエラーが出ました。
phpのパッケージが足りないようです。

  • sudo apt install php7.3-bcmath
  • sudo apt install php7.3-intl
  • sudo apt install php7.3-ldap

の3つを追加。
必要パッケージはガイドにちゃんと書いてありました。

再実行したらインストールできました。

...

+------------------------------------------------------------------------------+
|                                                                              |
| Thank you for installing Firefly III, v4.8.1.8!                              |
| This version of Firefly III requires PHP7.3.                                 |
|                                                                              |
+------------------------------------------------------------------------------+
> @php artisan key:generate
Application key set successfully.

作成されたディレクトリの権限変更します。

masashi@nginx-php-fastcgi:/var/www$ cd firefly/
masashi@nginx-php-fastcgi:/var/www/firefly$ sudo chown -R www-data:www-data firefly-iii
[sudo] password for masashi: 
masashi@nginx-php-fastcgi:/var/www/firefly$ sudo chmod -R 775 firefly-iii/storage
環境設定

.envファイルを自分の環境用に編集します。
ここドキュメント内でもノーヒントなのでちょっときつかったですね。

DB環境周りだけ変更。
HOSTはlocalhostだと後のフェーズでうまくいかなかったので127.0.0.1へ修正。
githubのissueにもありました。

404 "The requested URL /firefly-iii/public/login was not found on this server." · Issue #317 · firefly-iii/firefly-iii · GitHub

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=firefly
DB_USERNAME=firefly
DB_PASSWORD=<password>
DB初期化

3行コマンド打つだけなんですがなかなか通らず苦労しました。

  • php artisan migrate:refresh --seed
  • php artisan firefly-iii:upgrade-database
  • php artisan passport:install
masashi@nginx-php-fastcgi:/var/www/firefly/firefly-iii$ php artisan migrate:refresh --seed

In StreamHandler.php line 108:
                                                                                                               
  The stream or file "/var/www/firefly/firefly-iii/storage/logs/ff3-cli-2019-11-16.log" could not be opened:   
  failed to open stream: Permission denied                                                                     
                                                                                                               

www-dataユーザなので私のユーザでは権限がないというエラーです。
しかしsudoでやろうとまた別のエラーが出ます。

masashi@nginx-php-fastcgi:/var/www/firefly/firefly-iii$ sudo php artisan migrate:refresh --seed 

In Connection.php line 664:
                                                                                                               
  could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_n  
  ame = migrations and table_type = 'BASE TABLE')                                                              
                                                                                                               

In PDOConnection.php line 31:
                         
  could not find driver  
                         

In PDOConnection.php line 27:
                         
  could not find driver  
                         

これもissueがありました。
Initialize the database not working · Issue #2628 · firefly-iii/firefly-iii · GitHub

sudoってこんな使い方も出来たんですね。

また、databaseやDBのユーザ作ってなかったというミスもここで回収済みです。

masashi@nginx-php-fastcgi:/var/www/firefly/firefly-iii$ sudo -u www-data php artisan migrate:refresh --seed

Migration table not found.
Migration table created successfully.
Migrating: 2016_06_16_000000_create_support_tables
Migrated:  2016_06_16_000000_create_support_tables (0.4 seconds)

...

Seeding: AccountTypeSeeder
Seeding: TransactionCurrencySeeder
Seeding: TransactionTypeSeeder
Seeding: PermissionSeeder
Seeding: LinkTypeSeeder
Seeding: ConfigSeeder
Database seeding completed successfully.

残りも同じ。

masashi@nginx-php-fastcgi:/var/www/firefly/firefly-iii$ sudo -u www-data php artisan firefly-iii:upgrade-database

Now seeding the database...
Nothing to migrate.
Seeding: AccountTypeSeeder
Seeding: TransactionCurrencySeeder
Seeding: TransactionTypeSeeder
Seeding: PermissionSeeder
Seeding: LinkTypeSeeder
Seeding: ConfigSeeder
Database seeding completed successfully.

...
+------------------------------------------------------------------------------+
|                                                                              |
| Thank you for updating to Firefly III, v4.8.1.8!                             |
| This version of Firefly III requires PHP7.3.                                 |
|                                                                              |
+------------------------------------------------------------------------------+

最後は何のコマンドかわかりませんが、エラー出たので2回やっときました。

masashi@nginx-php-fastcgi:/var/www/firefly/firefly-iii$ sudo -u www-data php artisan passport:install
Encryption keys already exist. Use the --force option to overwrite them.
Personal access client created successfully.
Client ID: 3
Client secret: fMmYdXV0mk0elxhcAX3MRpSnO1je7V1B82JpdGKd
Password grant client created successfully.
Client ID: 4
Client secret: HUNPYAh26Jab3dNn1myzHlYSZXCcyTQMdFKrZMMY
masashi@nginx-php-fastcgi:/var/www/firefly/firefly-iii$ sudo -u www-data php artisan passport:install --force
Encryption keys generated successfully.
Personal access client created successfully.
Client ID: 5
Client secret: pOkkEnXHYXHBzlSNjyibl2ANtuntUYKy6CXEVaqC
Password grant client created successfully.
Client ID: 6
Client secret: LPC6hwF8rMtbypIcPeMACbdXrXhDGMNutAh3vuiD

アクセス

ここまで終わったらアクセスできるよとなっているのでアクセスしてみます。

アクセスしたら/fifefly-iii/public/にDocument root変えろとあるので一応変更しました。
f:id:paloma69:20191117152942p:plain

もう一度アクセス。
f:id:paloma69:20191117153134p:plain

なにも表示されない…。

nginxのエラーログ確認します。

2019/11/16 11:46:11 [error] 1825#1825: *1 FastCGI sent in stderr: "PHP Parse error:  syntax error, unexpected '?
', expecting variable (T_VARIABLE) in /var/www/firefly/firefly-iii/vendor/zendframework/zend-diactoros/src/functions/marshal_uri_from_sapi.php on line 83" while reading response header from upstream, client: 192.168.0.12, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/nginx/php-fastcgi.sock:", host: "192.168.0.17"

fastcgiのモジュールが読み込めていないようです。
nginxのlocation ~ \.php$を修正。

ここの該当部分を丸々貼りました。

firefly-iii/nginx.conf at master · firefly-iii/firefly-iii · GitHub

2019/11/16 11:54:54 [error] 1914#1914: *1 open() "/var/www/firefly/firefly-iii/public/login" failed (2: No such file or directory), client: 192.168.0.12, server: , request: "GET /login HTTP/1.1", host: "192.168.0.17"

今度はloginディレクトリにアクセスできないというエラーです。

これもissueを参考にlocation /を変更しました。

nginx: 404 Not Found · Issue #108 · firefly-iii/firefly-iii · GitHub

たぶんこの辺りの設定が怪しかったのですがクエリを読み込めなかったのかな?

try_files $uri $uri/ /index.php?$query_string;

再アクセス

f:id:paloma69:20191117153256p:plain

!!!

ログインページが表示されました!

ユーザ登録してログイン。

情報を登録します。
日本円はあるんですが現時点では日本語対応はしていないようです。

f:id:paloma69:20191117153724j:plain

balance(預金)は仮で100万で設定します。

ようやくセットアップできました。
ダッシュボードはこんな感じです。

f:id:paloma69:20191117153851j:plain

どこまで使いこなせるかわかりませんが、家計簿つけていこうと思います。

Webサーバのチューニングって難しい

久しぶりにnginx触りました。
Turnkey Linuxのほうでfastphp用にカスタマイズされているので、
ファイルを探すのも少し苦労しました。

locationの設定1つとっても今回のアプリが動く動かないの話になってくるので
webサーバって難しいですね。

でもいろいろ勉強になったので今回やってみてよかったです。
issueを漁る経験もできましたw

長くなるので細かい話はまた別で書いてみようと思います。