Magento 2 tips and tricks for beginners
This is a list of Magento 2 gotchas for brand new Magento developers
By: Ajdin Imsirovic 20 December 2020
This is an ever-growing list of tips, tricks, and gotchas picked up while working on Magento 2 projects.
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
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
'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,
2. When you buy web hosting, know that Magento 2 needs at least 2GB of RAM
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
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.
c:c cleans only the cache that Magento is actually using (doesn’t clean disabled cache types).
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
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>
To be continued…