PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/en/00_Getting_Started/03_Environment_Management.md

http://github.com/silverstripe/sapphire
Markdown | 122 lines | 93 code | 29 blank | 0 comment | 0 complexity | ec003f05c93bbc5b3bc5c94ec3bbaefd MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, CC-BY-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  1. ---
  2. title: Environment Management
  3. summary: How to configure your server environment for Silverstripe CMS
  4. ---
  5. # Environment management
  6. As part of website development and hosting it is natural for our sites to be hosted on several different environments.
  7. These can be our laptops for local development, a testing server for customers to test changes on, or a production
  8. server.
  9. For each of these environments we may require slightly different configurations for our servers. This could be our debug
  10. level, caching backends, or - of course - sensitive information such as database credentials.
  11. To manage environment variables, as well as other server globals, the [api:SilverStripe\Core\Environment] class
  12. provides a set of APIs and helpers.
  13. ## Security considerations
  14. Sensitive credentials should not be stored in a VCS or project code and should only be stored on the environment in
  15. question. When using live environments the use of `.env` files is discouraged and instead one should use "first class"
  16. environment variables.
  17. If you do use a `.env` file on your servers, you must ensure that external access to `.env` files is blocked by the
  18. webserver.
  19. ## Managing environment variables with `.env` files
  20. By default a file named `.env` must be placed in your project root (ie: the same folder as your `composer.json`) or the parent
  21. directory. If this file exists, it will be automatically loaded by the framework and the environment variables will be
  22. set. An example `.env` file is included in the default installer named `.env.example`.
  23. **Note:** The file must be named exactly `.env` and not any variation (such as `mysite.env` or `.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](#including-an-extra-env-file) section below for more information.
  24. ## Managing environment variables with Apache
  25. You can set "real" environment variables using Apache. Please
  26. [see the Apache docs for more information](https://httpd.apache.org/docs/current/env.html)
  27. ## How to access the environment variables
  28. Accessing the environment varaibles should be done via the `Environment::getEnv()` method
  29. ```php
  30. use SilverStripe\Core\Environment;
  31. Environment::getEnv('SS_DATABASE_CLASS');
  32. ```
  33. Individual settings can be assigned via `Environment::setEnv()` or `Environment::putEnv()` methods.
  34. ```php
  35. use SilverStripe\Core\Environment;
  36. Environment::setEnv('API_KEY', 'AABBCCDDEEFF012345');
  37. ```
  38. ### Using environment variables in config
  39. To use environment variables in `.yaml` configs you can reference them using backticks.
  40. ```yaml
  41. SilverStripe\Core\Injector\Injector:
  42. MyServiceClass:
  43. properties:
  44. MyProperty: '`ENV_VAR_HERE`'
  45. ```
  46. [info]
  47. Environment variables cannot be used outside of Injector config as of version 4.2.
  48. [/info]
  49. ## Including an extra `.env` file
  50. Sometimes it may be useful to include an extra `.env` file - on a shared local development environment where all
  51. database credentials could be the same. To do this, you can add this snippet to your `app/_config.php` file:
  52. Note that by default variables cannot be overloaded from this file; Existing values will be preferred
  53. over values in this file.
  54. ```php
  55. use SilverStripe\Core\EnvironmentLoader;
  56. $env = BASE_PATH . '/app/.env';
  57. $loader = new EnvironmentLoader();
  58. $loader->loadFile($env);
  59. ```
  60. ## Core environment variables
  61. SilverStripe core environment variables are listed here, though you're free to define any you need for your application.
  62. | Name | Description |
  63. | ---- | ----------- |
  64. | `SS_DATABASE_CLASS` | The database class to use, MySQLPDODatabase, MySQLDatabase, MSSQLDatabase, etc. defaults to MySQLDatabase.|
  65. | `SS_DATABASE_SERVER`| The database server to use, defaulting to localhost.|
  66. | `SS_DATABASE_USERNAME`| The database username (mandatory).|
  67. | `SS_DATABASE_PASSWORD`| The database password (mandatory).|
  68. | `SS_DATABASE_PORT`| The database port.|
  69. | `SS_DATABASE_SUFFIX`| A suffix to add to the database name.|
  70. | `SS_DATABASE_PREFIX`| A prefix to add to the database name.|
  71. | `SS_DATABASE_TIMEZONE`| Set the database timezone to something other than the system timezone.
  72. | `SS_DATABASE_NAME` | Set the database name. Assumes the `$database` global variable in your config is missing or empty. |
  73. | `SS_DATABASE_CHOOSE_NAME`| 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.|
  74. | `SS_DEPRECATION_ENABLED` | Enable deprecation notices for this environment.|
  75. | `SS_ENVIRONMENT_TYPE`| The environment type: dev, test or live.|
  76. | `SS_DEFAULT_ADMIN_USERNAME`| The username of the default admin. This is a user with administrative privileges.|
  77. | `SS_DEFAULT_ADMIN_PASSWORD`| The password of the default admin. This will not be stored in the database.|
  78. | `SS_USE_BASIC_AUTH`| Baseline protection for requests handled by SilverStripe. Usually requires additional security measures for comprehensive protection. See [Environment Types](/developer_guides/debugging/environment_types) for caveats.|
  79. | `SS_SEND_ALL_EMAILS_TO`| If you define this constant, all emails will be redirected to this address.|
  80. | `SS_SEND_ALL_EMAILS_FROM`| If you define this constant, all emails will be sent from this address.|
  81. | `SS_ERROR_LOG` | Relative path to the log file. |
  82. | `SS_PROTECTED_ASSETS_PATH` | Path to secured assets - defaults to ASSETS_PATH/.protected |
  83. | `SS_DATABASE_MEMORY` | Used for SQLite3 DBs |
  84. | `SS_TRUSTED_PROXY_IPS` | 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) |
  85. | `SS_ALLOWED_HOSTS` | A comma deliminated list of hostnames the site is allowed to respond to |
  86. | `SS_MANIFESTCACHE` | The manifest cache to use (defaults to file based caching). Must be a CacheInterface or CacheFactory class name |
  87. | `SS_IGNORE_DOT_ENV` | If set the .env file will be ignored. This is good for live to mitigate any performance implications of loading the .env file |
  88. | `SS_BASE_URL` | The url to use when it isn't determinable by other means (eg: for CLI commands) |
  89. | `SS_DATABASE_SSL_KEY` | Absolute path to SSL key file |
  90. | `SS_DATABASE_SSL_CERT` | Absolute path to SSL certificate file |
  91. | `SS_DATABASE_SSL_CA` | Absolute path to SSL Certificate Authority bundle file |
  92. | `SS_DATABASE_SSL_CIPHER` | Optional setting for custom SSL cipher |
  93. | `SS_FLUSH_ON_DEPLOY` | 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](api:SilverStripe\Core\Startup\DeployFlushDiscoverer) for more details.<br /><br />False by default. |