I have a statistics-file on my apache server on


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?

  • what does ls -l /var/www/vhosts/test/statistics/logs/access_log say? Jul 24, 2013 at 18:13
  • @UlrichSchwarz -rw-r--r-- 1 root root 130771 Jul 24 20:19 /var/www/vhosts/test/statistics/logs/access_log Jul 24, 2013 at 18:19
  • This serverfault link might clear things up for you. serverfault.com/questions/337022/…
    – Tim
    Jul 24, 2013 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. Jul 24, 2013 at 22:11
  • @Tim This didn't solve it. Jul 24, 2013 at 22:13

2 Answers 2


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

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.

  • not a suitable approach. Jul 24, 2013 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');


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

  • 1
    Yes. No permission using your approach. Jul 24, 2013 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, 2013 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, 2013 at 19:52
  • @aetimmes it might be wr but still I get permission denied Jul 24, 2013 at 22:14
  • @Zurechtweiser My bet would be on open_basedir restrictions -- those would show up in the error_log. Jul 25, 2013 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .