Two Laravel applications on the same server conflicting with one another - php

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'

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.

Related

Laravel - User Access denied

I created my first App, using Laravel in the Backend. Everything was running on my localhost and I deployed everything to my Netcup Webhosting Server.
I run composer install successfully, generated the API key by using php artisan key:generate --ansi and set up my .env file. The File looks like:
APP_NAME="App Name"
APP_ENV=prod
APP_KEY=base64_key
APP_DEBUG=false
APP_URL=https://xx.xxxxx.de
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME="db_user"
DB_PASSWORD="db_user_password"
The I want to create all the database tables by using php artisan migrate. I tripple checked the values set in the .env and red a lot of solutions of other posts, but nothing worked. I keep getting the following error:
I already added quotes to DB_User and Pasword in .env file. I restarted the server several times and cleaned the cache.
It looks like some problems with auth on the Netcup Web hosting Server. Can you check list of user with access to db_name? Besides, by screenshoot I guessing that hosting add prefix ("k177581_") for db_name and db_user
Solved the issue:
As #N69S correctly suggested it was related to Netcup. For any other Netcup user the following solution.
Netcup devides its Webhosting and Database. Therefore you need to put you Database IP into your .env file. That can be found in the connection data.

Cannot migrate SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)

I just cloned a website project on GitHub. When I want to migrate, an error like this appears. I have filled in DB_DATABASE and DB_USERNAME many times but it has no effect
.env
APP_NAME="Simple Company Profile"
APP_ENV=local
APP_KEY=base64:HLQuGR0tT28YYt4eg/GPPQpW4L+mii71zIx65nFNxDE=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=cms_pasti
DB_USERNAME=
DB_PASSWORD=
there is also a simple console here to create an admin account, but that also doesn't work
I don't think that cloning a project will automatically replicate the database. You have to create it with a script/sql. Which should be part of the Github project.
So,
Either find the schema and create the database and tables manually.
Or find a sql backup and restore it
You have just cloned the repository but, that doesn´t mean that is ready to run. You have to create a database and connect with it.
Use PHPMyAdmin or any other Database management solution (HeidiSQL, MySQL WorkBench) in order to check if your Database credentials are working.
Another thing that is probably happening is that you have to create the database 'cms_pasti' first and then fill the .env:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=cms_pasti
DB_USERNAME=
DB_PASSWORD=
With the proper settings.
When done, you have to run in your console:
PHP artisan migrate
This command will do the migrations to build the database structure. But you have to be sure that the database is already created.

Connection in laravel phpmyadmim

I am doing a project in laravael framework.
I'm trying to open some pages. It is showing an error:
at PDO->__construct('mysql:host=localhost;port=3306;dbname=dbname', 'root', 'phpmyadminpd', array('0', '2', '0', false, '0')) in Connector.php line 47
My .env file is:
APP_ENV=local
APP_DEBUG=true
APP_KEY=wdIYfgjVOzyDfchid9nBIlOzL4uIAYuY
DB_HOST=localhost
DB_DATABASE=dbname
DB_USERNAME=root
DB_PASSWORD=phpmyadminpd
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
PHPMYADMIN is just a web interface to deal with MySQL and has nothing to do with this.
If an app needs to deal with a database, such a Laravel app, it needs to get connected via the connection configuration files or the env files the usual stack.
From the phpMyAdmin docs:
Configuration
All configurable data is placed in config.inc.php in phpMyAdmin’s
toplevel directory. This file only needs to contain the parameters you
want to change from their corresponding default value in
libraries/config.default.php
Example documentation for reference:
https://docs.phpmyadmin.net/en/latest/config.html#config-examples
PHPMYADMIN is just a database management tool. It doesn't have to do anything with Laravel.
You have to deal with MySql. Make sure you put correct MySql database things correctly here
DB_DATABASE=dbname //your database name
DB_USERNAME=root //your database user
DB_PASSWORD=phpmyadminpd //your database password
I hope this will help you.

Laravel 5 Access Denied to mysql in browser but not in tinker

I've looked through a bunch of other threads but can't find a solution. My .env file is set up correctly and I can access the database fine through the php artisan tinker command. However when I spin it up on the browser I get access denied. I've changed the password a few different times to make sure I was putting in the correct username/password but still nothing.
APP_ENV=local
APP_DEBUG=true
APP_KEY=rUzE03agyabewGSbhch7QFKE0jqZqifO5
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=username
DB_PASSWORD=password
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
I have reloaded the virtualbox and even forced provisioned it, ran php config:clear, stop and started the mysql db. tried different users/passwords/dbs, switched localhost to 127.0.0.1, followed the documentation on laravel.com. Nothing has worked.
I'm at a loss...help would be appreciated.
The tutorials were not very clear on this point but basically I had to access mysql on the VM by using "vagrant ssh" and then logging onto mysql through the cli to create the correct user. Also all the migrate commands needed to be executed while logged into the VM through the ssh.
While the artisan commands will add the appropriate "make:' objects in either only the VM cli will allow you to access the correct database. Hopefully this post will save someone a day of sifting through a lot of posts about the .env file.

Lumen throws 500 sometimes when sending API request?

I am developing an application using anuglarJs on client side and Lumen in server side for REST API's. My .env configuration given below,
APP_ENV=local
APP_DEBUG=TRUE
APP_KEY=XrPbyRlU5p0szSw5MrAQWwWim8C0MXjT
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=kainfo
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
And enabled Dotenv::load(__DIR__.'/../'); in bootstrap/app.php
My problem is,sometimes Lumen throws 500 exception and working well on next refresh request.
The Error log is ,
[2015-06-06 07:51:41] lumen.ERROR: exception 'PDOException' with message 'SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'forge'' in C:\xampp\htdocs\Study\kasrodinfo\web\vendor\illuminate\database\Connectors\Connector.php:47
You need to change the database,username,password in \vendor\laravel\lumen-framework\config\database.php file
Also affected by this, here is what we have found on this issue:
We were using Apache 2.4 with the Event MPM and a thread-safe build of PHP 7. This caused the env() method to not respond reliably - it could give you the right variable at one point in the code, then just a few rows down it would return something else. (Like the default variable which trevorg was experiencing.)
We switched to using the prefork MPM and the problems vanished instantly.
Yes, prefork sucks in terms of speed when compared to another MPM, but it seems there is some issue with the event MPM and environmental variables in Apache, PHP and/or Laravel/Lumen.

Categories