沃梦达 / 编程问答 / php问题 / 正文

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

Two Laravel applications on the same server conflicting with one another(同一服务器上的两个 Laravel 应用程序相互冲突)

本文介绍了同一服务器上的两个 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

WTH?

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.

....

命名空间"ENV 变量.

'namespace' the ENV variables.

这篇关于同一服务器上的两个 Laravel 应用程序相互冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:同一服务器上的两个 Laravel 应用程序相互冲突

基础教程推荐