yclaes
yclaes

Reputation: 265

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.

Upvotes: 13

Views: 5525

Answers (3)

Capital C
Capital C

Reputation: 329

Use the getenv() function of PHP.

PHP: getenv - Manual

Upvotes: 2

codedge
codedge

Reputation: 5174

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');

Upvotes: 1

Alex Montoanelli
Alex Montoanelli

Reputation: 64

You should use 'env' function.

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

Upvotes: 0

Related Questions