本文转自:https://www.hellomagento2.com/switch-magento-2-system-modes/

原文地址

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

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

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

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

Magento 2 系统模式的主要特点

  Developer Mode Production Mode Default Mode
Static File Caching(缓存静态文件)    
Exceptions Displayed(显示异常)    
Exceptions Logged(异常记录日志)  
Negative Performance Impact(负面的性能影响)  

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 系统中所有可用的命令。同理,上面要显示当前的模式,命令是 php bin\magento deploy:mode:show

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

[转]magento 2 modes – 每种模式的特点及如何切换(翻译)的更多相关文章

  1. mod_wsgi 的两种模式

    mod_wsgi 的两种模式 http://ssmax.net/archives/977.html http://www.cnblogs.com/yuxc/p/3555005.html mod_wsg ...

  2. javascript 创建对象的7种模式

    使用字面量方式创建一个 student 对象: var student = function (){ name : "redjoy", age : 21, sex: women, ...

  3. javascript面向对象系列第二篇——创建对象的5种模式

    × 目录 [1]字面量 [2]工厂模式 [3]构造函数[4]原型模式[5]组合模式 前面的话 如何创建对象,或者说如何更优雅的创建对象,一直是一个津津乐道的话题.本文将从最简单的创建对象的方式入手,逐 ...

  4. javascript创建对象的几种模式

    在js中有几种模式可以创建对象,通过对象操作所包含的属性与方法. 一般来说,构造函数名称的第一个字母为大写字母,非构造函数名称的第一个字母为小写字母,当然,构造函数与一般函数唯一的区别只是调用的方式不 ...

  5. SQL Server 2012复制教程以及复制的几种模式

    简介 SQL Server中的复制(Replication)是SQL Server高可用性的核心功能之一,在我看来,复制指的并不仅仅是一项技术,而是一些列技术的集合,包括从存储转发数据到同步数据到维护 ...

  6. git push :推送本地更改到远程仓库的三种模式

    摘要:由于在git push过程中,no-fast-forward 的push会被拒绝,如何解决git push失败的问题?这里面有三种方法,分别会形成merge形式的提交历史,线性形式的提交历史,覆 ...

  7. App开发三种模式

    APP开发三种模式 现在App开发的模式包含以下三种: Native App 原生开发AppWeb App 网页AppHybrid App 混合原生和Web技术开发的App 详细介绍: http:// ...

  8. [转]VMware Workstation网络连接的三种模式

    经常要使用VMWare Workstation来在本地测试不同的操作系统,以前也搞不清楚网络连接三种模式,最近看了几篇文章才算明白.现总结如下: 1. VMware Workstation的虚拟网络组 ...

  9. Windows2003 IIS6.0支持32位和64位两种模式的设置方法

    IIS 6.0 可支持 32 位和 64 位两种模式.但是,IIS 6.0 不支持在 64 位版本的 Windows 上同时运行这两种模式.ASP.NET 1.1 只在 32 位模式下运行.而 ASP ...

随机推荐

  1. Excel表格公式大全[转]

    Excel技巧网_官方微博 作者: Excel技巧网_官方微博 2016-09-23 14:05:20 举报 阅读数:21219 ​1.查找重复内容公式:=IF(COUNTIF(A:A,A2)> ...

  2. 三、Kubernetes之深入了解Pod

      1.yaml格式的Pod配置文件内容及注解 深入Pod之前,首先我们来了解下Pod的yaml整体文件内容及功能注解. 如下: # yaml格式的pod定义文件完整内容: apiVersion: v ...

  3. Deque(队列)

    目录 Deque 概述 特点 常用方法 双向队列操作 ArrayDeque Deque 概述 一个线性 collection,支持在两端插入和移除元素.名称 deque 是"double e ...

  4. java的基本数据类型--四类八种

    java的数据类型 1.分为基本数据类型和引用数据类型 基本数据类型的分类:整数型: byte  占用一个字节 范围-128-127 short 占用两个字节  -2^15~2^15-1 int    ...

  5. Spring Boot中使用JdbcTemplate访问数据库

    本文介绍在Spring Boot基础下配置数据源和通过JdbcTemplate编写数据访问的示例. 数据源配置 在我们访问数据库的时候,需要先配置一个数据源,下面分别介绍一下几种不同的数据库配置方式. ...

  6. 解决 canvas 绘图在高清屏中的模糊问题

    解决 canvas 绘图在高清屏中的模糊问题 为什么模糊 CSS 像素是一个抽象单位(1 px),浏览器根据某种规则将 css 像素转化为屏幕需要的实际像素值. 在高清屏之前,屏幕上显示一个像素点需要 ...

  7. Postgres 的 deferrable

    仅 Postgres 支持 deferrable deferrable 即 推迟约束 一.定义字段时指定 定义:exam考试表里 subject_iddddd 字段关联了 subject 科目表的 i ...

  8. flask_mysql入库

    mysql 的入库和MongoDB的有一点点的区别 不过都很重要,都必须要掌握的技能, 现在我来演示一下mysql入库的过程: 首先  我们要导包,这是必不可少的一部分,都不用我说了吧 #导报 imp ...

  9. Vue2.5开发去哪儿网App 第五章笔记 下

    1. 多个元素或组件的过渡 多个元素的过渡: <style> .v-enter,.v-leace-to{ opacity: 0; } .v-enter-active,.v-leave-ac ...

  10. npm安装第三方库找不到“cl.exe”问题

    1.安装第三方库时找不到"cl.exe"的解决方法 安装 本地 remix时 出现错误(npm install remix-ide -g) 原因:remix 依赖的 python库 ...