简体   繁体   English

内置服务器的PHP无法加载环境变量

[英]PHP built in server unable to load env variables

First I run the following script to set the environment variable: 首先,我运行以下脚本来设置环境变量:

bin/env.sh bin / env.sh

#!/bin/bash

export JWT_SECRET="29dvqfmREKeOgBhGtTogK1pAi+2/x45hKzxONHetwFM="
export DB_HOST=127.0.0.1
export DB_NAME=myapp
export DB_USER=user
export DB_PASS=password
export DB_PORT=3306

bin/env.sh

Then I run the next script in the same terminal to start development server: 然后,我在同一终端中运行下一个脚本以启动开发服务器:

bin/dev.sh bin / dev.sh

#!/bin/bash

php -d variables_order=EGPCS -S localhost:5001 -t ./public

bin/dev.sh

The server starts, but when I echo getenv('DB_USER') it's blank. 服务器启动,但是当我回显getenv('DB_USER')它为空。

The thing is, if I put php -d variables_order=EGPCS -S localhost:5001 -t ./public at the foot of env.sh it works. 问题是,如果我把php -d variables_order=EGPCS -S localhost:5001 -t ./public在脚下env.sh它的工作原理。

Why does it work if it's in the same script, but doesn't otherwise? 如果在同一脚本中为什么会起作用,但在其他脚本中却不起作用? Both scripts are being ran from the same terminal 这两个脚本都从同一终端运行

The problem you are facing is none of the variables you defined in bin/env.sh is available in the current shell in which you are running the development server. 您面临的问题是您在其中运行开发服务器的当前shell中没有在bin/env.sh定义的变量。 Hence, you need to add a line 因此,您需要添加一行

source bin/env.sh

at beginning of the bin/dev.sh to make the variables available in the current session. bin/dev.sh开头,以使变量在当前会话中可用。

There is a built-in shell built-in source available just for that. 为此有一个内置的shell内置source On sourcing the file, you are actually executing commands from filename in the current shell environment. 在获取文件时,实际上是在当前Shell环境中从filename执行命令。

Remember sourcing is different from running a script which "executes" the script in a new shell than the shell originally invoked from. 请记住,源代码不同于运行脚本,该脚本在新的Shell中“执行”脚本,而不是最初从其调用的Shell。

To explain better with an example:- 用一个例子更好地解释:

Consider script.sh with following content: 考虑具有以下内容的script.sh

#!/bin/bash

echo "foo: "$(env | grep FOO)
export FOO=foo
echo "foo: "$(env | grep FOO)

Before we execute the script first we check the current environment: 首先执行脚本之前,请检查当前环境:

$ env | grep FOO

The variable FOO is not defined. 未定义变量FOO On executing the file 在执行文件时

$ ./script.sh
foo:
foo: FOO=foo

Check the environment again: 再次检查环境:

$ env | grep FOO

The variable FOO is not set. 未设置变量FOO

The script output clearly shows that the variable was set. 脚本输出清楚地表明已设置了变量。 The check post running the script showed that the variable is not set because the changes were made in a new shell. 运行脚本的检查站显示未设置变量,因为更改是在新的Shell中进行的。 The current shell spawned a new shell to run the script and all the exported variables are available only till the life time of that spawned shell. 当前shell产生了一个新的shell来运行脚本,并且所有导出的变量仅在该shell的生命周期内可用。 After the script terminates the new shell is destroyed. 脚本终止后,新的外壳将销毁。 All changes to the environment in the new shell are destroyed with the new shell. 新外壳中对环境的所有更改都将被新外壳破坏。 Only the output text is printed in the current shell. 当前外壳中仅打印输出文本。

Now we source the file: 现在我们source文件:

$ source script.sh
foo:
foo: FOO=foo

$ env | grep FOO
FOO=foo

You can see now the variable FOO is set. 您现在可以看到已设置变量FOO

The export command makes variables available to sub-shells, it doesn't add them to the parent environment. export命令使变量可用于子外壳,但不会将其添加到父环境中。 Use source if you want to run a script to update the current environment. 如果要运行脚本来更新当前环境,请使用source

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM