Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel `.env`, system-level, and server-level environment variables

Tags:

I am a bit confused by the following sentence in Laravel 5 Documentation :

Any variable in your .env file can be overridden by external environment variables such as server-level or system-level environment variables.

It looks like what I want to do, i.e. setting some of my .env variables from server-level environment variables, but I can't find any reference on how to do it.

It seems that there are some security concerns behind such a configuration, but the following stackoverflow answer does not comment this precise sentence.

I also tend to think that if references to environment variables are used in the .env file, it is precisely to remove the confidential information, and as such I don't see anymore the security concerns.

My conclusion is that I misunderstood the sentence, but I'd be very happy to understand why, or, if by chance it happens to be possible, know how to do that. Thanks.

like image 948
Pierre Cordier Avatar asked Aug 07 '17 22:08

Pierre Cordier


People also ask

How do I change environment variables in Laravel dynamically?

A simple way to update the . env key value in laravel is to add the below code in the controller function where you want to change . env values. $key = 'VARIABLE_NAME'; $value = 'NEW VALUE'; file_put_contents(app()->environmentFilePath(), str_replace($key .

For what do the .env is used in Laravel?

Laravel's default .env file contains some common configuration values that may differ based on whether your application is running locally or on a production web server. These values are then retrieved from various Laravel configuration files within the config directory using Laravel's env function.

What does Getenv mean in PHP?

getenv — Gets the value of an environment variable.


1 Answers

I was curious about this as well. Here's your answer:

System Level Env Variables:

These are set on the actual operating system themselves.

For example, in Windows, system level variables can be configured in:

  1. Control Panel
  2. System Properties
  3. Click Environment Variables
  4. You will see all the system level variables Windows contains

Not sure where env variables are stored in Linux unfortunately.

Server Level Env Variables:

These are set on the host server, for example in Apache, server level env variables can be configured via the file /etc/apache2/envvars

https://httpd.apache.org/docs/2.4/mod/mod_env.html#setenv

On Windows IIS, they can be configured via the FastCGI module described here:

https://docs.microsoft.com/en-us/iis/configuration/system.webserver/fastcgi/application/environmentvariables/#how-add-a-fastcgi-environment-variable-for-php

If hyperlink changes:

  1. Open IIS Manager
  2. In the Connections pane, click the server name for which you want to configure FastCGI settings.
  3. In the Home pane, double-click FastCGI Settings.
  4. Highlight the PHP application in the FastCGI Settings pane, then click Edit... in the Actions pane
  5. In the Edit FastCGI Application dialog box, click the ellipsis (...) next to EnvironmentVariables to open the environment variables collection editor.
  6. In the EnvironmentVariables Collection Editor dialog box, click Add.
  7. Enter the following information, and then click OK.
    • Enter "PHP_FCGI_MAX_REQUESTS" for the Name.
    • Enter "10000" for the Value.
  8. Click "Ok" to close the Add FastCGI Application dialog box.
like image 114
Steve Bauman Avatar answered Sep 29 '22 23:09

Steve Bauman