Frantisek
Frantisek

Reputation: 7703

Accessing root files with apache2

I have my apache2 server access log files in /var/log/apache2/access.log

The location is owned by root:root.

What I want to do is code my own server status (VirtualHost serving a .php file) which would parse the access.log and display some server statistics.

How can I make the access.log file accessible for my script/VirtualHost, but not for other sites on the server?

Upvotes: 0

Views: 101

Answers (2)

elitechief21
elitechief21

Reputation: 3054

You could give read and execute permissions to others for /var/log/apache2/ and read access to others for /var/log/apache2/access.log then in your php script that will show server status use the following code to get the content of the access.log file:

$access = shell_exec("cat /var/log/apache2/access.log");

Upvotes: 0

mwatts
mwatts

Reputation: 41

I would recommend that you reconfigure the (Virtual Host) instance (if possible) to log into an alternate location on your file-system.

In your *.conf file (located in a sub-directory of /etc/httpd/ or /etc/apache2/) for the instance just modify the logging to your destination of choice:

ErrorLog /var/www/{domain}/logs/{domain}-error.log
CustomLog /var/www/{domain}/logs/{domain}-access.log combined

Then verify that these changes (and any others that you might make) by running:

sudo /etc/init.d/httpd configtest

Then restart your webserver:

sudo /etc/init.d/httpd restart

This would permit you to change (dependent on your set up) the location that files were logged into and make the before mentioned file accessible for the VirtualHost and keep other Virtual Hosts intact - providing those have been unchanged. Just make sure that this directory belongs to the right group and contains the right permissions for the script accessing it.

I would recommend that you use another location to the one that was provided above as this is merely an example.

Reasons I would recommend not altering the permissions on /var/log

The '/var/log' directory contains explicitly detailed information that relates your operating system and by altering the permissions you would also potentially be enabling "outsiders" to access critical information that in turn could be used to compromise your system.

It's best to keep the permissions of this folder/directory as they are.

Upvotes: 1

Related Questions