环境配置和变量
¥Environment configuration and variables
Strapi 提供了特定的环境变量名称。在环境文件(例如 .env
)中定义它们将使这些变量及其值在你的代码中可用。
¥Strapi provides specific environment variable names. Defining them in an environment file (e.g., .env
) will make these variables and their values available in your code.
env()
实用程序可用于 检索环境变量的值 和 将变量转换为不同类型。
¥An env()
utility can be used to retrieve the value of environment variables and cast variables to different types.
此外,可以创建特定的 不同环境的配置。
¥Additionally, specific configurations for different environments can be created.
Strapi 的环境变量
¥Strapi's environment variables
Strapi 提供以下环境变量:
¥Strapi provides the following environment variables:
环境 | 描述 | 类型 | 默认值 |
---|---|---|---|
STRAPI_TELEMETRY_DISABLED | 不要将遥测使用数据发送到 Strapi | Boolean | false |
STRAPI_LICENSE | 用于激活企业版的许可证密钥 | String | undefined |
NODE_ENV | 应用运行的环境类型。production 启用特定行为(有关详细信息,请参阅 Node.js 文档) | String | 'development' |
BROWSER | 启动后在浏览器中打开管理面板 | Boolean | true |
ENV_PATH | 包含环境变量的文件的路径 | String | './.env' |
STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE 可选 | 应用的初始化区域设置(如果已安装 国际化 (i18n) 功能 并在内容类型上启用)(请参阅 生产环境中 i18n 的配置) | String | 'en' |
STRAPI_ENFORCE_SOURCEMAPS | 强制打包器发出源映射,这有助于调试管理应用中的错误。 | boolean | false |
FAST_REFRESH | (仅适用于 webpack) 使用 react-refresh 启用 "快速刷新" 以获得近乎即时的反馈,同时开发 Strapi 管理面板。 | boolean | true |
HOST | Strapi 服务器监听的地址 | String | 0.0.0.0 |
PORT | Strapi 服务器使用的端口 | Number | 1337 |
APP_KEYS | 用于签署 Cookie 和其他密钥信息的逗号分隔键 | String | auto-generated |
API_TOKEN_SALT | 创建 API 令牌 时使用的盐值 | String | auto-generated |
ADMIN_JWT_SECRET | 管理面板中使用的 JWT 令牌的密钥 | String | auto-generated |
JWT_SECRET | 用户和权限 功能生成的 JWT 令牌的密钥 | String | auto-generated |
TRANSFER_TOKEN_SALT | 数据管理 功能用于转移令牌的盐值 | String | auto-generated |
DATABASE_CLIENT | 要使用的数据库客户端(例如 sqlite ) | String | sqlite |
DATABASE_FILENAME | SQLite 数据库文件的位置 | String | .tmp/data.db |
在环境变量名称中添加 STRAPI_ADMIN_
前缀会将变量公开给管理前端(例如,可以通过 process.env.STRAPI_ADMIN_MY_PLUGIN_VARIABLE
访问 STRAPI_ADMIN_MY_PLUGIN_VARIABLE
)。
¥Prefixing an environment variable name with STRAPI_ADMIN_
exposes the variable to the admin front end (e.g., STRAPI_ADMIN_MY_PLUGIN_VARIABLE
is accessible through process.env.STRAPI_ADMIN_MY_PLUGIN_VARIABLE
).
.env
文件示例
¥Example .env
file
Strapi CLI 在创建新项目时会生成一个 .env
和一个 .env.example
文件。文件包含自动生成的安全密钥和类似以下内容的数据库设置:
¥The Strapi CLI generates an .env
and an .env.example
file when creating a new project. The files contain automatically-generated security keys and database settings similar to the following:
- .env.example
- .env
HOST=0.0.0.0
PORT=1337
APP_KEYS="toBeModified1,toBeModified2"
API_TOKEN_SALT=tobemodified
ADMIN_JWT_SECRET=tobemodified
TRANSFER_TOKEN_SALT=tobemodified
JWT_SECRET=tobemodified
ENCRYPTION_KEY=tobemodified
变量可能因项目创建时选择的选项而异。
¥The variables might differ depending on options selected on project creation.
# Server
HOST=0.0.0.0
PORT=1337
# Secrets
APP_KEYS=appkeyvalue1,appkeyvalue2,appkeyvalue3,appkeyvalue4
API_TOKEN_SALT=anapitokensalt
ADMIN_JWT_SECRET=youradminjwtsecret
TRANSFER_TOKEN_SALT=transfertokensaltvalue
ENCRYPTION_KEY=yourencryptionkey
# Database
DATABASE_CLIENT=sqlite
DATABASE_HOST=
DATABASE_PORT=
DATABASE_NAME=
DATABASE_USERNAME=
DATABASE_PASSWORD=
DATABASE_SSL=false
DATABASE_FILENAME=.tmp/data.db
环境配置
¥Environment configurations
可以使用以下命名和结构约定创建配置:./config/env/{environment}/{filename}
。当你需要针对特定环境的特定静态配置并且使用环境变量不是最佳解决方案时,这非常有用。
¥Configurations can be created with the following naming and structure conventions: ./config/env/{environment}/{filename}
. This is useful when you need specific static configurations for specific environments and using environment variables is not the best solution.
这些配置将合并到 ./config
文件夹中定义的基本配置中。该环境基于 NODE_ENV
环境变量,默认为 development
。
¥These configurations will be merged into the base configurations defined in the ./config
folder.
The environment is based on the NODE_ENV
environment variable, which defaults to development
.
当使用 NODE_ENV=production
启动 Strapi 时,它将加载 ./config/*
和 ./config/env/production/*
的配置。生产配置中定义的所有内容都将覆盖默认配置。与环境变量相结合,这种模式变得非常强大。
¥When starting Strapi with NODE_ENV=production
it will load the configuration from ./config/*
and ./config/env/production/*
. Everything defined in the production configuration will override the default configuration. In combination with environment variables this pattern becomes really powerful.
例如,使用以下配置文件将为你提供启动服务器的各种选项:
¥For instance, using the following configuration files will give you various options to start the server:
- JavaScript
- TypeScript
module.exports = {
host: '127.0.0.1',
};
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
});
export default ({ env }) => ({
host: '127.0.0.1',
});
export default ({ env }) => ({
host: env('HOST', '0.0.0.0'),
});
使用这些配置文件,服务器将根据传递的环境变量在各个端口上启动:
¥With these configuration files the server will start on various ports depending on the environment variables passed:
yarn start # uses host 127.0.0.1
NODE_ENV=production yarn start # uses host defined in .env. If not defined, uses 0.0.0.0
HOST=10.0.0.1 NODE_ENV=production yarn start # uses host 10.0.0.1
要深入了解如何在代码中使用环境变量,请参考以下指南:
¥To learn deeper about how to use environment variables in your code, please refer to the following guide: