Advanced Platform.sh configurations

The basic set-up let’s you easily set-up a Platform.sh project running your Sylius application. It should give you an environment suitable for testing Platform.sh in combination with Sylius.

In this guide additional tips will be given in order to benefit in a production environment.

Keep sessions between deployments

The default configuration saves PHP sessions into /tmp/sessions. Platform.sh functions in such way that each deployment spins up a new container instance and therefore the temporary folder holding sessions will be gone.

In order to save the PHP sessions on disk, the following steps need to be followed:

  • In platform.app.yml add the following under the mount property:
mount:
    "/app/sessions": "shared:files/sessions"
  • In the app/config/parameters_platform.php replace the session path:
ini_set('session.save_path', '/app/app/sessions');

Alternatively you can use a php.ini` file in the root of your project:

session.save_path = "/app/app/sessions"

Let Platform.sh generate your application secret:

The application secret is used in several places in Sylius and Symfony. Platform.sh allows you to deploy an environment for each branch you have, and therefore it makes sense to have a secret automatically generated by the Platform.sh system.

Add the following lines to app/config/parameters_platform.php to make use of the variables Platform.sh offers:

if (getenv('PLATFORM_PROJECT_ENTROPY')) {
    $container->setParameter('secret', getenv('PLATFORM_PROJECT_ENTROPY');
}

Use Redis for Doctrine caching:

Want to use the metacache, query cache or result cache Symfony and Doctrine have to offer? It comes with a caveat. Platform.sh doesn’t allow you to connect to all your services yet from inside the build hook. The following tutorial will guide you through this and make use of Redis. In the default example Redis is already activated.

  • In your app/config/parameters.yml.dist add:
parameters:
    metacache_driver: []
    querycache_driver: []
    resultcache_driver: []
    redis_dsn: ~
    redis_host: ~
    redis_port: ~
  • In the app/config/parameters_platform.php file, under the part where the database credentials are set, add:
foreach ($relationships['redis'] as $endpoint) {
    $container->setParameter('metacache_driver', 'redis');
    $container->setParameter('querycache_driver', 'redis');
    $container->setParameter('resultcache_driver', 'redis');

    $container->setParameter('redis_dsn', 'redis://'.$endpoint['host'].':'.$endpoint['port']);
    $container->setParameter('redis_host', $endpoint['host']);
    $container->setParameter('redis_port', $endpoint['port']);
}

Tip

Your Redis connection credentials are now available, which you can also use for the default Symfony cache.

  • In your app/config/config_prod.yml file add:
doctrine:
    orm:
        metadata_cache_driver:
            type: "%metacache_driver%"
            database: 1
            host: "%redis_host%"
            port: "%redis_port%"
        query_cache_driver:
            type: "%querycache_driver%"
            database: 2
            host: "%redis_host%"
            port: "%redis_port%"
        result_cache_driver:
            type: "%resultcache_driver%"
            database: 3
            host: "%redis_host%"
            port: "%redis_port%"
  • If you want to empty the cache on deployment, adjust the deploy hook in .platform.app.yaml:
hooks:
    deploy: |
        rm -rf var/cache/*
        php bin/console --env=prod doctrine:cache:clear-metadata
        php bin/console --env=prod doctrine:cache:clear-query
        php bin/console --env=prod doctrine:cache:clear-result
        php bin/console --env=prod doctrine:migrations:migrate --no-interaction

Advanced web configuration including cache control headers:

Platform.sh allows granular control over cache control headers, but it’s not introducted in the default configuration.

The following configuration can be a starting point for your customisations. Replace the web property in .platform.app.yml with the following example and you have a good starting point:

# The configuration of app when it is exposed to the web.
web:
    locations:
        '/':
            root: "web"
            passthru: "/app.php"
            allow: true
            expires: -1
            scripts: true
        '/assets/shop':
            expires: 2w
            passthru: true
            allow: false
            rules:
                # Only allow static files from the assets directories.
                '\.(css|js|jpe?g|png|gif|svgz?|ico|bmp|tiff?|wbmp|ico|jng|bmp|html|pdf|otf|woff2|woff|eot|ttf|jar|swf|ogx|avi|wmv|asf|asx|mng|flv|webm|mov|ogv|mpe|mpe?g|mp4|3gpp|weba|ra|m4a|mp3|mp2|mpe?ga|midi?)$':
                    allow: true
        '/media/image':
            expires: 2w
            passthru: true
            allow: false
            rules:
                # Only allow static files from the assets directories.
                '\.(jpe?g|png|gif|svgz?)$':
                    allow: true
        '/media/cache/resolve':
            passthru: "/app.php"
            expires: -1
            allow: true
            scripts: true
        '/media/cache':
            expires: 2w
            passthru: true
            allow: false
            rules:
                # Only allow static files from the assets directories.
                '\.(jpe?g|png|gif|svgz?)$':
                    allow: true

Add default Sylius cronjobs:

Add the example below to your .platform.app.yaml file. This runs these cronjobs every 6 hours.

crons:
    cleanup_cart:
        spec: '0 */6 * * *'
        cmd: '/usr/bin/flock -n /tmp/lock.app.cleanup_cart bin/console sylius:remove-expired-carts --env=prod --verbose'
    cleanup_order:
        spec: '0 */6 * * *'
        cmd: '/usr/bin/flock -n /tmp/lock.app.cleanup_order bin/console sylius:cancel-unpaid-orders --env=prod --verbose'

Additional tips:

  • Platform.sh can serve gzipped versions of your static assets. Make sure to save your assets in the same folder, but with
    a .gz suffix. The gulp-gzip node package comes very helpful integrating saving of .gz versions of your assets.
  • Platform.sh comes with a New Relic integration.
  • Platform.sh comes with a Blackfire.io integration