Banner图压缩
pub/media/wysiwyg 文件夹下的图片手动压缩并替换
JS、CSS设置优化
Stores->Configuration->Developer-> JavaScript Settings
Merge JavaScript Files: Yes
Enable JavaScript Bundling: Yes
Minify JavaScript Files: Yes
Stores->Configuration->Developer-> CSS Settings
Merge CSS Files: Yes
Minify CSS Files: Yes
模板压缩
Stores->Configuration->Advanced->Developer->Template Settings->minify HTML->Yes
系统后台开启缓存
System->Cache Management->select all, enable, submit-> Flush Cache
使用gzip压缩
根目录下.htaccess 文件,定位至IfModule mod_deflate.c>块之间,将注释行打开
############################################
## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip
# Insert filter on all content
###SetOutputFilter DEFLATE
# Insert filter on selected content types only
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
利用Apache mod_expires 与 mod_headers 实现文件缓存
利用mod_expires,在.htaccess中添加
ExpiresActive on #开启
ExpiresDefault A300 #默认
ExpiresByType text/html A300
ExpiresByType text/css A2592000 # 30天
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType application/x-javascript A2592000
利用mod_headers,在.htaccess中添加
Header set Cache-Control "max-age=604800, public"
Header set Cache-Control "max-age=18000, public, must-revalidate"
Header set Cache-Control "max-age=3600, must-revalidate"
更换生成模式
Magento2 有三种模式:default、developer 和 production。默认是 default 模式,而 default、developer 模式都不是为生产环境进行优化的。所有将Magento切换成Production模式。
查看所有命令:php bin/magento list
模式查看:php bin/magento deploy:mode:show
切换生产模式:php bin/magento deploy:mode:set production
运行命令:
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy
如果此时magento报错,无法访问,可尝试运行php bin/magento maintenance:disable
var 目录挂载内存
mount -t tmpfs -o size=100M,mode=0777 tmpfs var
MySQL 增加innodb_buffer_pool_size大小
innodb_buffer_pool_size 设置为RAM的70-80%
innodb_buffer_pool_size = 48G
PHP memory_limit增加
php.ini memory_limit 至少设置成1GB
memory_limit=1G
图片压缩自动化
apt-get install optipng // 压缩png
apt-get install jpegoptim // 压缩jpeg
apt-get install gifsicle // 压缩gif
#!/bin/bash
find ./media -iname '*.gif' -exec sh -c 'gifsicle -b -O3 -o "{}.out" "{}"; mv "{}.out" "{}"' \;
find ./media -iname '*.png' -exec optipng -o5 -keep -preserve '{}' \;
find ./media -type f -iname '*jpg' -exec sh -c 'jpegtran -outfile "{}.out" -optimize "{}"; mv "{}.out" "{}"' \;
在pub 目录下运行以上shell 程序
注意
进行修改之前请注意文件备份。
var 目录挂载内存、MySQL 增加innodb_buffer_pool_size大小、PHP memory_limit增加和图片压缩自动化尚未进行实践。
参考网址
How to speed up Magento 2. Maximum Performance
利用Apache mod_expires 与 mod_headers 实现文件缓存及mod_deflate压缩输出
magento性能优化
magento 站内优化和站外优化详解
7种方法让你的Magento 2主题模板速度加快
Magento 2 完整系列教程
magento2开发教程no3-如何创建一个模块module