A simple PHP package to generate .env files from example templates using system environment variables.
When you deploy an application, you usually have a .env.example file with all the environment variable keys your application needs. The problem is: how do you generate the actual .env file with the correct values from your server environment?
This package solves that problem. It reads the keys from your .env.example file and creates a new .env file containing only the variables that exist in your system environment.
- Security: Only exports variables that your application actually needs (defined in
.env.example) - Automation: No manual copy-paste of environment variables during deployment
- Consistency: Ensures your
.envfile always matches your.env.examplestructure
- PHP 7.1 or higher
- The package reads your
.env.examplefile - It extracts all the variable keys (for example:
APP_NAME,DB_HOST,REDIS_URL) - For each key, it checks if that variable exists in the system environment
- If the variable exists, it adds the key and value to the output
- It writes the result to your
.envfile
The package understands these formats:
# Simple key=value
APP_NAME=Laravel
# Empty value
APP_ENV=
# Quoted values
APP_NAME="My App"
# With export prefix
export APP_NAME=Laravel
# Comments are ignored
# This is a comment
; This is also a commentcomposer require tofuma/env-exporter<?php
use Tofuma\EnvExporter\EnvExporter;
// Generate .env file from .env.example
EnvExporter::generate('.env.example', '.env');
// Or get the key-value pairs as an array (without writing to file)
$pairs = EnvExporter::generate('.env.example', '.env', true);
print_r($pairs);
// Output: ['APP_NAME' => 'MyApp', 'DB_HOST' => 'localhost', ...]
// Generate file and get the count of exported variables
$count = EnvExporter::generateAndReport('.env.example', '.env');
echo "Exported {$count} variables";vendor/bin/env-exporter .env.example .envOutput:
Generated .env with 15 entries: .env
#!/bin/bash
# Your deployment script
cd /var/www/myapp
git pull origin main
composer install --no-dev
# Generate .env from system environment variables
vendor/bin/env-exporter .env.example .env
php artisan migrate --forceIn your docker-entrypoint.sh:
#!/bin/bash
# Generate .env file from container environment variables
vendor/bin/env-exporter .env.example .env
# Start the application
php-fpmIn your Dockerfile:
FROM php:8.2-fpm
WORKDIR /var/www/html
COPY . .
RUN composer install --no-dev
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["docker-entrypoint.sh"]In your docker-compose.yml:
services:
app:
build: .
environment:
APP_NAME: "My Application"
APP_ENV: production
APP_DEBUG: "false"
DB_HOST: database
DB_DATABASE: myapp
DB_USERNAME: root
DB_PASSWORD: secretMany projects use a run.sh script to start the application inside a Docker container. This is a common pattern for applications that need to do some setup before starting.
In your run.sh:
#!/bin/bash
set -e
# Generate .env file from container environment variables
vendor/bin/env-exporter .env.example .env
# Run database migrations (optional)
php artisan migrate --force
# Clear and cache configurations (optional)
php artisan config:cache
php artisan route:cache
# Start the application
exec php-fpmIn your Dockerfile:
FROM php:8.2-fpm
WORKDIR /var/www/html
# Install dependencies
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-scripts
# Copy application code
COPY . .
# Make run.sh executable
RUN chmod +x run.sh
# Use run.sh as the entrypoint
CMD ["./run.sh"]In your docker-compose.yml:
services:
app:
build: .
environment:
APP_NAME: "My Application"
APP_ENV: production
APP_DEBUG: "false"
APP_KEY: base64:your-app-key-here
APP_URL: https://myapp.com
DB_CONNECTION: mysql
DB_HOST: database
DB_PORT: 3306
DB_DATABASE: myapp
DB_USERNAME: root
DB_PASSWORD: secret
CACHE_DRIVER: redis
REDIS_HOST: redis
REDIS_PORT: 6379
depends_on:
- database
- redis
database:
image: mysql:8.0
environment:
MYSQL_DATABASE: myapp
MYSQL_ROOT_PASSWORD: secret
redis:
image: redis:alpineDokploy is a deployment platform that allows you to set environment variables through its web interface. When you add environment variables in Dokploy, they become available as system environment variables in your container.
The problem: Dokploy (and similar platforms) may inject many system variables that you do not need. If you try to export all environment variables, you will get many unnecessary variables.
The solution: This package reads only the keys defined in your .env.example file. This means you only get the variables your application needs.
Step 1: In your Dokploy project, go to the "Environment" tab and add your variables:
APP_NAME=My Application
APP_ENV=production
APP_DEBUG=false
APP_URL=https://myapp.com
DB_CONNECTION=mysql
DB_HOST=your-database-host
DB_PORT=3306
DB_DATABASE=myapp
DB_USERNAME=myuser
DB_PASSWORD=mysecretpassword
REDIS_HOST=your-redis-host
REDIS_PORT=6379
Step 2: In your project, make sure you have a .env.example file with all the keys:
APP_NAME=
APP_ENV=
APP_DEBUG=
APP_URL=
DB_CONNECTION=
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
REDIS_HOST=
REDIS_PORT=
Step 3: Create a run.sh script in your project root:
#!/bin/bash
set -e
# Generate .env file from Dokploy environment variables
vendor/bin/env-exporter .env.example .env
# Start your application
exec php-fpmStep 4: In your Dockerfile, use the run.sh script:
FROM php:8.2-fpm
WORKDIR /var/www/html
COPY . .
RUN composer install --no-dev
RUN chmod +x run.sh
CMD ["./run.sh"]The package will create a .env file with only the variables defined in .env.example, using the values from Dokploy environment settings.
In your Kubernetes deployment manifest:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
template:
spec:
containers:
- name: app
image: myapp:latest
envFrom:
- secretRef:
name: myapp-secrets
- configMapRef:
name: myapp-configIn your container run.sh:
#!/bin/bash
set -e
vendor/bin/env-exporter .env.example .env
exec php-fpmname: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: composer install --no-dev
- name: Generate .env file
run: vendor/bin/env-exporter .env.example .env
env:
APP_NAME: ${{ vars.APP_NAME }}
APP_ENV: production
DB_HOST: ${{ secrets.DB_HOST }}
DB_DATABASE: ${{ secrets.DB_DATABASE }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
- name: Deploy
run: ./deploy.shIn your deployment script:
cd /home/forge/myapp.com
# Generate .env from server environment variables
vendor/bin/env-exporter .env.example .env
php artisan config:cache
php artisan route:cache
php artisan view:cacheMIT License. See LICENSE for more information.
Created and maintained by tofuma.