7

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
Marc
  • 1,350
  • 2
  • 11
  • 29
  • Where do you have those environment variables set up? – Daniel Protopopov May 18 '20 at 08:48
  • Because it is an EB environment, there are automatically provided by AWS to the server instances, there is no need to set them up manually. – Marc May 18 '20 at 10:23
  • Some information here : https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_PHP.rds.html#php-rds-connect I use Laravel, so I access them using `env('RDS_XXX')` instead of `$_SERVER` but that's quite similar. – Marc May 18 '20 at 10:26
  • @Marc I am also facing this same problem and I can not find what the issue is. Please let me know if you found a fix for it. – realnsleo May 18 '20 at 11:06
  • @realnsleo That's why I put a bounty on this. I looked all over the internet and didn't find anything relevant. – Marc May 18 '20 at 12:15

1 Answers1

15

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.

Claire
  • 330
  • 2
  • 5
  • Thank you Claire! I have come across an issue however, the above does not escape values that are stored in `/opt/elasticbeanstalk/deployment/env` that contain spaces and as such, my commands are using different environment variables to my application. Any suggestions on how one can fix this? – Ben Carey Mar 05 '21 at 05:39