gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:07:48 28 4
gpt4 key购买 nike

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

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

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 文件是这样设置的:

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 发送消息,一切都按预期进行。

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

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

exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'asapps.preprocessor_config' doesn't exist' in C:\notify\vendor\laravel\framework\src\Illuminate\Database\Connection.php:332

什么?

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

Table 'asapps.preprocessor_config' doesn't exist'

最佳答案

经过更多挖掘(阅读疯狂的谷歌搜索),我找到了 problem and solution here

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

作者更好的解释:

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.

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' the ENV variables.

关于php - 同一台服务器上的两个 Laravel 应用程序相互冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42075542/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com