Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel artisan tinker from Amazon Linux 2 (Elastic Beanstalk)

I used to execute tinker on previous Amazon AMI by using this command :

sudo -E -u webapp php artisan tinker

Now I am using PHP 7.4 on Amazon Linux 2 and when I execute the above command, I get this error :

Unable to create PsySH runtime directory. Make sure PHP is able to write to /run/user/1000 in order to continue.

So, In order to test, I gave the full permission to this folder and then executed my command again :

sudo chmod 777 /run/user/1000
sudo -E -u webapp php artisan tinker

Actually, no error, but the environnement variables such as RDS_PASSWORD or RDS_DB_NAME are not loaded, making it impossible to perform database actions :

Psy Shell v0.10.4 (PHP 7.4.4 — cli) by Justin Hileman
>>> env('RDS_DB_NAME')
 => null
>>> User::first()
 Illuminate/Database/QueryException with message 'SQLSTATE[HY000] [2002] Connection refused 
 (SQL: select * from `users` limit 1)'

I also tried to echo the variable from a shell, I got the same result :

sudo su webapp
sh-4.2$ echo $RDS_DB_NAME
  # Empty here
like image 831
Marc Avatar asked May 15 '20 09:05

Marc


People also ask

What is tinker command in Laravel?

Tinker allows you to interact with your entire Laravel application on the command line, including your Eloquent models, jobs, events, and more. To enter the Tinker environment, run the tinker Artisan command: php artisan tinker.

Why we use php artisan tinker?

The php artisan is a command-line interface that is available with a Laravel. Tinker is a command tool that works with a php artisan. A tinker plays around the database means that it allows you to create the objects, insert the data, etc. The above screen shows that the tinker environment has been created.

What is meant by php artisan?

Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component.


Video Answer


1 Answers

It's not on the documentation but the environment variables are in /opt/elasticbeanstalk/deployment/env

So you can do something like this:

export $(sudo cat /opt/elasticbeanstalk/deployment/env) && sudo -E -u webapp php artisan tinker

Also, to deal with the PsySH problem, just create a .psysh.php file in your source code directory with that content:

<?php

return [
    'runtimeDir'    => './.psysh',
];

Psysh will then use that directory without any permission problem because webapp owns it.

like image 65
Claire Avatar answered Sep 25 '22 12:09

Claire