Laravel 4.1 Deployment - Production .env.php not being recognised - laravel

For some reason, my production laravel app thinks that it is in the local environment.
/var/www/appname/.env.php
<?php
return
[
'APP_ENV' => 'production',
'DB_HOST' => 'HIDDEN',
'DB_NAME' => 'HIDDEN',
'DB_PASSWORD' => 'HIDDEN'
];
/var/www/appname/bootstrap/start.php
$env = $app->detectEnvironment(function()
{
return getenv('APP_ENV') ?: 'local';
});
/var/www/appname/app/config/database.php
...
...
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'lar_',
),
...
...
sudo php artisan env (via SSH)
`Current application environment: local
php artisan tinker then getenv('DB_NAME')
$ php artisan tinker
[1] > getenv('DB_NAME');
// false
So either my environment variables are not being set, or Laravel is not recognising my .env.php file for the production environment.
Update
With some help from Anultro on IRC, it appears that .env.php is not loaded yet. As such APP_ENV must be set before laravel tries to detect environments. This makes sense, because Laravel needs to know what environment is running before determining whether to use .env.php or .env.local.php.
Having said this, .env.php should still be used to store db credentials and secret keys etc... But I am still having a problem because the app is still returning false when I try to run getenv('DB_NAME')
Any suggestions?

For all who want to know... to solve this issue, I just edited my httpd.conf file on the production server as follows:
SetEnv APP_ENV production
Now laravel knows that the app is in production.
If you are using nginx which I have now migrated my site to add the following where you pass scripts to the FCGI server in the active sites-available /etc/nginx/sites-available/{sitename}:
fastcgi_param APP_ENV production;

Related

How to configure Laravel to use PhpRedis?

PHP 7.3
Laravel 5.8
Until now I was using Predis for my cache in the Laravel project. Now I want to switch to PhpRedis. I've read it's really simple (just config changes), but I have a lot of problems. I don't know what to begin with, so I'll write all what I know.
My hosting provider claims that PhpRedis is enabled.
The code below executed in a controller (Predis is set) works fine - I receive the set value.
$redis = new \Redis();
$redis->connect( 'socket path', 0 );
$redis->set('test', 'testValue');
print_r( $redis->get('test') );
However, the same code in the raw PHP file executed via SSH returns "Uncaught Error: Class 'Redis' not found in..."
Let's go to changes in config/database.php. Here is my configuration:
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'/*'phpredis'*/),
'cluster' => true,
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'/*'redis'*/),
'prefix' => Str::slug(env('APP_NAME'), '_').'_',
'parameters' => ['password' => env('REDIS_PASSWORD', null)],
],
'default' => [
'scheme' => 'unix',
'path' => env('REDIS_HOST'),
'host' => env('REDIS_HOST'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT'),
'database' => env('REDIS_CACHE_DB', 0)
],
(...) // other
],
When I change values to these in comments, my website shows just a blank page - any errors in the mailbox.
Furthermore, when I run for example "php73 artisan config:clear" in the SSH, console returns "Please remove or rename the Redis facade alias in your "app" configuration file in order to avoid collision with the PHP Redis extension." in the Illuminate/Redis/Connectors/PhpRedisConnector.php.
When I change the alias in config/app.php from "Redis" to "RedisManager" and try again it returns
Uncaught Error: Class 'Redis' not found in /path/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:70.
What's going on? How to set Laravel's configuration to use PhpRedis? Maybe it's my hosting provider issue? Thanks in advance for every advice.
If I missed some important code, give me a sign - I will add it.
The PHPRedis libraries are not installed by default in a shared hosting environment, and are generally not part of a PHP installation by default. You would have to ask your host to install these libraries within their shared hosting platform.

Database [] not configured Laravel 5

I create new db in phpmyadmin and new tables.
Then i do
public function next(Request $request){
$langs = DB::connection('mydb')->select('select * from lang');
}
and get
Database [compgen] not configured.
in my .env
DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=123
in my config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', '123'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'test_',
'strict' => false,
],
In my case I used 2 db connections and the second added was not cached, the command call:
php artisan config:cache
did the trick.
To see what's going on it was sufficient to output the $connections variable in DatabaseManager->configure method.
You're using single connection, so you don't need to specify it:
$langs = DB::table('lang')->get();
You should use connection() method only when you're working with multiple DB connections.
For me the connection worked in development but not in the production environment, so after a lot of searching, I had to rebuild the configuration cache and It has worked. I ran the following command in the laravel root directory:
php artisan config:cache
Anyone stumbling upon this - if you are using Laravel 5.4 or newer, you can setup a new database connection in config/database.php
'mysql_test' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE_TEST', 'forge'),
'username' => env('DB_USERNAME_TEST', 'forge'),
'password' => env('DB_PASSWORD_TEST', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'modes' => [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_AUTO_CREATE_USER',
'NO_ENGINE_SUBSTITUTION',
],
'engine' => null,
],
I created 3 new env variables DB_USERNAME_TEST, DB_PASSWORD_TEST, DB_DATABASE_TEST
Edit .env with something like
DB_DATABASE_TEST=test_db
DB_USERNAME_TEST=local
DB_PASSWORD_TEST=localpassword
Make sure config is refreshed: php artisan config:cache
You should be able to run a database migrate on your new test database, like so:
php artisan migrate:refresh --database=mysql_test
In my case it was a bad database username. I had it set in my config/database.php as well as the .env file and one of them was different than the other. Found this thread when searching, thought this might help someone.
I struggled with this error for a few hours and found out solution that works for me. If you can not delete cache with php artisan config:cache because it throws error after you run it:
[InvalidArgumentException]
Database [] not configured
And if you are sure that your connections parameters are good then try manually delete bootstrap cache config files which are placed in app/bootstrap/cache folder.
Hope it helps someone.
make sure the parameters
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME=dbuser
DB_PASSWORD="password"
and then issue,
php artisan optimize:clear
I had the same issue but when i added
'default' => env('DB_CONNECTION', 'mysql'),
to database.php.It seems to have solved the problem
if this error shows when you use the validation so it is simple :) just check all the validation rules and messages are written >
correctly
for me i faced this error and i solve it by replace only point by comma in the validation rules.

Dynamic env-files (multiple databases) and artisan commands

I have a large project which will have each customer on their own separate database. To get this to work we use a custom .env-loader that loads each customers .envby checking the customers subdomain (unique to each customer).
However, of course this doesn't work with artisan commands. For instance, when I want to migrate, I will need to migrate all databases at once. So I've set up an Artisan command that fetches the .env-files and loop through them and then calls the default artisan migrate. But it is not working as expected.
I've tried everything; for instance:
$dotenv = new Dotenv('/env', '.test.env');
$dotenv->overload();
And:
app()->useEnvironmentPath('/env');
app()->loadEnvironmentFrom('.test.env');
And even:
config('database.connections.mysql.database', 'test_database');
As soon as I run $this->call('migrate'); the app defaults to the default .env and ignores all customizations at runtime. Does anyone have an idea on how I can overload the migration commands choice of database?
Note: I know that I can manually setup multiple connections in config/database.php (for instance like: Overriding Default Laravel database configuration for artisan migrate commands), however, image a few dozen customers and this would not be viable.
I had to do something similar with SQLite database that were being created by the console commands, and the only way I could get the migrations to run was by creating a database config on the fly:
Config::set('database.connections.'.$config_key, array(
'driver' => 'sqlite',
'database' => storage_path($database_name),
'prefix' => '',
));
And then I would call the migrate command:
Artisan::call('migrate', [
'--database' => $config_key,
'--path' => 'database/offline/'.$type.'/migrations',
]);
After a whole lot of issues I was able to sort it this way;
In Laravel 5 there seem to be a difference in Config::set(), config('config',['key' => 'value]) and config()-set('config', ['key' => 'value']).
After a lot of testing different variant we managed to get a solution this way;
$connection = 'connection';
$iterator = 0;
foreach ($files as $file) {
App::useEnvironmentPath('/env');
App::loadEnvironmentFrom('.file.env');
// Create a new connection "on the fly"
config()->set('database.connections.' . $connection . '_' . $iterator, [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]);
// Call regular migration command
$this->call('migrate', ['--force' => true, '--database' => $connection . '_' . $iterator]);
$iterator++;
}
This manages to set multiple new connections to the MySQL-database, and then seed each one of them.
Thanks to #David Allen here for the inspiration.

Laravel: SQLSTATE[28000] [1045] Access denied for user 'homestead'#'localhost'

I just installed laravel and made a migration. But when i try to run it i get this error:
[PDOException]
SQLSTATE[28000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
Can anyone tell me what is wrong? I think the Database.php file looks different than normal. Is this something new in the new Laravel?
My Database config:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'LaraBlog'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Hope someone can help me :)
I tried to make changes to database.php file present within config folder it looks something like this
'default' => 'mysql',
....
...
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'sample'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I am not using any VM, I am using my local machine, with the database user as root and password a null.
I have also changed my .env file and it looks something like this:
APP_ENV=local
APP_DEBUG=true
APP_KEY=zLzPMzs5W4FNNuguTmbG8M0iFqhIVnsP
DB_HOST=localhost
DB_DATABASE=sample
DB_USERNAME=root
DB_PASSWORD=null
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
Even after doing all the changes when I try to register using the registration form that is shipped with laravel, I try to add a user to my database I get the following error
After doing all the changes I cleared the cache and loaded it again and it seems to work for me now!
if any one is also facing the same issue just run the following commands
php artisan cache:clear
php artisan config:cache
I figured it out :) Had to chance the .env file :)
Is there any changes in the schemaes?
Shouldn't this work:
public function up()
{
// Create table with columns
Schema::create('users', function($table) {
$table->increments('id');
$table->string('username');
$table->string(Hash::make('password'));
$table->string('firstname');
$table->string('lastname');
$table->string('email')
$table->string('role');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// Insert table to database
Schema::drop('users');
}
Try to check out the ".env" file in your root directory. These values are taken first. Yours are just the defaults if there are none given in the .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Your_Database_Name
DB_USERNAME=Your_UserName
DB_PASSWORD=Your_Password
Make changes in the database.php file in **config > local >database.php
rather than change config > database.php file.
hope it will work ;)
practically when you are using localhost server like xampp, you create the database manually "homestead", such errors are due to missing database arguments. 100% percent working test it and see
hit your cmd and type in:
create database homestead;
i got the same issue after creating laravel project. The authentication command run successfully.
php artisan make:auth
But when i try to register or login i got the same error. I run the following command
php artisan config:clear
then stopped laravel application and also my server. after restarting my laravel app and server everything was ok.
Tried all the solutions here but no work for me
This Worked on the first try:
If you are using the PHP's default web server (eg. php artisan serve) you need to restart your server after changing your .env file values.
I found this solution from here laravel.io (Read the solution of Byjml)

codeception laravel migration take the credentials form app/testing/database.php not codeception.yml

I noticed a strage behaviour in codeception:
if I rely only on codeception.yml with:
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
modules:
config:
Db:
dsn: 'mysql:host=localhost;dbname=test'
user: 'test'
password: 'test'
dump: tests/_data/dump.sql
run:
php artisan migrate --package=cartalyst/sentry --env="testing" php
artisan migrate --seed --env="testing"
I've got this error
[PDOException]
SQLSTATE[42000] [1044] Access denied for user ''#'localhost' to
database 'f orge'
if I put a file database.php in app/config/testing
with
return array(
'default' => 'mysql',
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'lama-test',
'username' => 'lama-test',
'password' => 'lama-test',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
),
all it's ok.
I'm new at codeception so I'm wondering what's the matter and if I make something wrong.
So there are two aspects to database access:
From your application
From codeception directly
From your application:
When running tests, your application needs to access your database as part of your code. i.e. when you search for the current user, or add a new blog post etc. This database access will occur through Laravel in "testing" mode - so it is good practice to define a "testing" database (which you have done).
From codeception directly:
When running tests, it is important that databases are the same before/after each test. That way a test does not rely on the output of a previous test etc. Codeception will use the database access defined in the yaml file to directly access the database, and 'refresh' it with your tests/_data/dump.sql file in between each test.
Codeception is not using the 'direct' database access during the actual test - just before/after.

Resources