How to run Grocy on Platform.sh

Goal

Grocy is an awesome app one can use to manage groceries, food stock, recipes, chores and more. It’s like a personal ERP, I am using it to hoard responsibly without wasting food and other household items.

Here’s how it looks like:

Screenshot%20from%202020-03-16%2017-02-06

It is very easy to run Grocy on Platform.sh, and I will show you here what it takes. You can always fork this run-ready repo, and have it running in seconds.

Assumptions

You will need:

  • Platform.sh project, it can be development sized one too.

Steps

Clone your Platform.sh project locally first:

platform get <project id>

It should be fresh and empty project. Now, let’s add Grocy as submodule so we can upgrade easily when new version is released:

cd <project id>
git submodule add https://github.com/grocy/grocy.git
git submodule init
git submodule update
cd grocy
git checkout v2.6.1
cd ..
git add .
git commit -am 'Added Grocy 2.6.1'

OK, latest Grocy is ready. Now, let’s add some info so Platform.sh knows how to run it. Grocy itself is not using any additional services, all the data is kept in a file, so let’s create empty .platform/services.yaml file:

# No need for services

Routes can be default ones, have this in .platform/routes.yaml:

"https://{default}/":
    type: upstream
    upstream: "app:http"

"http://{default}/":
    type: redirect
    to: "https://{default}/"

Good. Now, let’s create .platform.app.yaml. You can take complete file from here, but here we’ll cover it section by section.

Grocy, at the time this is written, needs PHP 7.2 so let’s use that:

name: 'app'
type: 'php:7.2'

It keeps all the data in single file with db extension it searches for in data/ directory. So, let’s give it writable permanent mount there:

disk: 512

mounts:
    '/data':
        source: local
        source_path: 'data'

For build it only requires yarn around, so let’s get it:

build:
    flavor: none

dependencies:
    nodejs:
        yarn: "*"

Now, this is the build hook:

hooks:
    build: |
        set -e
        mkdir -p www

        cd www
        ln -s ../data/data data
        cd ..

        mv grocy/public www
        mv grocy/controllers www
        mv grocy/helpers www
        mv grocy/localization www
        mv grocy/middleware www
        mv grocy/migrations www
        mv grocy/publication_assets www
        mv grocy/services www
        mv grocy/views www
        mv grocy/composer.* www
        mv grocy/.yarnrc www
        mv grocy/yarn.* www
        mv grocy/*.php www
        mv grocy/*.json www

        cd www
        composer install
        yarn install

This results with built app in www sub-directory. Notice the trick we did with the data directory. By Grocy’s own installation documentation, /data directory keeps cache and plugins as well (structure is provided in the repo of the app itself), not just the data created by user. So we link it to the permanent mount and then copy the provided plugins in the deploy hook:

hooks:
    deploy: |
        set -e
        mkdir -p data/data/viewcache
        cp -rp grocy/data/* data/data
        cp config.php data/data

Deploy hook also provides configuration file config.php. You can look at the defaults, but at least you should disable PHP’s error reporting since it breaks session management:

<?php

error_reporting(E_ERROR | E_PARSE);

The only thing that’s left is describing how to serve the application, and that’s fairly easy:

web:
    locations:
        '/':
            root: 'www/public'
            expires: 5m
            passthru: '/index.php'
            allow: false
            rules:
                '\.(jpe?g|png|gif|svgz?|css|js|map|ico|bmp|eot|woff2?|otf|ttf)$':
                    allow: true
                    expires: 2w

And that is it, you’re now ready to run Grocy and manage your household like a proper company.

2 Likes