Why Magento 2 is extremely slow?
-
05-10-2020 - |
Question
I used Magento 1.9 and it was okay.It wasnt as fast as a simple joomla site but i had many customers with no problem.
Now i wanted to upgrade my site to Magento 2,i bought a new host (same as i currently have) and installed Magento 2 without sample data and its a pain to use.Not just the admin,but the empty homepage as well take minutes to load and when i try to create an account i getting error that i "Resource Limit Is Reached"
I tried with Facebook FlashCache,OptimumCache and Cloudflare but nothing has changed even though the creators said Magento 2 is much faster than the first version.
I'd like to understand why they cant fix it after so many years and why so many people using it?
Is there any way fasten it up?
UPDATE:
I used mgt-commerce before and it was incredibly fast,took less than 2s to load but it cost too much: AWS Price + € 199.00
They offered php7 / HHVM , Varnish, Redis , CDN ,NGINX by default i guess thats why it was fast.
I had to buy Amazon WS account i don't know why i guess my site was actually hosted by Amazon.
Im looking for the same speed for a lower price. A VPS provider example.
I was looking around but found that every hosting provider claims they have Magento hosting. Siteground,Mochahost,Hostgator,Fastcomet etc.
Maybe there's a hosting provider who would install these caches to beat competitors.
PS: Sorry if my post seems to be silly for programmers i'd choosen economics,commerce and marketing at university unfortunately :(
Solution
There are multiple resources available to improve Magento 2 performance and all of them are pretty well documented.
Mode: Magento 2 has introduced modes, being the default one the slowest. Give the developer mode a try and always run your live store in production mode. More info here.
Cache: Varnish is supported out of the box, pretty easy to configure and use.
Redis: Redis is an optional backend cache solution to replace Zend_Cache_Backend_File
, which is used in Magento 2 by default. It can be used for session storage and page caching.
Memcache: Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. In Magento 2 it can be used for sessions.
PHP 7: Magento 2 is compatible with PHP 7 out of the box. There is a lot of research on how PHP 7 affects performance for good.
This is just the start, you should be able to fine tune your webserver and database server but that will heavily depend on your particular use case.
If you don't know how to do that yourself, there are plenty of companies specialized in Magento hosting.
For a pretty solid dev environment my personal recommendation is Paliarush' Vagrant or the VM I got from Magento for the courses. There is an official docker image coming soon.
OTHER TIPS
Magento runs reasonably well even in developer mode. It does need some initial configuration though.
First make sure Magento is set up correctly, assuming your on Ubuntu:
Use the latest build as Magento 2.2+ supports php 7.1 http://devdocs.magento.com/guides/v2.2/install-gde/prereq/php-ubuntu.html
sudo apt-get install php7.1 php7.1-imap php7.1-xml php7.1-dom php7.1-intl
If your running multiple php versions set 7.1 to the default via
sudo update-alternatives --set php /usr/bin/php7.1
Onto Magento
Set to Developer if not already
php -f bin/magento deploy:mode:show
php -f bin/magento deploy:mode:set developer
Check Magento's cache is enabled
php -f bin/magento cache:status
If not enabled (series of 1's)
php -f bin/magento cache:enable
Enable JS/CSS bundling
Stores > Configuration > Advanced > Developer
*Note the above menu item only shows up whilst in developer mode
Template Setttings
- Minify HTML = Yes
Javascript Settings
Merge JS files = Yes
Enable JS Bundling = Yes
Minify JS files = Yes
CSS Settings
- Merge CSS = Yes
- Minify CSS = Yes
Do a cache flush from here on in after every step flush the cache, this is your baseline for trying to improve the loading time.
php -f bin/magento cache:flush
Advanced bundling
This would be a post in itself follow guidelines from magento devdocs on how to enable this, this could further increase the frontend JS loading making it 3x as fast.
Use Redis
sudo apt-get update
sudo apt-get install build-essential tcl8.5
sudo apt-get install make
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
cd utils/
sudo ./install_server.sh
Edit /www/project/app/etc/env.php
'cache' =>
array (
'frontend' =>
array (
'default' =>
array (
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' =>
array (
'server' => '127.0.0.1',
'database' => '0',
'port' => '6379',
),
),
'page_cache' =>
array (
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' =>
array (
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1',
'compress_data' => '0',
),
),
),
),
Use PHP-FPM
apt-get install php7.1-fpm
a2enmod proxy_fcgi setenvif
a2enconf php7.1-fpm
sudo service php7.1-fpm restart
sudo service apache2 restart
Enable opcache in PHP
Edit /etc/php/7.1/apache2/php.ini find opcache.enable
opcache.enable=1
I would also recommend using a Cloud DB such as AWS RDS or other it will save you the headaches of configuring mysql.
If you now enable production mode
php -f bin/magento deploy:mode:set production
you should find it's running reasonably well.
Taking it further from here you can add Varnish, Switch to Nginx add a CDN for static files.
I had this problem too on a Macbook Pro (late 2016) with MAMP.
What I wanted to do was to create a Magento 2 theme. I had deactivated all caches and it took like 30s to reload a page (frontend and backend).
I activated all caches with the command "php magento cache:enable" and the site loads very fast. I can still work with Grunt and LESS without deactivating any cache type.
Magento 2 is not slow by default. You have to follow these steps to make it fast:
Server and System Requirements
Update Magento Version
Enable Varnish Cache
Configure Memcached
Enable Flat Categories and Products
Optimize Javascript and CSS
Content Delivery Network
Use lightweight theme
Bug-Free Extensions
Images Should be Fully Optimized
How do you figure Magento 2 is slow? My site which uses a very heavy theme scores a solid 93/100 in Page Speed and load times are 2 secs. I'd say get a better host for Magento because your web server plays a big part in the performance of your site. As for "default" mode being the slowest not true at all. I ran my store in default mode until I was ready to switch over to production mode and it scored a 91/100 in default mode and only went to a 93/100 after switching to production mode.
Get a better host and a better web server if you really want to use Magento 2.
for me this was the magic php -f bin/magento cache:enable
:-*