Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 get Linux system environment variables

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.

like image 323
yclaes Avatar asked Mar 17 '16 09:03

yclaes


People also ask

How can I get current environment in Laravel?

The best way to do this is using your config file. However you can also ask Laravel directly what the current environment is. I prefer to use the config option, however with the app()->environment('production') you can directly check on the correct environment.

How do I see my environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables.


2 Answers

Use the getenv() function of PHP.

PHP: getenv - Manual

like image 175
Capital C Avatar answered Nov 03 '22 05:11

Capital C


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 [email protected]
    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');
like image 36
codedge Avatar answered Nov 03 '22 03:11

codedge