13

Is there a possibility to get some Linux system environment variables and still use the .env variables?

We want to use an auto-generated database password that's set as Linux environment variable but can't get Laravel to find the Linux system environment variables.

yclaes
  • 265
  • 3
  • 11
  • 2
    as per my knowledge, you cannot use Linux environment variable directly, you have to run shell commands, grab your variable and assign it to Laravel global variable for internal use – Qazi Mar 17 '16 at 09:32
  • 3
    http://stackoverflow.com/questions/13568191/how-to-get-system-environment-variables-into-php-while-running-cli-apache2hand have you looked at it? – Nabin Kunwar Mar 17 '16 at 10:52
  • Any reason this question is still open? – miken32 Feb 07 '17 at 00:37

3 Answers3

2

Use the getenv() function of PHP.

PHP: getenv - Manual

Capital C
  • 319
  • 3
  • 13
1

Linux system variables cannot be access through PHP/Apache. You could set a variable in the Apache Vhost of your site via SetEnv and grab it in Laravel.

You could do

  • Apache: SetEnv DB_Pass dbpassword123 in your Vhost
  • Nginx: fastcgi_param DB_Pass dbpassword123

Example Apache Vhost:

<VirtualHost example.com:80>
    ServerAdmin root@mpj.local.dev
    DocumentRoot /var/www/html
    ServerName mpj.local.dev

    SetEnv DB_Pass dbpassword123

    <Directory /var/www/html>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog "/var/log/apache2/error_log"
    CustomLog "/var/log/apache2/access_log" common
</VirtualHost>

and fetch the variable DB_Pass in Laravel with

$dbPass = env('DB_Pass');
codedge
  • 4,754
  • 2
  • 22
  • 38
  • 1
    Halfway there. It is very possible, the variables just have to be passed into the HTTP server first. As @NabinKunwar shared, see [How to get system environment variables into PHP while running CLI & Apache2Handler?](http://stackoverflow.com/a/23635043/5816643). – Qevo Jun 24 '16 at 16:20
0

You should use 'env' function.

https://laravel.com/docs/5.2/configuration#environment-configuration

  • 1
    The env function is limited to Laravel project environment variables, we're not able to get the Linux system environment variables. – yclaes Apr 01 '16 at 09:00
  • Well, you need to export you variable to be accessible by any user. /etc/profile.d must be a good start point. – Alex Montoanelli Apr 01 '16 at 19:52