Deploying Magento 2 with Redis on Platform.sh


At 5 weeks ago I a friend showed me a new method to host Magento using a PAAS platform, the infra technical knowledge and commands used are less required to you be more productive focusing in your application, so this solution will fix your issues related to infrastructure optimizing your workflow. Using my technical overview I see like the best feature the configurable micro-container architecture, Platform.sh uses it to run each system component, such as the web server, a PHP application, a database, or a search-engine. Like I’m working in many Magento 2 projects recently I resolve to write this article explaining how to configure your Magento 2 + Redis + Platform.sh.

Prerequisites

  • Magento 2 installed locally using composer.
  • Platform.sh account created.

1. Install Redis and Platform.sh

Access your project folder and run these commands below.

    composer require predis/predis
    composer require platformsh/magento2-configuration
    composer update

2. Configuring the platform

Give a name to your project and choose the Import your existing code option.

In your terminal, add the remote to your git project :

git remote add platform xxx@git.eu.platform.sh:xxx.git

Before you can push to the Platform.sh repository, you need to configure your account’s ssh keys except if you logged in through GitHub and you’re GitHub have already been imported.

3. Configuring the platform

The first step is to get the Platform.sh cli tool :

curl -sS https://platform.sh/cli/installer | php

Launch the platform tool and get your project id :

platform

Once you get your id, initialize the project in your current git folder :

platform local:init --project xxx

Your project is now dispatched in three folders : builds, repository & shared.

4. Configuring files

Here are the files that we need create or change, these files were used in this official example from Platform.sh on Github.


.platform/routes.yaml

.platform/services.yaml

.platform.app.yaml

auth.json

composer.json

magento-vars.php

php.ini


in .platform.app.yaml we have the basic configuration of our application (we call it mymagento), saying this is a Composer based application, that we depend on a database called database and that we what to run a build script and a deploy script during deployment.. and also set up some crons.

In .platform/routes.yaml we just say that we will redirect www to the naked domain, and that the application that will be serving HTTP will be the one we called php.

In .platform/services.yaml we say we want a MySQL instance, a Redis and a Solr. That would cover most basic Magento needs, right?

The composer.json will fetch the Magento 2, and some configuration scripts to prepare your application for Platform.sh.

Make sure you add your Magento credentials to the auth.json file and that those credentials can get you access to Magento Enterprise Edition.

Commit those configurations :

git add .
git commit -m "Add platform config files"

Check that the project is building by executing :

platform local:build

5. Magento installation

We now need to copy our app/etc/* files onto the newly created environment :

scp -r  app/etc/* xxx-master@ssh.eu.platform.sh:/app/app/etc/

SSH into your environment :

platform environment:ssh

Get your MySQL credentials :

echo $PLATFORM_RELATIONSHIPS| base64 --decode

Adapt the Magento CLI install command with your MySQL details and base-url:

cd public
./bin/magento setup:install \
--admin-user="niels" \
--admin-email="your@email" \
--admin-password="YourPassword999" \
--base-url="http://xxx.eu.platform.sh" \
--db-host="xxx" \
--db-name="main" \
--db-user="user" \
--use-rewrites=1 \
--session-save="db" \
--admin-use-security-key=0 \
--timezone="Europe/Paris" \
--admin-firstname="Your Name" \
--admin-lastname="LastName" \
--cleanup-database

Remember the Magento Admin URI given at the end of the process !

Magento running on Platform.sh

6. Redis configuration

Connect to your environment (ssh or scp) and edit the file app/etc/env.php. Add the cache array to the existing configuration. Don’t forget to edit the IP adresses found in the $PLATFORM_RELATIONSHIPS variable.

'cache' =>
array (
    'frontend' => array(
        'default' => array(
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => array(
                'server' => 'xxx.xxx.xxx.xxx',
                'port' => '6379',
                'persistent' => '',
                'database' => '0',
                'password' => '',
                'force_standalone' => '0',
                'connect_retries' => '1',
                'read_timeout' => '10',
                'automatic_cleaning_factor' => '0',
                'compress_data' => '1',
                'compress_tags' => '1',
                'compress_threshold' => '20480',
                'compression_lib' => 'gzip',
                'use_lua' => '0'
            )
        ),
        'page_cache' => array(
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => array(
                'server' => 'xxx.xxx.xxx.xxx',
                'port' => '6379',
                'persistent' => '',
                'database' => '0',
                'password' => '',
                'force_standalone' => '0',
                'connect_retries' => '1',
                'lifetimelimit' => '57600',
                'compress_data' => '0'
            )
        )
    )
),

On the environment prompt, clear all the Magento caches :

./bin/magento cache:clean

Redis should be now fully working. You can test it through :

redis-cli -h xxx.xxx.xxx.xxx
> INFO keyspace

References

Platform.sh Magento 2 Example

First Look at Platform.sh

Platform.sh Magento

Platform.sh Github

Deploying to Platform.sh

Platform.sh Documentation

Deploying Magento 2 on Platform.sh