DEV Community

Cover image for How generate a Laravel APP_KEY with OpenSSL
Gregori Piñeres
Gregori Piñeres

Posted on • Edited on

How generate a Laravel APP_KEY with OpenSSL

Hello everyone, today I will show you a little trick that I use frequently in my scripts to create a Laravel application key commonly generated with artisan.

It's simple, with the help of the openssl package we can generate a random string of 32 characters and at the same time, it can be encoded in base64.

This will generate a fully valid laravel application key that you can use in your .env configuration file.

echo "base64:$(openssl rand -base64 32)"
Enter fullscreen mode Exit fullscreen mode

Thanks for reading this little tip, don't forget to share it on your social networks or with your development team.

We read soon.

Top comments (4)

Collapse
 
gregorip02 profile image
Gregori Piñeres

Laravel added a flag to the command to get a valid key without modifying the .env file

php artisan key:generate --show
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zubairmohsin33 profile image
Zubair Mohsin

Hi, thanks for sharing this tip. Just curious about the reason behind it? Or is it just another way to do it?

Collapse
 
gregorip02 profile image
Gregori Piñeres

On one occasion I had to do it this way to generate a secret file in a Laravel application in Kubernetes. Obviously this is another way to do it without having the PHP interpreter installed locally. However you do it, the result is the same.

Collapse
 
bramaudi profile image
Brama Udi

Just in-case if need a PHP version:

$rand = random_bytes(32); // chiper = AES-256-CBC ? 32 : 16
echo 'base64:'.base64_encode($rand);
Enter fullscreen mode Exit fullscreen mode