magento性能优化的教程(非常详细)

piaoling  2016-08-04 13:36:09

magento性能优化的教程(非常详细)

Magento是一套专业开源的电子商务系统,Magento设计得非常灵活,具有模块化架构体系和丰富的功能但有朋友会发现此模块用到了会发现非常的缓慢了,那么下面我们来看关于magento性能优化的例子。

前面优化

mod_deflate模块,将text、 css 和 javascript 先进行压缩再发送到浏览器。这样就会减少网络下载量,缩短等待时间,示例如下:

# 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 application/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 MSIE !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
 
    # enable resulting html compression
    php_flag zlib.output_compression on

启用Expires Headers

注意:这一配置在Litespeed servers无效
浏览器使用 Expires Headers来确定页面组件可以被缓存多长时间。静态的部件,如图像,可以设置为永不过期,但还是建议设置一个Expires Headers。要打开这个特性,可以取消注释对应的行并添加"ExpiresActive On" 如下所示:

ExpiresActive On
    ExpiresDefault "access plus 1 year"

禁用 ETags
ETags(Entity tags)是服务器和浏览器的一个功能,它用来判断浏览器缓存里的元素是否和原来服务器上的一致。ETags比last-modified date更具有弹性,它用一个独一无二的字符串来标识一个元素的版本。 要关闭它很简单,做法如下:
FileETag none

日志太大,执行如下sql:


truncate dataflow_batch_export;
truncate dataflow_batch_import;
truncate log_customer;
truncate log_quote;
truncate log_summary;
truncate log_summary_type;
 
truncate log_url;
truncate log_url_info;
truncate log_visitor;
truncate log_visitor_info;
truncate log_visitor_online;
 
truncate report_viewed_product_index;
truncate report_compared_product_index;
truncate report_event;
 
set foreign_key_checks = 0;
truncate index_process_event;
truncate index_event;
set foreign_key_checks = 1;


mysql配置优化,充分发挥你的硬件资源,下面的数值要根据你的配置调整

key_buffer_size = 512M
max_allowed_packet = 64M
table_cache = 512
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 2M
myisam_sort_buffer_size = 64M
tmp_table_size = 128M
query_cache_size = 96M
query_cache_type = 1
thread_cache_size = 8
max_connections = 400
wait_timeout = 300
5、将magento的var目录挂载到内存中,加快读取速度,如

mount -t tmpfs -o size=100M,mode=0777 tmpfs var

6、安装php加速器,如APC,XCACHE,eAccelerator,安装方法参考相关的网站

然而归于根本,最大的优化来自于对模板,对代码,对block的优化。

 

 

在Magento的优化中, Mysql的优化是很重要的,在Magento的官方网给出了有关Mysql的优化, 官方网并没有要我们把Mysql的存储引擎改为innodb, 但我觉的得这是很有必要的,当改成innodb后,还得把Mysql的配置文件修改修改下, 有时根据官方的文档来并不能成功, 这时就要看你的环境而定了, 下面给出我所配置的mysql文件给大家参考:

(继续)

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
default-character-set=utf8
#init_connect='SET NAMES utf8'
default-storage_engine = innodb

# To allow mysqld to connect to a MySQL Cluster management daemon, uncomment
# these lines and adjust the connectstring as needed.
#ndbcluster
#ndb-connectstring="nodeid=4;host=localhost:1186"

[client]
#default-character-set=utf8


[mysqld_safe]
log-error=/var/log/mysqld.log

[Magento]
max_connections = 1000
max_connect_error = 10
table_cache = 1024
max_allowed_packet = 16M
max_heap_table_size = 64M
sort_buffer_size = 8M
join_buffer_size = 8M
thread_cache_size = 8
thread_concurrency = 8
query_cache_size = 64M
query_cache_limit = 2M
tmp_tables_size = 64M
key_buffer_size = 32M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_max_extra_sort_file_size=10G
myisam_repair_threads = 1
myisam_recover
innodb_additional_mem_pool_size = 16M
innodb_log_buffer_size = 8M
innodb_log_file_size = 512M
innodb_log_files_in_group = 2
innodb_buffer_pool_size = 3G
innodb_data_file_path = ibdata1:3G;ibdata2:1G:autoextend
innodb_autoextend_increment=512

 Apache优化

这里主要讲的是,自己编译的Apache, 而如果是系统自带的话, Apache所需的模块, 系统会自动加载。

(继续)

#vi httpd.conf

ServerTokens OS

ServerRoot "/etc/httpd"

PidFile run /httpd.pid

Timeout 120

