geerlingguy / ansible-role-composer

Ansible Role - Composer PHP Dependency Manager
https://galaxy.ansible.com/geerlingguy/composer/
MIT License
182 stars 104 forks source link

Composer Path Web User #38

Closed jasperf closed 7 years ago

jasperf commented 7 years ago

Trying to set up composer so I can use my web user to install Laravel. So I added

composer_home_path: '/home/web/.composer'
composer_home_owner: web
composer_home_group: www-data
composer_add_to_path: true

but composer does not get added to the web user's path:

web@Laravel:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

When I only added

composer_global_packages: 
  - { name: laravel/installer }

it did get added to the root user path:

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/root/.composer/vendor/bin

I only thought it would get added to the system path as stated in the readme:

If true, and if there are any configured composer_global_packages, the vendor/bin directory inside composer_home_path will be added to the system's default $PATH (for all users). When I check what I think is the Ubuntu 17.0.4 system default path I saw no composer added either

cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

I must be missing something..

geerlingguy commented 7 years ago

I haven't tested on Ubuntu 17, so no guarantees there... but this should work the same whether using root or another user.

This task: https://github.com/geerlingguy/ansible-role-composer/blob/master/tasks/global-require.yml#L13-L18

Uses this template: https://github.com/geerlingguy/ansible-role-composer/blob/master/templates/composer.sh.j2

To put composer paths in the global $PATH settings.

Note, however, that if you do this in one terminal session, you would need to then log out and log back in to see the updated $PATH. Otherwise the new global environment variables won't be read in.

jasperf commented 7 years ago

I think the issue I am having is that I just need to have composer available for the web user. And as I install it as root composer and laravel become available for root and are added to the global path for root. I just need it to work for either both root and web or just for web alone. To have composer installed at /usr/local/bin/composer root access is needed or sudo. The user web will not be sudo as I want that user only to work in the webroot. So in this case I have to install the role as root. Doing a new installation now and reading some on adding composer to path like https://stackoverflow.com/questions/25373188/laravel-installation-how-to-place-the-composer-vendor-bin-directory-in-your once again. Will do some more trials with perhaps additional commands as tasks.

jasperf commented 7 years ago

Solved it by adding

 - name : Add Composer to path
      lineinfile: dest=/home/web/.bashrc state=present line='export PATH="$PATH:$HOME/.composer/vendor/bin"'

to user web. And for composer I am using these variables:

composer_global_packages:
  - { name: laravel/installer }
composer_home_path: '/home/web/.composer'
composer_home_owner: web
composer_home_group: www-data
composer_add_to_path: true