本文转自: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. PowerDesigner常用功能介绍

    PowerDesigner常用功能:1:把SQL脚步导入PowerDesigner打开powerdesigner,选择File --> Reverse Engineer --> Datab ...

  2. JS学习笔记9_JSON

    1.JSON概述 JavaScript Object Natation,js对象表示法,(像XML一样)是一种数据格式,它与js有相同的语法形式 P.S.一点小历史:JSON之父是道格拉斯,<J ...

  3. 利用 Azure Devops 创建和发布 Nuget 包

    利用 Azure Devops 创建和发布 Nuget 包 原 Visual Studio Team Service ,简称 VSTS,能够创建 pipelines 管道以构建应用程序,并将其部署到任 ...

  4. C#基础——C#中问号的使用

    1. 可空类型修饰符(?): 引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空. 例如:string str=null; 是正确的,int i=null; 编译器就会报错. 为了使值 ...

  5. B/S FastReprot使用

    FastReport 交流群 群   号:554714044 前言 由于公司开发新产品,前后端分离.netcore +Angular ,之前C/S项目一直使用FastReport ,考虑到员工切换比较 ...

  6. .net图表之ECharts随笔04-散点图

    见图说话,修改参数option实现上图显示: 1. 共用参数title还有一个属性subtext,可以用来设置副标题 2. tooltip与toolbox也是共用属性 3. dataZoom是设置滚动 ...

  7. AEAI DP创建弹窗

    1 背景概述 在平时我们浏览页面时,经常会看见这样的应用情景,点击某个按钮或点击某个页面区域时,会弹出一个浮动窗口,像这类的功能,在一些开发的项目中很常见,笔者发现使用AEAI DP应用开发平台可以很 ...

  8. 2019/4/23 todolist

    近期的任务单子大概是这样吧 bjoi2019改完,写题解 hnoi2019改一些,写题解 找3道网络流写写 写一场agc,写题解 找2道简单计算几何写写

  9. 回车符与换行符问题——C语言

    回车符(carriage return,’\r’)与换行符 (line feed,’\n’) 从上面可以看出换行对应的ASCII码值是10,回车符对应的ASCII码值是13,需要注意的是用户按下回车键 ...

  10. cad2014卸载/安装失败/如何彻底卸载清除干净cad2014注册表和文件的方法

    cad2014提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装cad2014失败提示cad2014安装未完成,某些产品无法安装,也有时候想重新安装cad2014 ...