KeepAlive off

MaxkeepAliveRequests 100

KeepAliveTimeout 15

StartServers                100

MinSpareServers      100

MaxSpareServers    150

ServerLimit               256

MaxClient                  256

MaxRequestsPerChild  40000

Listen *:80

在这里还要一些必须的Apache模块

1,  mod_authz_host.so

2,  mod_expires.so

3,  mod_deflate.so

4,  mod_mime.so

5,  mod_dir.so

6,  mod_rewrite.so     这个模块很重要, 当设置Magento地址重写时,要用到

7,  mod_log_config.so

8, libphp5.so            你有可能用的是php4, 那就得改成libphp4.so

注意:

在安装好Apache后, 我们还能让Apache识别以php为后缀的文件

AddType application/x-httpd-php .php .phtml

好了, 到这就讲完了, 这章其实对Magento的优化不大, 但有些模块又不能少, 少了, Magento后台可能出问题

 

 

当我们安装好Magento时, 为了加快Magento的速度,我们一般还要做一些对于Magento服务的优化, 来提高Magento被访问的速度。 一般我们会从三个角度去考虑Magento的优化, 如: 安装Magento的系统; Magento模板中的代码; Magento所用到的数据库。 在这我们主要讲的是Magento系统的优化

php配置优化

Magento是在PHP环境下开发的, 所以优化PHP对Magento有着不小的影响,下面是Magento标准的PHP配置。

(继续)

一, 对于Magento所需的扩展, 只开启所需的就可以了

# Required extensions

extension=bcmath.so

extension=curl.so

extension=dom.so

extension=gd.so

extension=mcrypt.so

extension=memcache.so

extension=mhash.so

extension=pdo.so

extension=pdo_mysql.so

extension=mysql.so

extension=xmlwriter.so

下面的一些扩展并不需要开启, 我们可以把它们关闭

# Not  needed extensions from default setup

;;extension=dbase.so

;;extension=json.so

;;extension=mysqli.so

;;extension=pdo_sqlite.so

;;extension=sqlite.so

;;extension=wddx.so

;;extension=smlreader.so

;;extension=xsl.so

;;extesnion=zip.so

事无绝对, 当你的系统环境需要某个扩展时, 可以按照需要开启

下面所要讲的, 也是PHP中优化的重点, 将会讲到二种方法,二选一就行了, 切忌, 不能同时应用二种方案

一,安装APC扩展, APC, 用来优化PHP本身, 提高PHP的运行速度

# APC configuration apecifics if it is used

extension=apc.so

apc.shm_size=256

apc.num_files_hint=10000

apc.user_entries_hint=10000

apc.max_file_size=5M

二,安装eaccelerator加速器,功能跟APC类似, 在这就不多说了。

安装好eaccelerator后,将会产生eaccelerator模块

zend_extension="/usr/lib64/php/modules/eaccelerator.so"

eaccelerator.shm_size = "256"


简单的linux优化提示:

系统采用最小化安装,仅安装必需的软件包,不装GUI/X-Window等
停止或禁用无需使用的服务,比如cups
调优内核参数: (例如)
# echo '8192' > /proc/sys/fs/file-max
# echo '32768' > /proc/sys/fs/inode-max
# echo 268435456 > /proc/sys/kernel/shmall (SHMALL可用共享内存的总数量 单位:字节or页面[如果是字节,就和 SHMMAX 一样;如果是页面,ceil(SHMMAX/PAGE_SIZE)] )
# echo 268435456 > /proc/sys/kernel/shmmax
#ulimit -n 4096


编译 apache HTTP服务器

编译必须模块时使用静态编译的方式来取代DSO(动态共享对象)方式,并且禁用无需使用的模块:
./configure --prefix=/usr/local/apache2 --disable-status --disable-userdir --disable-threads
--disable-ipv6 --enable-modules='ssl so rewrite deflate headers expires'


启用 mod_deflate 模块

说明: DEFLATE 输出过滤器,允许服务器在将输出内容发送到客户端以前进行压缩,以节约带宽

打开 magento 根目录下的 .htaccess文件,定位到<IfModule mod_deflate.c></IfModule>块之间,
将 其中的注释行开启,例如:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    BrowserMatch MSIE !no-gzip !gzip-only-text/html
    SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    Header append Vary User-Agent env=!dont-vary
</IfModule>


启用 Header Expires 模块

打开 magento 根目录下的 .htaccess文件,定位到<IfModule mod_expires.c></IfModule>块之间,例如

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 month"
    ExpiresByType text/html "access plus 1 month"
    ExpiresByType text/plain "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
</IfModule>


禁用 Etag,启用 KeepAlive

打开 magento 根目录下的 .htaccess文件,移除FileETage None行的注释,如果在httpd.conf没有启用KeepAlive,可在此处新增几行,例如

FileETage None
KeepAlive On
MaxKeepAliveRequests 200
KeepAliveTimeout 5

magento后台设置

 

开启产品及产品分类的 Flat 功能。

开启这个功能后系统会为产品和产品分类的各个店铺视图建立独立的数据表以储存相关属性的数据,在编辑产品和分类信息的时候也会同时更新这些数据表。 前台要获得产品数据的时候,程序从这些独立的表中读取数据,而无需从多个 EAV 关联表中读取。这样做可以通过减少数据表的联立查询,有效地提高前台对产品数据的查询速度。

开启方法是到后台 System / Configuration > CATALOG / Catalog > Frontend 中,分别将 Use Flat Catalog Category 和 Use Flat Catalog Product 设成 Yes 并保存。

 

设置 CRON-JOB 定期清理 Log 信息。

Magento 的 Log 模块会将一些网站访问记录、用户登录信息、购物车修改情况等记录到数据库中。这些数据多了就会让数据库变得很臃肿,非常影响数据读取速度。因此我们应该开启 log 信息自动清理功能,并设置 CRON-JOB 定期进行清理。

后台开关路径为 System / Configuration > ADVANCED / System > Log Cleaning / Enable Log Cleaning。

 

关闭系统 Log 功能。

Magento 系统的 Log 功能会把一些运行信息或出错信息记录到硬盘中。这些信息一般只在网站开发阶段才会用到,并且可能造成频繁的 I/O 操作,影响程序运行效率。因此网站上线后应该把此功能关闭。

后台开关路径为 System / Configuration > ADVANCED / Developer > Log Settings / Enabled。

 

开启 Javascript 及 CSS 文件整合功能。

开启 Javascript 及 CSS 文件整合功能可以把通过 layout 加载的多个 Javascript 和 CSS 文件分别整合。这样做的好处是可以减少资源请求数量,加速下载速度,降低服务器负担。

为了更好地利用这项设置,开发的时候应该尽量把 Javascript 整理到脚本文件里并通过 layout 进行加载,这样开启设置后所有的脚本都能只通过一次请求便加载完毕了。

后台开关路径分别为 System / Configuration > ADVANCED / Developer > JavaScript Settings / Enabled 和 System / Configuration > ADVANCED / Developer > CSS Settings / Enabled。

 

为 Skin、Media、Javascript 设置 CDN 或者不同的子域名。

由于一般浏览器对同一域名的并发请求数量都有限制,这样就算页面上有很多资源需要加载,也只能一个个地从服务器中读取,无法做到同时加载多个资源,影响页面渲染效率。

后台设置路径分别为 System / Configuration > GENERAL / Web > Unsecure 及 System / Configuration > GENERAL / Web > Secure 下的 Base Skin URL、Base Media URL 和 Base JavaScript URL。

 

开启系统缓存。

可以说开启系统缓存是最有效的提速措施,因为开启后可以把初始化和逻辑计算的结果直接保存到缓存中,跳过很多重复的程序操作,最直接地节省程序运行时间和资源。

开启缓存的方法是到后台 System / Cache Management 页面的列表中勾选需要缓存的项目,再选择列表右上角 Actions 的 Enable 并点击 Submit 按钮。

 

关闭没有使用的原生模块。

Magento 页面初始化的时候会查找和合并所有组件的配置文件,并把这些组件的在数据库中的版本与配置文件的版本进行比较,进一步执行组件的更新操作。不管打开的页面 中是否会使用到,系统都会对所有开启的组件执行这些工作。显然,如果把项目中没有用到的模块也一起加入初始化是很浪费资源的,可以把不用的原生模块关闭 掉。

由于模块之间有所关联,不是每个模块都能做到真正独立,部分模块关闭后可能导致后台出现问题。为了方便开发,在这里整理了一些可以简单做到安全关闭的原生模块。

Write the code ,change the world!

类别 :  magento(258)  |  浏览(8237)  |  评论(1)
发表评论(评论将通过邮件发给作者):

Email:
userhead
2016-11-17 13:27:18

While utilizing Magento, you find Dedicated Certified Magento developers produce problems by neglecting the appropriate folder and file permission. The magento developer is known for altering its approval, so you might be required to update and install new products.


http://www.hiremagentoprogrammer.com/services/hire-magento-developer/