dotenvx / phpdotenvx
the secure dotenv—from the creator of `dotenv`
Requires
- php: ^8.1
- ext-phpdotenvx: *
Requires (Dev)
- phpunit/phpunit: ^9.0|^8.0|^7.0|6.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The secure dotenv–from the creator of dotenv.
- Encrypt
- Commit
- Ship
Packagist · Quickstart · Changelog
Quickstart
In your project directory, install the native extension with PIE and the PHP package with Composer. Prebuilt extensions are available for non-thread-safe PHP 8.1–8.5 on macOS and glibc Linux, on x86-64 and ARM64.
pie install dotenvx/phpdotenvx-ext composer require dotenvx/phpdotenvx
Create these two files in the same directory.
# .env HELLO=World
<?php // index.php require __DIR__ . '/vendor/autoload.php'; Dotenvx\Dotenvx::createImmutable()->load(); echo 'Hello ' . getenv('HELLO') . PHP_EOL;
Run php index.php. The script prints Hello World.
Loading files
createImmutable() preserves existing variables. Use createMutable() to overwrite them. load() returns the injected values and makes them available through getenv(), $_ENV, and $_SERVER. Use load(strict: true) to raise on loading errors, or safeLoad() when a missing file is expected.
<?php require __DIR__ . '/vendor/autoload.php'; Dotenvx\Dotenvx::createImmutable(__DIR__ . '/.env.production')->load(strict: true);
An explicit path takes precedence. With the default settings and no path argument, the SDK checks DOTENV_FILE, then DOTENV_PATH, then looks for files named by exported private keys (for example, DOTENV_PRIVATE_KEY_PRODUCTION selects .env.production). Otherwise it loads .env from the working directory.
Encrypt and deploy
With the dotenvx CLI installed, run this in the example project to encrypt the values in .env.
dotenvx encrypt
Commit the encrypted .env with your code. Keep the generated .env.keys file out of version control. In production, set DOTENV_PRIVATE_KEY in your deployment environment using the value from .env.keys.
Run php index.php again. The SDK decrypts HELLO at runtime and the script still prints Hello World.
Design
Three widely used and proven primitives make up Dotenvx's design. Git, .env, and secp256k1.
We chose git because it is the best way to deliver digital goods. It is the container ship of the digital world. All code travels on its ships. Why not secrets? Why invent an inferior secrets delivery mechanism when the world class one is right there at your fingertips.
We chose .env because it is the open standard for loading secrets into code. It represents environment variables, the core primitive that all secrets end up injected into at runtime of code.
Lastly, we needed to choose an encryption algorithm. Encryption of the values inside the .env file would allow us to place those values with code, leveraging git's distribution advantages while still following the twelve-factor config – by separating the decryption key from the code (environment).
It was important that we choose a proven, simple, asymmetric standard with small keys. We chose secp256k1, battle-tested by Bitcoin for more than seventeen years. We made cryptography an opinionated choice so developers wouldn't have to. Encryption just worked.
The result is a system built entirely from primitives, developers already understand, and infrastructure they already use. Secrets can travel with code without becoming part of the code. Git distributes them, .env defines how apps consume them, and secp256k1 keeps their values unreadable. The kicker, all this works by setting a single private key on your infrastructure - no more risky async coordination of secrets, no more centralized downtime risk. Your secrets are always just there with your code, ready to be unlocked at runtime.