This is an ever-growing list of tips, tricks, and gotchas picked up while working on Magento 2 projects.

Magento 2 Image by Mark König on Unsplash

1. Fix the error: Environment variable “MAGENTO_CRYPT_KEY” must be set

This error appears if you haven’t added the env.php file to ./app/etc/ folder, or if that file is not correctly configured.

It’s just a plain file to connect to the database.

Solution: put the env.php file with correct credentials to ./app/etc/ folder.

The following credentials in env.php, need to be updated:

'db' => [
    'table_prefix' => '',
    'connection' => [
        'default => [
            'host' => 'localhost',
            'dbname' => readEnvVarOrDie('MAGENTO_DATABASE_NAME'),
            'username' => readEnvVarOrDie('MAGENTO_DATABASE_USERNAME'),
            'password' => readEnvVarOrDie('MAGENTO_DATABASE_PASSWORD'),
            'active' => '1',
            'driver_options' => [

            ]
        ]
    ]
]

There are some other settings that we can update in this file, but the ones in the default array are the most important. Here’s a sample update to the env.php file:

'db' => [
    'table_prefix' => '',
    'connection' => [
        'default => [
            'host' => 'localhost',
            'dbname' => 'somelocaldb',
            'username' => 'root',
            'password' => 'somepassword',
            'active' => '1',
            'driver_options' => [

            ]
        ]
    ]
]
.
.
.
'MAGE_MODE' => 'developer',

Note that this is also the location to edit the 'MAGE_MODE' from, for example, default, to, for example, developer.

2. When you buy web hosting, know that Magento 2 needs at least 2GB of RAM (4GB preferrable)

And preferrably, even 4GB of RAM is desirable. This is an important piece of information to keep in mind.

3. What do to when you need to sync images in Magento 2, from a remote server that has many GB and you need images fast

You could run rsync to copy the images from the remote server onto your own local machine, but if there are 10+ GB of images on the remote server, and you need to have them available locally immediately, what can you do?

Try running these two commands from the root of your Magento 2 project, locally:

php bin/magento config:set web/unsecure/base_media_url <https://remote-server.com/media/>
php bin/magento c:c

The second line’s c:c is an alias for cache:clean.

3. What’s the difference between cache:clean and cache:flush in Magento 2?

The two commands that we have are:

php bin/magento cache:clean
php bin/magento cache:flush

To remove old items from the cache, we can run the above two commands.

The c:c cleans only the cache that Magento is actually using (doesn’t clean disabled cache types).

The c:f command cleans all the cache storage.

Which one to use? We can first start with c:c, and if we still have some stubborn cache that won’t go away, we can run c:f.

4. Deploy static files to a web-accessible directory

To do this, we need to run the following command:

php bin/magento s:s:d -f -t <folder-name> -t <folder-name>

5. Inspect enabled modules

In the root of your project, simply run the following command:

php bin/magento module:status --enabled

6. Inspect disabled modules

In the root of your project, run:

php bin/magento module:status --disabled

7. Upload a logo image for a Magento 2 store from the backend

Here are the steps:

  1. In the admin dashboard, navigate to Content, then Configuration.
  2. Find the store view you want to update and click Edit in the Design Configuration page’s Action column.
  3. Twirl-open the Header section
  4. Click the Upload button and locate your image file
  5. Once done, click the Save Configuration button