magento性能優化策略大全

 

magento的網站實在是太慢了,想了好多辦法,參考了好多資料,作了不少測試,總結一下大概的步驟以下php

(繼續)css



1、壓縮js,css代碼,若是有必要把全部的css,js分別綜合到一個文件中,並壓縮,緩存

2、清除magento模板中沒必要要的註釋,爲所使用到的圖片瘦身

3、 優化magento代碼,這個步驟工做量大,但多是效果顯著的步驟,不過前提是你很是熟悉magento,完全刪除不用的模塊,關閉沒用的block, 清除無效,無用的xml(解析xml很是昂貴的),在一個頁面中儘可能不要大量調用magento的image resize功能,很是消耗內存,除非你本身優化代碼。

4、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的優化。html

 

 

Magento的優化中, Mysql的優化是很重要的,在Magento的官方網給出了有關Mysql的優化, 官方網並無要咱們把Mysql的存儲引擎改成innodb, 但我覺的得這是頗有必要的,當改爲innodb後,還得把Mysql的配置文件修改修改下, 有時根據官方的文檔來並不能成功, 這時就要看你的環境而定了, 下面給出我所配置的mysql文件給你們參考:node

(繼續)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=512sql

 

這裏主要講的是,本身編譯的Apache, 而若是是系統自帶的話, Apache所需的模塊, 系統會自動加載。數據庫

(繼續)json

#vi httpd.conf緩存

ServerTokens OSapp

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系統的優化

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"