Yes, you can use an external configuration system like HashiCorp Vault, AWS Secrets Manager, Azure Key Vault, etc., instead of putting all secrets and connection details into .env. Laravel's default use of the .env file is for convenience and local development, but it’s not mandatory in production, especially for secrets management.
.env is bloating with multiple service credentials (MQTT, AMQP, DBs, Redis, Elasticsearch, etc.).Config::get() calls..env.You can integrate Vault (or any other system) during the application boot and override configuration values programmatically.
Edit your AppServiceProvider.php or make a custom SecretProviderServiceProvider and do something like:
public function register()
{
if (app()->environment('production')) {
$secrets = $this->getSecretsFromVault(); // pull from Vault/API
config([
'database.connections.mysql.username' => $secrets['db_user'],
'database.connections.mysql.password' => $secrets['db_pass'],
'cache.stores.redis.password' => $secrets['redis_pass'],
'queue.connections.rabbitmq.host' => $secrets['amqp_host'],
// etc.
]);
}
}
You can use Guzzle or a Laravel Vault package like:
ryanwinchester/vault-php.envYour .env can then contain only:
APP_KEY=...
APP_ENV=production
VAULT_TOKEN=...
VAULT_ADDR=https://vault.yourdomain.com
Some Laravel internals require values from .env before any config logic can run:
.env (or system-level ENV):APP_KEY – Needed before Laravel encryption works.APP_ENV, APP_DEBUG – Used in bootstrap/app.php.LOG_CHANNEL, LOG_LEVEL – Logging setup runs early.CACHE_DRIVER, QUEUE_CONNECTION – Drivers need early resolution..env is parsed before your app bootstraps.You cannot use Config::get() to replace those unless Laravel has already bootstrapped.
? A workaround: define custom placeholder values in .env like:
DB_USERNAME=__vault__
DB_PASSWORD=__vault__
Then during boot, if you detect __vault__, replace it with real ones.
.env for production..env values.AppServiceProvider or a custom bootstrapping class to inject secrets from Vault (or AWS Secrets Manager).Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.