Permission denied, error log

15,388

I got this is a server permission problem, but i am working on a localhost (Xampp on Mac os x) and i'd like to manage these permissions. How can i place log/ to be writable only? Is there any better ways of doing what i am trying to do?

You might want to checkout set_error_handler, to make sure all errors are automatically sent through your custom function. Anyway, that's not the source of your problem, your issue is actually the permissions on the server. Now, the first thing to do is to determine as what user PHP is running. This can be very simple to determine, you just have to run this script:

<?php
echo `whoami`;

In case of your Mac, that probably is 'www-data'. If you want to change that (possibly), you should delve into how (f.e.) Apache MPM ITK works. If you're fine with 'www-data' running your scripts, you should give 'www-data' access to the log directory. First, go to the parent directory and issue command 'ls -laF', to see detailed information about the log directory. Let's say the output is the following:

rwxr-xr-x 2 berry berry 4096 2011-01-18 16:53 log/

In this case, I (the user "berry") would be the owner. The group that I'm primarily in (namely, "berry") is the group that owns the directory. Now, you can tell by the rwx r-x r-x, that the owner (first rwx) can read, write and execute. The second (the group) can read and execute. The others (everyone else) can read and execute.

"www-data" is probably not in the group "berry". To fix this, you want to issue the command: chgrp -R www-data log/, followed by chmod -R g+x log/. That means: change the owning group to "www-data", and give the group (g) execute permission (+x), do that recursively (-R) on directory "log/".

Good luck.

Share:
15,388
Shoe
Author by

Shoe

I like C++, Haskell, PostgreSQL, Prolog and you.

Updated on June 04, 2022

Comments

  • Shoe
    Shoe almost 2 years

    I'm trying to create a simple error log that write down php errors in case these cannot be reported to the admin (with a database connection). I wrote this:

    function errorLog($array)
    {
        $time = date("F j, Y, g:i a");
        $ip = $_SERVER['REMOTE_ADDR'];
        $no = $array['e-no'];
        $string = $array['e-string'];
        $file = $array['e-file'];
        $line = $array['e-line'];
        $context = $array['e-context'];
    
        $string = "TIME {$time} IP {$ip} ERROR {$no} | {$string} | {$file} | {$line} | {$context}";
        if ($handler = fopen('log/log-file.txt', 'a'))
        {
            fwrite($handler, $string);
            fclose($handler);
        }
        else
        {
            exit('Fatal Error #0002');
        }
    }
    

    START EDIT

    Function had became

    function errorLog($array)
    {
        $time = date("F j, Y, g:i a");
        $ip = $_SERVER['REMOTE_ADDR'];
        $type = $array['e-type'];
        $string = $array['e-string'];
        $file = $array['e-file'];
        $line = $array['e-line'];
        $context = $array['e-context']
        $string = "ON {$time} IP {$ip} ERROR {$type} | {$string} | {$file} | {$line} | {$context}"; 
        if (!error_log($string, 3, 'log/log-file.txt')) { exit('Fatal Error #0002'); }
    }
    

    END EDIT

    But when i run it i get

    Warning: fopen(log/log-file.txt) [function.fopen]: failed to open stream: Permission denied in myfile.php on line 17
    

    I got this is a server permission problem, but i am working on a localhost (Xampp on Mac os x) and i'd like to manage these permissions. How can i place log/ to be writable only? Is there any better ways of doing what i am trying to do?