As part of website development and hosting it is natural for our sites to be hosted on several different environments. These can be our laptops for local development, a testing server for customers to test changes on, or a production server.
For each of these environments we may require slightly different configurations for our servers. This could be our debug level, caching backends, or - of course - sensitive information such as database credentials.
To manage environment variables, as well as other server globals, the
Environment class provides
a set of APIs and helpers.
Sensitive credentials should not be stored in a VCS or project code and should only be stored on the environment in
question. When using live environments the use of
.env files is discouraged and instead one should use "first class"
If you do use a
.env file on your servers, you must ensure that external access to
.env files is blocked by the
Managing environment variables with
By default a file named
.env must be placed in your project root (ie: the same folder as your
composer.json) or the
parent directory. If this file exists, it will be automatically loaded by the framework and the environment variables
will be set. An example
.env file is included in the default installer named
Note: The file must be named exactly
.env and not any variation (such as
.env.mysite) or it will
not be detected automatically. If you wish to load environment variables from a file with a different name, you will
need to do so manually. See the Including an extra
.env file section below for more
Managing environment variables with Apache
You can set "real" environment variables using Apache. Please see the Apache docs for more information.
How to access the environment variables
Accessing the environment variables should be done via the
Environment::getEnv() will return
false whether the variable was explicitly set as
false or simply wasn't set at all. You can use
Environment::hasEnv() to check whether an environment variable was set or not.
Using environment variables in config
To use environment variables in
.yaml configs you can reference them using backticks. You can have multiple
environment variables within a single value, though the overall value must start and end with backticks.
ThisWillNotSubstitute: 'lorem `REGULAR_TEXT` ipsum'
Environment variables cannot be used outside of Injector config as of version 4.2.
Including an extra
Sometimes it may be useful to include an extra
.env file - on a shared local development environment where all
database credentials could be the same. To do this, you can add this snippet to your
Note that by default variables cannot be overloaded from this file; Existing values will be preferred over values in this file.
$env = BASE_PATH . '/app/.env';
$loader = new EnvironmentLoader();
Core environment variables
Silverstripe core environment variables are listed here, though you're free to define any you need for your application.
|The database class to use, MySQLPDODatabase, MySQLDatabase, MSSQLDatabase, etc. defaults to MySQLDatabase.
|The database server to use, defaulting to localhost.
|The database username (mandatory).
|The database password (mandatory).
|The database port.
|A suffix to add to the database name.
|A prefix to add to the database name.
|Set the database timezone to something other than the system timezone.
|Set the database name. Assumes the
$database global variable in your config is missing or empty.
|Boolean/Int. If defined, then the system will choose a default database name for you if one isn't give in the $database variable. The database name will be "SS_" followed by the name of the folder into which you have installed Silverstripe. If this is enabled, it means that the phpinstaller will work out of the box without the installer needing to alter any files. This helps prevent accidental changes to the environment. If
SS_DATABASE_CHOOSE_NAME is an integer greater than one, then an ancestor folder will be used for the database name. This is handy for a site that's hosted from /sites/examplesite/www or /buildbot/allmodules-2.3/build. If it's 2, the parent folder will be chosen; if it's 3 the grandparent, and so on.
|Absolute path to SSL key file (optional - but if set,
SS_DATABASE_SSL_CERT must also be set)
|Absolute path to SSL certificate file (optional - but if set,
SS_DATABASE_SSL_KEY must also be set)
|Absolute path to SSL Certificate Authority bundle file (optional)
|Custom SSL cipher for database connections (optional)
|Enable deprecation notices for this environment.
SS_ENVIRONMENT_TYPE must be set to
dev for deprecation notices to show.
|The environment type: dev, test or live.
|The username of the default admin. This is a user with administrative privileges.
|The password of the default admin. This will not be stored in the database.
|Baseline protection for requests handled by Silverstripe. Usually requires additional security measures for comprehensive protection. See Environment Types for caveats.
|If you define this constant, all emails will be redirected to this address.
|If you define this constant, all emails will be sent from this address.
|Relative path to the log file (see Logging and Error Handling).
|Path to secured assets - defaults to ASSETS_PATH/.protected
|Used for SQLite3 DBs
|IP address or CIDR range to trust proxy headers from. If left blank no proxy headers are trusted. Can be set to 'none' (trust none) or '*' (trust all)
|A comma deliminated list of hostnames the site is allowed to respond to
|The manifest cache to use (defaults to file based caching). Must be a CacheInterface or CacheFactory class name
|If set the .env file will be ignored. This is good for live to mitigate any performance implications of loading the .env file
|The URL to use when it isn't determinable by other means (eg: for CLI commands)
|Absolute path to SSL key file
|Absolute path to SSL certificate file
|Absolute path to SSL Certificate Authority bundle file
|Optional setting for custom SSL cipher
|Try to detect deployments through file system modifications and flush on the first request after every deploy. Does not run "dev/build", but only "flush". Possible values are
true (check for a framework PHP file modification time),
false (no checks, skip deploy detection) or a path to a specific file or folder to be checked. See DeployFlushDiscoverer for more details.
False by default.
|File storage used for the default cache adapters in Manifests, Object Caching and Partial Template Caching. It defaults to creating a sub-directory of PHP's built-in
sys_get_temp_dir(). Can be an absolute path (with a leading
/), or a path relative to the project root.