[转]magento性能优化的教程(很是详细)

本文转自:https://www.sypopo.com/post/kMQE8dERoV/javascript

前面优化php

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

# 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 \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
 
    # enable resulting html compression
    php_flag zlib.output_compression onhtml

启用Expires Headersjava

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

ExpiresActive On
    ExpiresDefault "access plus 1 year"mysql

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

日志太大,执行以下sql: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 \bMSIE !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 页面初始化的时候会查找和合并全部组件的配置文件,并把这些组件的在数据库中的版本与配置文件的版本进行比较,进一步执行组件的更新操做。无论打开的页面中是否会使用到,系统都会对全部开启的组件执行这些工做。显然,若是把项目中没有用到的模块也一块儿加入初始化是很浪费资源的,能够把不用的原生模块关闭掉。

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

相关文章
相关标签/搜索