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.
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.
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.
Use the getenv() function of PHP.
PHP: getenv - Manual
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
SetEnv DB_Pass dbpassword123
in your Vhostfastcgi_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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With