同一台服务器上的两个 Laravel 应用程序相互冲突

我在同一台服务器上运行了 2 个 Laravel 应用程序.服务器是 Apache 2.4,我设置了虚拟主机来为不同域上的每个应用程序提供服务.

I have 2 Laravel applications running on the same server. The server is Apache 2.4 and I have vhosts set up to serve each application on a different domain.

第一个应用程序是一个 API,它的 .env 文件设置如下:

The first application is an API and it's .env file is set up like this:

APP_ENV=production
APP_KEY=YYYYYYYYYYYYYYYYYY
APP_DEBUG=false
APP_LOG_LEVEL=debug
APP_URL=https://notify.mysite.com
APP_DOMAIN=notify.mysite.com


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=notify
DB_USERNAME=YYYYYYYYYYYYYYYYYY
DB_PASSWORD=YYYYYYYYYYYYYYYYYY

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

第二个应用程序是一个 UI,其中使用了第一个应用程序的 API.它的 .env 文件是这样设置的:

The second application is a UI that among other things, utilizes the API from the first application. Its .env file is set up like this:

APP_ENV=local
APP_KEY=XXXXXXXXXXXXXX
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=https://asapps.mysite.com
APP_DOMAIN=asapps.mysite.com
APP_VERSION=1


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=asapps
DB_NOTIFY_DATABASE=notify
DB_FLIGHT_DATABASE=flights
DB_USERNAME=XXXXXXXXXXXXXX
DB_PASSWORD=XXXXXXXXXXXXXX

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

我可以从 Swagger 编辑器、Postman 和其他服务器向我的 API 发送消息,一切都按预期进行.

I can send messages to my API from my Swagger editor, from Postman, and from other servers and everything works as expected.

我的第二个应用程序本身也按预期工作.

My second application by itself also works as expected.

但是,如果我的第二个应用程序向 API 发送请求,则 API 应用程序会抛出此错误:

However, if my second application sends a request to the API, the API application throws this error:

带有消息SQLSTATE[42S02]"的异常PDOException":未找到基表或视图:1146 表asapps.preprocessor_config"不存在"在 C: otifyvendorlaravelframeworksrcIlluminateDatabaseConnection.php:332

exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'asapps.preprocessor_config' doesn't exist' in C: otifyvendorlaravelframeworksrcIlluminateDatabaseConnection.php:332

什么?

API 的数据库设置为DB_DATABASE=notify,当我从其他服务器发送消息时,它肯定会正确使用该连接.那么为什么要尝试使用第二个应用程序的数据库当我从该应用程序调用 API 时连接???它几乎就像在缓存数据库连接并试图继续使用相同的连接......我该如何阻止?

The database for the API is set to DB_DATABASE=notify and it definitely does properly use that connection when I send messages from other servers. So why the heck is it trying to use the second application's database connection when I call the API from that app??? Its almost like it's caching the DB connection and trying to keep using that same one.... How do I stop that?

表'asapps.preprocessor_config'不存在'

Table 'asapps.preprocessor_config' doesn't exist'

推荐答案

经过更多的挖掘(阅读疯狂的谷歌搜索),我找到了 问题和解决方案在这里

After more digging (read frantic googling), I found the problem and solution here

最重要的是,当站点 A 接受请求时,php 会在 http 请求的整个长度内加载它的 .env 变量.在该请求期间,当站点 A 调用站点 B 时,由于它们在运行相同 php 的同一台服务器上,php 仍在使用站点 A 的 .env 并且根本不单独加载站点 B 的 .env 文件.

The bottom line, when site A accepts a request, php loads it's .env variables for the entire length of the http request. During that request, when site A calls site B, since they are on the same server running the same php, php is still using the .env from site A and does not separately load site B's .env file at all.

作者更好的解释:

创建了包含变量的 .env 文件,这样人们就不会将他们的凭据推送到 github 存储库和其他可能共享源的地方.

The .env file with the variables was created so that people would not push their credentials to github repositories and other places where they may share the source.

现在,作为环境变量,它们在 http 请求的整个持续时间内(在本例中为脚本执行)成为系统范围内的变量.关键是你有一个长时间运行的脚本.

Now, being environment variables they become system wide for the entire duration of the http request (in this case script execution). The point is that you got a long running script.

要找到最终解决方案,您可以采用以下三种方法之一.

To find a definitive solution you could go one of the three ways.

....

'namespace' ENV 变量.

'namespace' the ENV variables.

相关文章