Lykos22's avatar

Configure Laravel to use different env file?

I would like to ask how to handle the cases where you want to use a different env file for your Laravel projects and how you point Laravel to this file? Like for example let's take this scenario: You are having a Laravel app as a backend api and a next/nuxt js app on the frontend and let's say the entire project is running on Docker, so the folder structure would be something like this

root-directory
- docker
- backend-laravel
- frontend-nextjs
docker-compose.yml
app.env

Now by default Laravel is using its own .env file for keeping all the env variables it needs, but what if you want to have all variables for both laravel and nextjs app in one single file outside of the laravel folder (in this case the app.env) ?

0 likes
3 replies
LaryAI's avatar
Level 58

To configure Laravel to use a different .env file, such as app.env located outside the Laravel project directory, you can follow these steps:

  1. Modify the bootstrap/app.php file:

    Laravel's environment file is loaded in the bootstrap/app.php file. You can modify this file to specify a different environment file. Open bootstrap/app.php and locate the following line:

    $app->loadEnvironmentFrom('.env');
    

    Change it to point to your app.env file:

    $app->loadEnvironmentFrom('../app.env');
    

    This assumes that your app.env file is located one directory above the Laravel project directory, as per your folder structure.

  2. Ensure Docker is set up correctly:

    If you're using Docker, make sure your Docker setup is configured to copy or mount the app.env file into the container. You can do this by modifying your docker-compose.yml file to include a volume that maps the app.env file into the container.

    Here's an example of how you might configure the volume in docker-compose.yml:

    services:
      backend-laravel:
        build:
          context: ./backend-laravel
        volumes:
          - ./app.env:/var/www/html/app.env
    

    Adjust the path /var/www/html/app.env to match the path where your Laravel application is located inside the Docker container.

  3. Access environment variables in Laravel:

    Once you've set up the environment file, you can access the environment variables in your Laravel application using the env() helper function or by using configuration files.

    For example, to access a variable named API_KEY, you can use:

    $apiKey = env('API_KEY');
    

By following these steps, you can configure Laravel to use a different environment file located outside the Laravel project directory. This setup allows you to manage environment variables for both your Laravel backend and Next.js frontend in a single file.

1 like
Snapey's avatar

You can use ACTUAL environment variables

MouteeSabouni's avatar

You can configure Docker to pass the app.env file to Laravel by setting the env_file directive in your docker-compose.yml file. This way, Docker will automatically load environment variables from app.env without needing to change anything in Laravel's internal configuration.

version: '3'
services:
  backend:
    build:
      context: ./backend-laravel
    env_file:
      - ./app.env # This points to the shared env file at the root
    volumes:
      - ./backend-laravel:/var/www/html

This tells Docker to load the environment variables from app.env at the root and inject them into the backend-laravel container. Laravel will automatically use these variables, as it first checks the system environment before reading from .env.

Please or to participate in this conversation.