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

查看:242
本文介绍了同一台服务器上的两个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,该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:\ notify \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Connection.php:332

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时发生连接?几乎就像它在缓存数据库连接并尝试继续使用相同的连接....如何停止该连接?

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应用程序相互冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