Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

albertoG-MG's avatar

How to set a new path for .env in laravel 11

I'm currently working with Laravel 11 and have encountered a situation where I need to set a new path for my .env file. By default, Laravel expects the .env file to be located in the root of the project. However, for various reasons—such as organizing environment configurations more effectively or accommodating specific deployment scenarios—I want to change this path.

I’ve tried a few approaches, but I'm unsure of the best way to implement this without breaking any functionality in my application. I’d appreciate any guidance or tips on how to properly set a new path for the .env file in Laravel 11 and ensure that my application continues to run smoothly.

Thank you!

0 likes
2 replies
jlrdw's avatar

Suggest that the folder containing .env is not in a web accessible folder. You point to public as the document root.

1 like
tykus's avatar

I believe it is as simple as this:

// bootstrap/app.php

$app = Application::configure(basePath: dirname(__DIR__))
    // ... etc.
    ->create();

$app->useEnvironmentPath('/the/new/path'); // path to directory not the .env file

return $app;
2 likes

Please or to participate in this conversation.