PHPackages                             nystudio107/dotenvy - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nystudio107/dotenvy

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nystudio107/dotenvy
===================

Speed up your production sites by ditching .env for key/value variable pairs as Apache, Nginx, and shell equivalents.

1.1.0(6y ago)326.9k1[1 PRs](https://github.com/nystudio107/dotenvy/pulls)MITPHPPHP &gt;=7.0.0

Since Feb 17Pushed 6y ago1 watchersCompare

[ Source](https://github.com/nystudio107/dotenvy)[ Packagist](https://packagist.org/packages/nystudio107/dotenvy)[ RSS](/packages/nystudio107-dotenvy/feed)WikiDiscussions v1 Synced 2mo ago

READMEChangelog (4)Dependencies (1)Versions (6)Used By (0)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/792d67663e7a1d292e7971c4311e8e4b45f3faf99d90ba8697e5a5fc29a34e7b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e7973747564696f3130372f646f74656e76792f6261646765732f7175616c6974792d73636f72652e706e673f623d7631)](https://scrutinizer-ci.com/g/nystudio107/dotenvy/?branch=v1) [![Code Coverage](https://camo.githubusercontent.com/9341d1a65445014ee5a5b22cfef19831cbcc449e32f9d6f00e6b3729e0bfd1c7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e7973747564696f3130372f646f74656e76792f6261646765732f636f7665726167652e706e673f623d7631)](https://scrutinizer-ci.com/g/nystudio107/dotenvy/?branch=v1) [![Build Status](https://camo.githubusercontent.com/27bb46b9530b90bcc1b2ccaa5bb5ddc64235bfbac3d0009aa6e07247c01f50cb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e7973747564696f3130372f646f74656e76792f6261646765732f6275696c642e706e673f623d7631)](https://scrutinizer-ci.com/g/nystudio107/dotenvy/build-status/v1) [![Code Intelligence Status](https://camo.githubusercontent.com/ef08068c5d34be78e48df97beffb59de61ab8e0425b6cc2b1d754aca68752ef6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e7973747564696f3130372f646f74656e76792f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d7631)](https://scrutinizer-ci.com/code-intelligence)

Dotenvy
=======

[](#dotenvy)

Speed up your production sites by ditching .env for key/value variable pairs as Apache, Nginx, and shell equivalents

Requirements
------------

[](#requirements)

PHP 7.0 or later, and a project that uses [Composer](https://getcomposer.org/)

Installation
------------

[](#installation)

To install this package, follow these instructions.

1. Open your terminal and go to your project:

    ```
     cd /path/to/project

    ```
2. Then tell Composer to require the package:

    ```
     composer require nystudio107/dotenvy

    ```

CraftQuest.io video: Injecting Environment Variables with Dotenvy
-----------------------------------------------------------------

[](#craftquestio-video-injecting-environment-variables-with-dotenvy)

[![CraftQuest.io video: Injecting Environment Variables with Dotenvy](https://camo.githubusercontent.com/974db0c2d35276243ac745f01ecb1d818018e6e41f77adc61ede149d075123ab/68747470733a2f2f692e76696d656f63646e2e636f6d2f766964656f2f3736393734333339302e6a70673f6d773d31393030266d683d3130363926713d3730)](https://craftquest.io/livestreams/injecting-environment-variables-with-dotenvy)

Dotenvy Overview
----------------

[](#dotenvy-overview)

Dotenvy is a small tool that takes the contents of your `.env` file, and outputs them in a format that can be pasted directly into an Apache server config, Nginx server config, shell CLI `.bashrc`, or Docker `Dockerfile`

Why? Because as per the [phpdotenv](https://github.com/vlucas/phpdotenv) documentation:

> phpdotenv is made for development environments, and generally should not be used in production. **In production, the actual environment variables should be set so that there is no overhead of loading the .env file on each request.** This can be achieved via an automated deployment process with tools like Vagrant, chef, or Puppet, or can be set manually with cloud hosts like Pagodabox and Heroku.

The `.env` file is meant to be a convenience to make things easier to change in local development environments.

What the [phpdotenv](https://github.com/vlucas/phpdotenv) package does is parse your `.env` file, and then call [putenv()](http://php.net/manual/en/function.putenv.php) to set each environment variable. This sets the [$\_ENV superglobal](http://php.net/manual/en/reserved.variables.environment.php) that your application can later read in via [getenv()](http://php.net/manual/en/function.getenv.php).

Using the technique described here, the exact same `$_ENV` superglobal gets set with your environmental variables, and are made available via the same `getenv()` function. The difference is that your webserver or CLI sets the variables directly, without having to parse the `.env` file.

Without question, this is a micro-optimization... and is unlikely to make a significant performance difference. But why add overhead for no reason?

This is a partial implementation of feature I've been hoping to have in Craft CMS core in some fashion: [Add `craft config/cache` as a console command](https://github.com/craftcms/cms/issues/1607)

Using Dotenvy
-------------

[](#using-dotenvy)

From your project's root directory that contains the `.env` and `/vendor` directory, do:

```
vendor/bin/dotenvy
```

If you're on Windows, do:

```
vendor/bin/dotenvy.bat
```

If your `.env` file lives somewhere else, you can pass in the directory to the `.env` file:

```
vendor/bin/dotenvy /path/to/some/dir/
```

Then **do not create** a `.env` file on your production environment, instead paste or insert via a deployment system the resulting file that Dotenvy generates for you.

In this way, the appropriate `.env` variables will be automatically injected by your Apache server, or Nginx server, or via CLI.

This means that the `.env` file no longer needs to be parsed on every request.

### Updating `.gitignore`

[](#updating-gitignore)

Make sure you `.gitignore` all of the `.env*` files with a line like this in your root project `.gitignore` file:

```
.env*

```

...to ensure that none of your secrets in the generated `.env*` files are checked into git. Note the trailing `*`

### Example `.env` file

[](#example-env-file)

Given a `.env` file that looks like this:

```
# The environment Craft is currently running in ('dev', 'staging', 'production', etc.)
ENVIRONMENT="local"

# The secure key Craft will use for hashing and encrypting data
SECURITY_KEY="jMgCxHuaM1g3qSzHiknTt5S8gDy5BNW7"

# The database driver that will be used ('mysql' or 'pgsql')
DB_DRIVER="mysql"

# The database server name or IP address (usually this is 'localhost' or '127.0.0.1')
DB_SERVER="localhost"

# The database username to connect with
DB_USER="homestead"

# The database password to connect with
DB_PASSWORD="secret"

# The name of the database to select
DB_DATABASE="craft3"

# The database schema that will be used (PostgreSQL only)
DB_SCHEMA="public"

# The prefix that should be added to generated table names (only necessary if multiple things are sharing the same database)
DB_TABLE_PREFIX=""

# The port to connect to the database with. Will default to 5432 for PostgreSQL and 3306 for MySQL.
DB_PORT="3306"
```

The following files will be output in the same directory as the `.env` file:

#### Apache `.env_apache.txt`

[](#apache-env_apachetxt)

Paste these inside the `` block

```
# Apache .env variables
# Paste these inside the  block:
SetEnv    ENVIRONMENT             "local"
SetEnv    SECURITY_KEY            "jMgCxHuaM1g3qSzHiknTt5S8gDy5BNW7"
SetEnv    DB_DRIVER               "mysql"
SetEnv    DB_SERVER               "localhost"
SetEnv    DB_USER                 "homestead"
SetEnv    DB_PASSWORD             "secret"
SetEnv    DB_DATABASE             "craft3"
SetEnv    DB_SCHEMA               "public"
SetEnv    DB_TABLE_PREFIX         ""
SetEnv    DB_PORT                 "3306"

```

...or you can include the files that Dotenvy generates directly in your Apache conf via:

```
Include /home/forge/SOMEDOMAIN/.env_apache.txt

```

#### Nginx `.env_nginx.txt`

[](#nginx-env_nginxtxt)

Paste these inside the `server {}` or `location ~ \.php {}` block or in the `fastcgi_params` file:

```
# Nginx .env variables
# Paste these inside the server {} or location ~ \.php {} block or in the fastcgi_params file:
fastcgi_param    ENVIRONMENT             "local";
fastcgi_param    SECURITY_KEY            "jMgCxHuaM1g3qSzHiknTt5S8gDy5BNW7";
fastcgi_param    DB_DRIVER               "mysql";
fastcgi_param    DB_SERVER               "localhost";
fastcgi_param    DB_USER                 "homestead";
fastcgi_param    DB_PASSWORD             "secret";
fastcgi_param    DB_DATABASE             "craft3";
fastcgi_param    DB_SCHEMA               "public";
fastcgi_param    DB_TABLE_PREFIX         "";
fastcgi_param    DB_PORT                 "3306";

```

...or you can include the files that Dotenvy generates directly in your Nginx conf via:

```
include /home/forge/SOMEDOMAIN/.env_nginx.txt

```

See [Nginx-Craft](https://github.com/nystudio107/nginx-craft) for details.

#### CLI (Bash shell) `.env_cli.txt`

[](#cli-bash-shell-env_clitxt)

Paste these inside your `.bashrc` file in your `$HOME` directory:

```
# CLI (bash) .env variables
# Paste these inside your .bashrc file in your $HOME directory:
export ENVIRONMENT="local"
export SECURITY_KEY="jMgCxHuaM1g3qSzHiknTt5S8gDy5BNW7"
export DB_DRIVER="mysql"
export DB_SERVER="localhost"
export DB_USER="homestead"
export DB_PASSWORD="secret"
export DB_DATABASE="craft3"
export DB_SCHEMA="public"
export DB_TABLE_PREFIX=""
export DB_PORT="3306"
```

#### Docker `.env_docker.txt`

[](#docker-env_dockertxt)

Paste these inside your `Dockerfile` file:

```
# Docker .env variables
# Paste these into your Dockerfile
ENV ENVIRONMENT="local"
ENV SECURITY_KEY="jMgCxHuaM1g3qSzHiknTt5S8gDy5BNW7"
ENV DB_DRIVER="mysql"
ENV DB_SERVER="localhost"
ENV DB_USER="homestead"
ENV DB_PASSWORD="secret"
ENV DB_DATABASE="craft3"
ENV DB_SCHEMA="public"
ENV DB_TABLE_PREFIX=""
ENV DB_PORT="3306"
```

The Craft CMS CLI
-----------------

[](#the-craft-cms-cli)

Note that if you set the `.env` variables directly in your Apache or Nginx config, these variables will **not** be available using the Craft CMS `./craft` CLI command.

That's because the webserver doesn't run at all for CLI requests. Instead, you'll need to add them to your `.bashrc` file as noted above, or you can use the Unix [source](https://bash.cyberciti.biz/guide/Source_command) command, e.g:

```
source .env_cli.txt && ./craft migrate/all
```

In the above example, the `source` command will execute the `export` statements in the `.env_cli.txt` and then run the `./craft` executable with those environmental variables set.

This pattern is useful if you are running multiple sites on a single instance, and so setting the `.env` variables globally for a user via `.bashrc` doesn't make sense.

Dotenvy Roadmap
---------------

[](#dotenvy-roadmap)

Some things to do, and ideas for potential features:

- Release it

Brought to you by [nystudio107](https://nystudio107.com/)

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~44 days

Total

5

Last Release

2469d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7570798?v=4)[Andrew Welch](/maintainers/khalwat)[@khalwat](https://github.com/khalwat)

---

Top Contributors

[![khalwat](https://avatars.githubusercontent.com/u/7570798?v=4)](https://github.com/khalwat "khalwat (30 commits)")[![brandonkelly](https://avatars.githubusercontent.com/u/47792?v=4)](https://github.com/brandonkelly "brandonkelly (1 commits)")

---

Tags

apachebashdotenvenvenvironmentenvironment-variablesnginxphpshellenvironmentenvdotenvnginxapachebash

### Embed Badge

![Health badge](/badges/nystudio107-dotenvy/health.svg)

```
[![Health](https://phpackages.com/badges/nystudio107-dotenvy/health.svg)](https://phpackages.com/packages/nystudio107-dotenvy)
```

###  Alternatives

[vlucas/phpdotenv

Loads environment variables from `.env` to `getenv()`, `$\_ENV` and `$\_SERVER` automagically.

13.5k602.4M5.4k](/packages/vlucas-phpdotenv)[symfony/dotenv

Registers environment variables from a .env file

3.8k226.7M2.3k](/packages/symfony-dotenv)[diarmuidie/envpopulate

Tool to interactively populate a `.env` file based on an `.env.example` file whenever Composer installs or updates.

1892.0k](/packages/diarmuidie-envpopulate)[zepgram/magento-dotenv

Simple autoloader to integrate the Symfony Dotenv component into Magento2

1371.3k](/packages/zepgram-magento-dotenv)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
