[转]magento 2 modes – 每种模式的特点及如何切换(翻译)
本文轉自: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 wwwhellomagento2
這樣我現在的位置在E:wwwhellomagento2下,這是我的 Magento 2 的根目錄。這個目錄下有app,bin等等文件。下面回到 CMD 輸入如下命令:
php binmagento list
就像下面這樣,
好了,上面的命令告訴你 Magento 系統中所有可用的命令。同理,上面要顯示當前的模式,命令是php binmagento deploy:mode:show
如果你出現錯誤提示php 不是內部或外部命令,也不是可運行的程序或批處理文件,可以去這里看看。
總結
以上是生活随笔為你收集整理的[转]magento 2 modes – 每种模式的特点及如何切换(翻译)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 系统测试常用测试方法介绍
- 下一篇: 马氏距离+匈牙利算法+卡尔曼滤波-by