spatie/global-ray

Can't instal on linux because permissions

abkrim opened this issue · 8 comments

Ubuntu 20.04

I'm user registered for ray.

If try install with sudo get same problem. Is normal, file php.ini are files of system with onwer root

composer global require spatie/global-ray
Changed current directory to /home/abkrim/.composer
Info from https://repo.packagist.org: #StandWithUkraine
Using version ^1.0 for spatie/global-ray
./composer.json has been updated
Running composer update spatie/global-ray
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking spatie/global-ray (1.0.2)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading spatie/global-ray (1.0.2)
  - Installing spatie/global-ray (1.0.2): Extracting archive
Generating autoload files
13 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
❯ global-ray install

   ⚡️
   debug your applications faster.

   🌎 For more info visit: https://myray.app

   💪 By installing global-ray you will be able to use
   these functions in any PHP script: ray(), rd(), dump(), dd()
   
   You can chain a lot of useful functions on ray()
   - ray()->green(): colorize the output in Ray
   - ray()->pause(): pause your code
   - ray()->measure(): quickly measure runtime and memory
   - ray()->trace(): see where your function is being called
   - ray()->charles(): Let's dance!

   📗 You can see more Ray functions in the docs:
   https://spatie.be/docs/ray/v1/usage/framework-agnostic-php-project

   🤙 Do you wish to install Ray globally? (Y/n) > Y

   Updating PHP ini: /etc/php/8.1/cli/php.ini...



PHP Warning:  file_put_contents(/etc/php/8.1/cli/php.ini): Failed to open stream: Permission denied in /home/abkrim/.composer/vendor/spatie/global-ray/src/Support/PhpIni.php on line 36
   ❌ Unable to update PHP ini.

``
sudo global-ray install
sudo: global-ray: orden no encontrada


I put here beacuse thre're some issues wiyh links on "Ask a question" 
![Castris_20220309_160459](https://user-images.githubusercontent.com/1238625/157469832-05e05073-6dfb-4a26-817c-ebd4989367f6.jpg)

Hi @abkrim,

Strange... Maybe you have to install global-ray with sudo if access to your php.ini is denied, since sudo may run under a different context? Otherwise, you will have to update your php.ini manually.

I've just added instructions to the global-ray install command that displays the option you must set in your php.ini to have global ray installed. To get these instructions, run composer global update and then global-ray install.

Let me know if the above works! 👍

This seems to be exact same issue I have with CentOS
spatie/ray#648
I'm planning on trying to find a solution for this tonight, will post back if I did.

FYI just adding it manually causes any website to instead get a

Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0
Fatal error: Unknown: Failed opening required '/home/jonathan.dejong/.config/composer/vendor/spatie/global-ray/src/scripts/global-ray-loader.php' (include_path='.:/usr/share/pear:/usr/share/php') in Unknown on line 0

so it seems there's the issue of that user path not being accessible by the apache user as well..
Or phrased differently:

The problem is that I have a system where you have users:

  • Root = we do not mess with this user unless we really have to
  • Apache (also often referred to as www-data) = This is the user that runs Apache and PHP which reads the php.ini file and is responsible for the "output" of my sites.
  • jonathan.dejong = My own user. this is the user I SSH in with etc.

So when I run composer global require spatie/global-ray I install the package in my global composer bin for jonathan.dejong.

When I try to run global-ray install it will fail to update php.ini as my user does not have permission to write to that file owned by root without sudo.
When I try to run sudo global-ray install the command will not be recognized as I am now trying to run a command in the context of root where it does not actually exist.
When I try to run sudo /home/jonathan.dejong/.config/composer/vendor/spatie/global-ray/bin/global-ray install it works as I've specified the path to the bin I want to run as sudo.
Cool! So now I have my value added to auto_prepend_file in php.ini.

I also need to restart apache (or httpd as some call it inc. me) for the php.ini changes to take effect so:
sudo systemctl restart httpd
All good!

Now I'll try to visit a site running on this server. I'll get:

Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0
Fatal error: Unknown: Failed opening required '/home/jonathan.dejong/.config/composer/vendor/spatie/global-ray/src/scripts/global-ray-loader.php' (include_path='.:/usr/share/pear:/usr/share/php') in Unknown on line 0

Because the apache user does not have the permission to access my users .config/composer directory.

I don't want to modify the chmod or chown of that directory for my user. I also don't see it as viable to try and log in as the apache/www-data user and run up a whole composer and install it for that user.

Not really sure what the best approach here is..? 🤔

It might just be a case of "f*ck it, just install the package per project instead"

Hey @jonathan-dejong, thanks for the detailed issue, much appreciated 🙏

I'm not sure if we'll be able to work around your use-case. Are you able to install PHP and Apache underneath your user account? This is the assumption that global-ray makes right now...

The only way we could likely resolve this off the top of my head would be to copy the global-ray directory from your global composer directory into your PHP folder and update your PHP ini manually to point to the global-ray-loader.php script...

Yeah I'm afraid I can't change the setup of the server.. or well, I suppose I could but it'd go against the larger points of how to setup a server. If this was a local install like it is when you run say mamp or local by flywheel etc. it'd be a different thing ofc :)

I think you're right, that'd probably work, but it'd be troublesome to maintain as you make updates to this repo. It's probably smarter admitting that the use case and the idea of global-ray doesn't fit together. It's not a big deal grabbing it per project instead in modern PHP development where you pretty much always have a project specific composer setup anyway :)

Perhaps you can clean up my late-night ramblings above into a new thing under "known issues" or "incompatibility" in the readme so you won't continue to get these issue tickets 😄

vsg24 commented

I'm also having this problem with a Docker setup.

Closing this as unfixable due to PHP requiring read access to the user’s Composer global directory to be able to execute properly.

@jonathan-dejong i don't know if you still wanted to try and get this working.

i'm running WSL for local development and have done a bit of an small work around to get this working.

I placed my websites in /mnt/c/wsl/www (it's windows c drive then wsl -> www folder in WSL).
In the www folder i made a new folder global-ray/php8.1 and did the composer require there.
Because the www folder is accessible for PHP you won't get the failed to load error as long as you set the rights correct for it.

Then i manually added "auto_prepend_file = /mnt/c/wsl/www/global-ray/php8.1/vendor/spatie/global-ray/src/scripts/global-ray-loader.php" to /etc/php/8.1/fpm/php.ini at the bottom of the file and restarted my PHP.

Only problem i had was getting this also to work for PHP7.4 because i got an "Call to undefined function str_starts_with()" error. But at least i got it working for PHP8.1, so for all new projects.