I\'m trying out the PHP micro Framework Lumen (from Laravel).
One of my first steps was to look into the .env.example
file and make a copy of it to have my
An easy solution is just running PHP code from the terminal (without using tinker
, because that is not available with Lumen):
php -r "require 'vendor/autoload.php'; echo str_random(32).PHP_EOL;"
It uses Laravel's Str::random()
function that makes use of the secure random_bytes()
function.
The APP_KEY generation is a step of development process (I don't think that creating temporarily routes is a practical way to do it). The function str_random
can help us, but this function is part of Laravel/Lunmen framework.
I recommend running tinker
php artisan tinker
and then run the function
>>> str_random(32)
The result is the key you're looking for.
=> "y3DLxnEczGWGN4CKUdk1S5GbMumU2dfH"
Simply use PHP CLI. Run this from your local or a remote command line to generate a random 32-character Lumen APP_KEY:
php -r "echo bin2hex(random_bytes(16));"
Output: bae48aba23b3e4395b7f1b484dd25192
Works with PHP 7.x on Mac and Windows.
All I do on mac is execute this command in the terminal:
date | md5 | pbcopy
This copies the value into the clipboard and so you can easily paste the key into the .env
file.
I have used these commands:
php -r \"copy('.env.example', '.env');\"
php -r "echo password_hash(uniqid(), PASSWORD_BCRYPT).\"\n\";"
The command generates a key similar to this:
$2y$10$jb3kw/vUANyzZ4ncMa4rwuR09qldQ2OjX8PGrVB5dIlSnUAPCGjFe
This answer was inspired by @thomas-venturini 's update to the question. Here's a bash script that takes care of creating .env
and updating it with an APP_KEY
using the aforementioned PHP command and the UNIX sed
command:
#!/usr/bin/env bash
function generate_app_key {
php -r "echo md5(uniqid()).\"\n\";"
}
APP_KEY=$(generate_app_key)
sed -e s/APP_KEY=.*$/APP_KEY=${APP_KEY}/g .env.example > .env
Hope someone finds this useful.