Two Laravel applications on the same server confli

2019-01-28 05:48发布

问题:

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.

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

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

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.

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

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

WTH?

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?

Table 'asapps.preprocessor_config' doesn't exist'

回答1:

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

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.

The author's better explanation:

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.



回答2:

This is more of a comment than an answer, but I don't have enough rep to comment yet. I had a similar issue, though and may be able to help.

If these two applications are sharing any directories, laravel uses the filesystem to cache by default.

If you want to share a codebase with two different database connections, use the database for caching instead. You can publish the migration to create the cache table with the artisan command php artisan cache:table

Then, just run php artisan migrate and change the line CACHE_DRIVER in your .env file to 'database'