怎样优化Prestashop的性能

In order to properly benchmark your shop, 你需要检查PHP, Apache 和 MySQL的设置.

1) PHP 设置

尽量使用PHP版本 >=7, 它能很明显的提升Prestashop商城的速度!

如果你使用了 PHP-FPM, you have to check the pool configuration. 它通常保存在这个文件: /etc/php/7.x/fpm/pool.d/www.conf. 在这个文件里,最重要的设置是pm.max_children 设置. 它必须大于在bench中模拟的最大并发请求用户数

确保启用了Zend Opcache.

Note this configuration of PHP should be used in Production environments only.

Use the following settings to optimize the performances:

[Date]
date.timezone = UTC

[Session]
session.auto_start = Off

[PHP]
short_open_tag = Off
realpath_cache_size = 4096K
realpath_cache_ttl = 600
display_errors = Off

magic_quotes_gpc = off
; Increase this value if you are able to do it
memory_limit = 512M
max_execution_time = 300
max_input_time = 300
upload_max_filesize = 20M
post_max_size = 22M
; Increase this value if you work with products with a lot of combinations
max_input_vars = 20000
allow_url_fopen = on

[opcache]
opcache.enable_file_override = On
opcache.interned_strings_buffer=64
opcache.fast_shutdown=1
opcache.memory_consumption=256
opcache.max_accelerated_files=20000

; Extra-optimization https://symfony.com/doc/current/performance.html#don-t-check-php-files-timestamps
; opcache.validate_timestamps=0

2) Composer (Autoloading 优化)

The class loader used while developing the application is optimized to find new and changed classes. In production servers, PHP files should never change, unless a new application version is deployed. That’s why you can optimize Composer’s autoloader to scan the entire application once and build a “class map”, which is a big array of the locations of all the classes and it’s stored in vendor/composer/autoload_classmap.php.

Execute this command to generate the class map (and make it part of your deployment process too):

composer dump-autoload --optimize --no-dev --classmap-authoritative
  • --optimize dumps every PSR-0 and PSR-4 compatible class used in your application;
  • --no-dev excludes the classes that are only needed in the development environment (e.g. tests);
  • --classmap-authoritative prevents Composer from scanning the file system for classes that are not found in the class map.

If you install a new module in production, you need to execute again this command.

3) Apache 设置

如果你使用的是 PHP-FPM, 你应该启用apache mpm_event. 使用下面的配置 (去设置mpm_event.conf文件) should allow you to test up to 400 concurrent users:

   ServerLimit             16
   MaxClients              400
   StartServers            3
   ThreadLimit             64
   ThreadsPerChild         25
   MaxRequestWorkers       400
   MaxConnectionsPerChild  0

4) MySQL/MariaDB 设置

如果你使用的MySQL版本 < 8 或使用 MariaDB, 通过添加下面设置到/etc/mysql/my.cnf 文件来启用query cache:

query_cache_limit               = 128K
query_cache_size                = 32M
query_cache_type                = ON

另外一个重要的设置是:

table_open_cache                      = 1000
read_buffer_size                      = 2M
read_rnd_buffer_size                  = 1M
thread_cache_size                     = 80
join_buffer_size                      = 2M
sort_buffer_size                      = 2M
max_connections                       = 400
tmp_table_size                        = 32M
max_heap_table_size                   = 32M
table_definition_cache                = 1000
performance_schema                    = OFF

尝试将innodb_buffer_pool_size的值设置为 something greater than the size of your database on disk.

Before launching the benchmark, and after importing the data, it’s always great to launch an ANALYZE TABLE on all your database by running on your server:

mysqlcheck -a -A -uroot -pyour_password

5) 使用CDN

Always try to use a CDN to reduce the amount of hits on your server. Cloudflare是一个很好且免费的服务. You can use as well to minify your page or to easily enable SSL on your site.

6) 优化你的prestashop

在Prestashop后台的performances 选项下:

  • 如果你只有一台前端服务器或你的smarty cache与你的所有前端服务器在同一文件系统上, 那么不要启用 “Multi-Front synchronisation”
  • Caching type 使用”File System”, 不要用”MySQL” 类型
  • 启用Smarty cache
  • 如果你的MySQL (<8) / MariaDB 数据库放在同一机器上, 不要启用Cache from the "Cache" section, MySQL的 query cache会更高效.
    如果你还是想启用它, 如果你只有一个front, 使用APC, 如果你有多个front, 使用一个独立的中央memcached服务器(而不应该是localhost!)