Can't use apache/www-data to run commands, which user should be used?

Re: Can't use apache/www-data to run commands, which user should be used?

by Lexi K -
Number of replies: 2

Hello Emma, ok, I'll change it once I am sure it really works.

Yeah I'm logged in as root, as the site I linked to says logging in as www-data is disabled on purpose beacuse you shouldn't do it. I don't understand the reason why that is but it looks like there's a better way.

In reply to Lexi K

Re: Can't use apache/www-data to run commands, which user should be used?

by Matteo Scaramuccia -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

Hi Lexi,
which Debian version?

For a quick smock test, I've just started a docker container running PHP 7.2.8 on stretch as root and from a bash "console":

# cat /etc/debian_version
9.5
# getent passwd www-data
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
# echo "<?php echo posix_getpwuid(posix_geteuid())['name'].\"\\n\";" > /tmp/test.php
# cat /tmp/test.php
<?php echo posix_getpwuid(posix_geteuid())['name']."\n";
# php /tmp/test.php
root
# apt-get update
# apt-get install sudo
# which php
/usr/local/bin/php
# sudo -u www-data /usr/local/bin/php /tmp/test.php
www-data

HTH,
Matteo

Average of ratings: Useful (1)
In reply to Matteo Scaramuccia

Re: Can't use apache/www-data to run commands, which user should be used?

by Lexi K -

Hello, thanks for the help. I was correct in that it's not possible to switch to/login as www-data. However it's not necessary. I thought su username -c "command" does the same as sudo -u username command but it turns out su tries to log in as that user and fails if it doesn't work and sudo does not log in as that user but just runs the command as that user. So, all is correct and I just made a wrong assumption smile

Regards,

Lexi

Average of ratings: Useful (1)