I have a symfony2 application on my ubuntu. Symfony has a plenty of useful console commands (like php app/console cache:clear
or php app/console assets:install web
).
The problem is If I run them as root
user the newly generated files will have root:root
user/group, and if I acces my website I get errors (becouse apache cannot read/modify these files -> they should have www-data:www-data
).
Running chown www-data:www-data
solves the problem, but running it every time I clear my cache is not a solution.
How can I configure PHP CLI to always run as www-data user/group?
or
Best Answer
Run a command as another user once:
This should work if you have
sudo
installed and areroot
(or another user that is allowed to do that; see thesudo
group,man sudoers
andvisudo
).For reusability, add an alias. Place this in your
.bashrc
,.profile
or similar (and reload the shell to make it effective):You can then type
phpwww script.php
and it will actually executesudo -u www-data php script.php
for you.For other, more complex and error-prone ways, read on.
As for always running php as
www-data
, there are several possiblities. You could create a simple wrapper shellscript. If/usr/bin/php
is only a soft-link to/usr/bin/php5
or similar, that makes it simpler. Just replace the soft-link (NOT the filephp5
) with a script like this:That's not tested though. Also be aware that this will ALWAYS try to run
php5
as userwww-data
, even if the user may not beroot
and may not have permission to do so. And it may also not be what you really want. Some installed services may run into problems when trying to execute php.A (possibly better) solution to only apply that to root may be to leave the soft-link
/usr/bin/php
alone and place the script in/root/bin
instead. Then add that folder to PATH via.bashrc
,.profile
or similar. If you have/etc/skel/.profile
, that may point out how that is done:Once this is in your
.bashrc
,.profile
or similar, every new shell you open will allow you to directly execute any executables (+x) in$HOME/bin
(/root/bin
for root).Hint: You may want to name the wrapper script something like
phpwww
so you explicitly specifyphp script.php
orphpwww script.php
to decide if you want regular or sudo'ed php.