.env file wont load variables into config.php - student setting up first php dev environment

余生长醉 提交于 2020-07-31 03:34:07

问题


Afternoon everyone,

I'm a student studying Computer Science and I'm trying to recreate the environment my friend is using to host their PHP based web app. They're on a Mac using heroku local (Procfile calling heroku-php-apache2) to set up their environment. I'm on a Windows 10 PC, and from what research I'm done, heroku local is not supported in any way. So I enabled WSL installed Ubuntu 18.04 and Apache2 and, as far as I can tell, downloaded and installed all of the other components necessary to make it run (composer, modrewrite, modenv, etc). phpinfo(), heroku's sample project, and any simple php pages I make display properly. My friend's app on the other hand is still giving me trouble.

They're using a .env to declare project specific environment variables that are further defined in a config.php. The app is deployed and works in Heroku and on their machine, but when I try to load the app locally on my machine I get an exception thrown saying the environment variables aren't being loaded. If I add "local: php -S localhost:80" to the Procfile and run heroku local on Ubuntu, it sees the .env file and says its loaded, only to kick back the same errors my apache2 instance is throwing.

What could be causing this? I've edited php.ini to include an "E", enabled modrewrite and modenv, made sure my .env file was encoded in UTF-8 - I've searched far and wide for a reason this might be happening but I keep coming to a dead end. Is there something about the "heroku local" command and instance that I'm missing? I'm still new to php, web servers, and programming in general, so any relevant information regarding why my .env file isn't working or any possible ways I can get heroku local to work on Ubuntu using WSL would be massively appreciated.


回答1:


I'm using WSL and am similarly having problems with my .env not working properly.

Although I'm still looking for a more elegant fix, the hack I'm relying on now is:

  1. Copy the contents of your .env to a temporary text file.
  2. In that text file, do a regex find-and-replace searching for "\n" and replacing with "\nexport " (with a trailing space).
  3. Move the final line's "export " to the first line.
  4. Copy the contents of this temporary text file.
  5. Paste into your terminal to run these commands.
  6. Now you should be able to run your app.



回答2:


Straightforward Method

You can do this very simply by adding the environment variable to your .bashrc file, assuming that your Windows Environment variables are set correctly and WSL is installed, I'm going to use Java as an example, but any environment variable will work.

Use text editor in WSL or type code .bashrc from WSL home to initialize the WSL VSCode editor. Add the environment variable to the file:

# Shared environment variables
export JAVA_HOME=/mnt/d/Java/jdk11.0.4_10

Just ensure that you change the directory to point to your directory. In my case, it's in D:\Java\jdk11.0.4_10 which in WSL is /mnt/d/Java/jdk11.0.4_10

Also, since you're using Windows binaries, you must specify the file type when running from a WSL bash shell:

Example

Windows:

javac MyClass.java
java MyClass

WSL:

javac.exe MyClass.java
java.exe MyClass

Note that the .exe file extension is being used, since we're calling the Windows binary. If it was a native Linux distribution of the JDK you could simply use java command.

This holds true for any Windows binary that is being run through WSL.



来源:https://stackoverflow.com/questions/62707357/env-file-wont-load-variables-into-config-php-student-setting-up-first-php-dev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!