Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a statistics-file on my apache server on

/var/www/vhosts/test/statistics/logs/access_log

I tried using

echo system('cat /var/www/vhosts/test/statistics/logs/access_log');

in a php script to access it but I get no output.

echo system('dir');

gives the expected dir output.

cat /var/www/vhosts/test/statistics/logs/access_log

using a putty connection and as root works though.

How can I access the log via the php script?

share|improve this question
 
what does ls -l /var/www/vhosts/test/statistics/logs/access_log say? –  Ulrich Schwarz Jul 24 at 18:13
 
@UlrichSchwarz -rw-r--r-- 1 root root 130771 Jul 24 20:19 /var/www/vhosts/test/statistics/logs/access_log –  Zurechtweiser Jul 24 at 18:19
 
This serverfault link might clear things up for you. serverfault.com/questions/337022/… –  Tim Jul 24 at 19:45
 
I tried chmod and stuff but each time the access_log is being updated, it rewrites old permission. All I want is output this data. –  Zurechtweiser Jul 24 at 22:11
 
@Tim This didn't solve it. –  Zurechtweiser Jul 24 at 22:13
show 2 more comments

2 Answers

Your HTTP process cannot access the file. A CentOS 6 Apache process runs as the user apache. Example:

[root@talara ~]# ps aux | grep httpd
root      2300  0.0  2.7 334744 28020 ?        Ss   Jul10   0:47 /usr/sbin/httpd
apache    8354  0.0  2.0 334744 20536 ?        S    Jul21   0:00 /usr/sbin/httpd
apache    8355  0.0  2.0 334744 20536 ?        S    Jul21   0:00 /usr/sbin/httpd
apache    8356  0.0  2.0 334744 20536 ?        S    Jul21   0:00 /usr/sbin/httpd

To test accessing the file as apache I do:

[root@talara ~]# su apache
bash-4.1$ cat /var/log/messages
cat: /var/log/messages: Permission denied
bash-4.1$

Most ways I know of to allow apache direct access to those files will be frowned upon in one way or another due to the HUGE security hole it opens. However if you think you have security locked down enough, add apache to the sudoers file with no password (see? VERY very bad). Then change your PHP code to something like this:

echo system('sudo cat /var/www/vhosts/test/statistics/logs/access_log');

Input from the community on a better approach would be welcome.

share|improve this answer
 
not a suitable approach. –  Zurechtweiser Jul 24 at 22:13

Is there a specific reason why you're making system calls to access the file?

If not, you can just use the existing constructs within PHP to get access to the file:

$log = file_get_contents('/var/www/vhosts/test/statistics/logs/access_log');

or

$log = file('/var/www/vhosts/test/statistics/logs/access_log');

share|improve this answer
 
Yes. No permission using your approach. –  Zurechtweiser Jul 24 at 18:32
 
The file is world-readable according to the ls output you pasted earlier - any user or service account should be able to get the contents. What error message do you get when attempting to use file_get_contents? –  aetimmes Jul 24 at 18:38
 
@aetimmes as you can see from my su approach above, your method would not have worked on my centos install. –  Tim Jul 24 at 19:52
 
@aetimmes it might be wr but still I get permission denied –  Zurechtweiser Jul 24 at 22:14
 
@Zurechtweiser My bet would be on open_basedir restrictions -- those would show up in the error_log. –  Ulrich Schwarz Jul 25 at 8:00
show 1 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.