问题
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:
- Copy the contents of your .env to a temporary text file.
- In that text file, do a regex find-and-replace searching for "\n" and replacing with "\nexport " (with a trailing space).
- Move the final line's "export " to the first line.
- Copy the contents of this temporary text file.
- Paste into your terminal to run these commands.
- 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