本文介绍了.env文件不会将变量加载到config.php-学生设置第一个php开发环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家下午,

我是一名学习计算机科学的学生,我正在尝试重新创建我的朋友用来托管其基于PHP的Web应用程序的环境.他们在Mac上使用heroku本地程序(Procfile调用heroku-php-apache2)来设置环境.我使用的是Windows 10 PC,根据我所做的研究,不以任何方式支持heroku local.因此,我启用了WSL,安装了Ubuntu 18.04和Apache2,据我所知,下载并安装了使其运行所需的所有其他组件(编写器,modrewrite,modenv等). phpinfo(),heroku的示例项目以及我制作的任何简单的php页面都可以正确显示.另一方面,我朋友的应用仍然给我带来麻烦.

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.

他们使用.env声明项目特定的环境变量,这些变量在config.php中进一步定义.该应用程序已部署并可以在Heroku及其计算机上运行,​​但是当我尝试在计算机上本地加载该应用程序时,会抛出一个异常,表明未加载环境变量.如果我添加"local:php -S localhost:80",到Procfile并在Ubuntu上本地运行heroku时,它会看到.env文件并说已加载,只是反击了我的apache2实例抛出的相同错误.

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.

可能是什么原因造成的?我已经编辑了php.ini,使其包含"E",启用了modrewrite和modenv,并确保我的.env文件使用UTF-8编码-我已经进行了广泛搜索,原因可能是这种情况发生,但我一直死胡同.关于"heroku local"有什么问题吗?我缺少的命令和实例?我仍然对php,Web服务器和编程尚不熟悉,因此,有关.env文件为何无法正常工作或我可以将heroku本地化以使用WSL在Ubuntu上进行工作的任何可能方式的任何相关信息,将倍受赞赏.

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.

推荐答案

简单方法

您可以通过将环境变量添加到您的.bashrc文件中来非常简单地进行此操作,假设您正确设置了Windows环境变量并安装了WSL,我将以Java为例,但是任何环境变量会起作用.

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.

在WSL中使用文本编辑器,或从WSL主页键入code .bashrc来初始化WSL VSCode编辑器.将环境变量添加到文件中:

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

只需确保将目录更改为指向目录即可.就我而言,它在D:\Java\jdk11.0.4_10中,而在WSL中是/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

此外,由于您使用的是Windows二进制文件,因此从WSL bash shell运行时必须指定文件类型:

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

Windows:

javac MyClass.java
java MyClass

WSL:

javac.exe MyClass.java
java.exe MyClass

请注意,由于我们正在调用Windows二进制文件,因此正在使用.exe文件扩展名.如果它是JDK的本地Linux发行版,则只需使用java命令.

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.

这对于通过WSL运行的任何Windows二进制文件都适用.

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

这篇关于.env文件不会将变量加载到config.php-学生设置第一个php开发环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:33