Mario Mueller
Mario Mueller

Reputation: 1470

Get apache linux user from php

I'm on a foreign linux system and need to determine the user that apache runs on (and so does php).

The aim: I need to get the owner of the script (this is no problem as I can use SplFileInfo) and compare it to the owner of the apache process.

I'm open to any alternative proposals.

Regards, Mario

Edit: Additional info:

The script is a thumbnail generator, that uses an XML file to generate thumbs from larger images. The script needs to create folders and write files. As I cannot influence the php configuration and I do not have any shell access, this has to be done very silently. The creation process stopps via exception and sends a mail on failue. As most of php's function cannot throw exceptions on failue, I need some manual checks to determine the environment I'm in. Therefore I need the apache user to compare it to some directory or fileowner.

Upvotes: 29

Views: 34258

Answers (5)

atmacola
atmacola

Reputation: 387

Some php script must be run on apache user (cli), whoami is not appropriate in that case. Here is my solution :

$output = exec('apachectl -S 2>/dev/null | grep User');
$apacheUser = preg_match('/name="([^"]+)"/', $output, $match) ? $match[1] : 'www-data';

Upvotes: 0

Jamie Carl
Jamie Carl

Reputation: 1204

Some complicated answers here.

This works for me:

$user = getenv('APACHE_RUN_USER');

Not sure if this is just a new thing that been added to apache since this question was asked but it's definitely there now.

Upvotes: 5

grantc
grantc

Reputation: 1723

You can call the php exec function to execute whoami:

<?php echo exec('whoami'); ?>

Upvotes: 57

Anonymous
Anonymous

Reputation: 3051

see posix_getuid() and posix_getpwuid()

Upvotes: 8

zombat
zombat

Reputation: 94147

phpinfo will dump a lot of system information. For apache2 installs, there is a section that displays the apache user and group ids. Try creating a php script that just has one line, a call to phpinfo(), and open it in your web browser.

Upvotes: 1

Related Questions