Magento 2 的每种模式的特点及如何切换

在开始 Magento 2 使用之前,需要了解一些基本的东西,其中之一就是系统可用的模式。

  • 每种模式的主要特点
  • 如何切换模式
  • 如何显示当前的模式

Magento 2 总共有三种主要模式:developer(开发者模式), production(生产模式), default(默认模式)

还有一个 maintenance (维护)模式,该模式运行方式很特别,他将完全阻止对系统的访问。

Magento 2 系统模式的主要特点

Developer Mode

当你正在开发代码的时候,你应该选择开发者模式,为什么呢?

  • 可以看到错误信息
  • 每次请求都会生成静态文件(static view files)
  • 没有使用缓存
  • 立刻可以看到变化
  • 会在浏览器中显示未捕获的异常
  • 在错误处理程序中抛出异常
  • Exceptions are thrown whenever an event subscriber cannot be invoked(每当事件订阅服务器不能调用时,则将引发异常)
  • 该模式下 var/report 中的系统日志非常详细
  • 显示自定义的 X-Magento-* HTTP 请求和响应标头

该模式会降低性能,所以在生产环境中不该使用它。

Production Mode

部署到生产环境中后,你就应该使用生产模式,他是面向客户的,为什么呢?

  • 性能最好
  • 错误会记录到文件系统中,绝对不向客户展示错误。
  • Static view file materialisation is disabled
    • 这意味着 static view file 不会在每次需要时重新生成,他们已经通过 CLI 命令部署到 pub/static 文件夹下
    • 对 view files 的任何更改都要通过 CLI deploy 才会有效果
    • view files 已经被部署到 pub/static ,这个文件夹下只需要 read-only 权限,这样更安全
  • Magento docroot 只有 read-only 权限

developer 模式不应该用在生产环境中。 So, when you need to make changes, you should instead generate static content in development and then use the deployer.php tool to push changes to production

Default Mode

这是一个 fallback 模式,如果没有指定其他模式,就会使用默认模式。

  • 错误被记录到 var/reports 中,但不会展示给用户
  • Static view files are materialized and then cached
    • view files 的变化不会有效直到原来生成的 static view files 被清除了
  • 隐藏自定义的 X-Magento-* HTTP 请求和响应标头
  • 这种模式没有为生产做最好的优化

Maintenance Mode

这是 Magento 2 预置的一个功能。当你在给系统升级或做其他改变的时候,你不想让用户使用网站,这时候会返回给用户 503 提示。

Bootstrap::assertMaintenance() 方法控制该模式,你可以通过创建一个标识文件(var/.maintenance.flag)来启用该模式。

You can specify a group of people to have access to the site while this mode is employed by placing the associated IPs in var/.maintenance.ip.

如何切换模式

CLI 命令

magento deploy:mode:set [mode] [-s|--skip-compilation]

[mode] 不可少,可以是developer 或者 production
--skip-compilation 是可选的,如果你想在 production 模式的时候跳过 code compilation

Web Server Environment

(这种方式博主没有试过,暂时不翻译,以后试验过理解了再补上)
Apache web servers with mod_php support this method. The environment variables can be set in the main apache configuration or in the .htaccess.

If you are using an Apache system you can do the following:

  • Open Apache
  • Open the .htaccess file
  • Use the MAGE_MODE system environment variable to specify the mode as follows:
SetEnv MAGE_MODE [mode]

[mode] is required; it can be either default, developer or production

After setting the mode you will need to restart the web server for it to take effect.

php-fpm Environment

You can specify the mode in the php-fpm config or in the system environment in which the php-fpm is started.

In the php-fpm config file, the value can be set as follows:

env[MAGE_MODE]=[mode]

[mode] is required; it can be either default, developer or production

显示当前模式

CLI 命令:

magento deploy:mode:show

然后你会看到如下信息:

Current application mode: [mode].

补充:Magento 的命令怎么用

以 windows 下的 wamp 环境为例,请打开 CMD (命令提示符)
然后切换到 Magento 的根目录

e:
cd www\hellomagento2

这样我现在的位置在 E:\www\hellomagento2 下,这是我的 Magento 2 的根目录。这个目录下有 app,bin 等等文件。下面回到 CMD 输入如下命令:

php bin\magento list
magento 2 cmd

就像下面这样,

好了,上面的命令告诉你 Magento 系统中所有可用的命令。同理,上面要显示当前的模式,命令是 php bin\magento deploy:mode:show

如果你出现错误提示php 不是内部或外部命令,也不是可运行的程序或批处理文件,可以去这里看看。

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用*标注