Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Environmental variables

89 vues

Publié le

How do you handle the variables specific to the environment you use: Development, QA, Production, etc? Environmental Variables of course. But WHAT are they, WHY should you use them, WHERE do you put them, and HOW are they used. As an added bonus, Environmental Variables are not limited to PHP!

Publié dans : Technologie
  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

Environmental variables

  1. 1. Environmental Variables https://github.com/sketchings/example-env
  2. 2. What are Environments?
  3. 3. Software Environments ● Local ● Dev ● QA ● Staging ● Production
  4. 4. What are environmental variables? ● Tied to the environment ○ Not tied to a language ○ Not tied to an application ● Used for things that change based on the environment
  5. 5. How can you set them up? ● Apache/Nginx (System wide or Virtual Hosts) ● Hosting Company Dashboard ● Docker ● .htaccess ● Composer Package (.env)
  6. 6. Docker Environment: DB_SERVER: "db" DB_NAME: "env-test" DB_USERNAME: "env" DB_PASSWORD: "password" CONFIG_ENV: "development" S3_BUCKET: "docker bucket"
  7. 7. Using a Package PHP dotenv Loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically. https://github.com/vlucas/phpdotenv
  8. 8. .env File DB_SERVER="db" DB_NAME="env-test" DB_USERNAME="env" DB_PASSWORD="password" CONFIG_ENV="development" S3_BUCKET="dotenv bucket"
  9. 9. <?php require __DIR__ . '/vendor/autoload.php'; // can be a relative or absolute path // optional second parameter for filename, default is .env $dotenv = DotenvDotenv::createImmutable(__DIR__); if (getenv('CONFIG_ENV') != "production") { $dotenv->load(); }
  10. 10. //Immutability refers to if Dotenv is allowed to overwrite existing environment variables. If you want Dotenv to overwrite existing environment variables, use createMutable instead of createImmutable: $dotenv = DotenvDotenv::createMutable(__DIR__); Mutability
  11. 11. <?php require __DIR__ . '/vendor/autoload.php'; $dotenv = DotenvDotenv::createImmutable(__DIR__); if (getenv('CONFIG_ENV') != "production") { $dotenv->load(); } $dotenv->required( ['DB_SERVER', 'DB_NAME', 'DB_USERNAME', 'DB_PASSWORD'] );
  12. 12. What about Security? ● Never check .env files into source control ○ Same applies to sensitive configuration ● Use a secrets manager ○ https://www.phparch.com/magazine/2019/06/how-to-tame-your-data/
  13. 13. Alena Holligan ● Lead Software Engineer at Lumen Learning ● PHPDX User Group Leader ● Web Developer for 20 years ● Twitter @alenaholligan File: https://github.com/sketchings/example-env Slide and to rate this Talk: https://joind.in/talk/cf3cc

×