How to get username (only) of the Apache?
I am trying to write a Bash script for automatic chown'in files to the Apache2 user. But I need before to be 100% sure what is the username of it? I know that usually it is www-data. But, I need to be sure if, say, before the username has been somehow changed.
I am looking for a string that will return ONLY username of Apache2.
I know # ps -aux | grep apache2 and many others, but they return a bulk of data and I just need a username.
Any help is highly appreciated!
Thank you.
13 Answers
The username and group ID of Apache2 is set by a directive in the configuration file. This is located at in the file /etc/apache2/envvars.
You can examine that file and notice that by default, as you suspected, it's www-data. Also, as you can see from the file the usergroup is set by default to the same name.
Now that you have the effective user of apache you can use this to change the files to that user:
$ sudo chown -R www-data:www-data myhtmldirectoryThat will change the user and group owner of myhtmldirectory and all files and directories below it to www-data.
If you want to just change the user owner, drop the :www-data part which is for group.
A very sure way of having the correct usrID is:
$ awk -F= '$1 == "export APACHE_RUN_USER" {print $2}' /etc/apache2/envvarsThis line will parse the envvars file for the current session that was started.
9To find the names of the effective user of any process matching apache2, use:
ps -o euser= -C apache2The -o option sets the output format. In our case, we ask just for the effective useer name, euser. The trailing = tells ps to suppress the normal header line. This way, the output consists only of user names matching apache2.
I don't have apache2 running, so as an example:
$ ps -o euser= -C mount.ntfs
root
$ ps -o euser= -C atd
daemon 3 I found a great easy solution, that returns ONLY apache2 username:
ps -ef | egrep '(httpd|apache2|apache)' | grep -v `whoami` | grep -v root | head -n1 | awk '{print $1}'Thanks.
4More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"