aboboras has asked for the wisdom of the Perl Monks concerning the following question:

I'm using Perl/Ubuntu/Apache. My script is on /usr/lib/cgi-bin and i'm trying to write a file(open ">") to /var/www/my_custom_dir but i get "Permission denied".
If i try to write to the script directory or any other the error is the same. The permissions seem right. Everything is 755, root owner. I tried to change "my_custom_dir" owner to www-data(apache user), but does not make any difference.

Replies are listed 'Best First'.
Re: Perl can't write files
by kcott (Archbishop) on Apr 10, 2014 at 05:53 UTC

    G'day aboboras,

    You should probably start by showing us the relevant code (in <code>...</code> tags). 'open ">"' isn't telling us a lot. Check your code follows the examples in the open documentation.

    Next, show us the full error message (in <code>...</code> tags). Where does this message appear: in the browser, in one of the Apache logs, somewhere else?

    Giving only root write access probably isn't correct: that's unlikely to be the user involved. Check your Apache configuration for User.

    Have you checked your various Apache logs? Do their entries help you in any way? If not, show us: maybe we can help you with that.

    The "Security Tips - Apache HTTP Server" document may be helpful to you.

    The guidelines in "How do I post a question effectively?" will show you the sort of information you can provide to us such that we are better able to help you.

    -- Ken

      open ( UPLOADFILE, ">$filepath" ) or die "$!"; binmode UPLOADFILE; while ( <$file> ) { print UPLOADFILE $_; } close($file); close UPLOADFILE; }
      It "die" on the the first line. The error on log is "Permission denied at /usr/lib/cgi-bin/script.cgi"
        Does script.cgi have execute permissions? If not:
        chmod +x script.cgi
Re: aboboras can't write files
by Anonymous Monk on Apr 10, 2014 at 06:52 UTC

    Use sub Fudge to get a better error message

    Also, check your sever config for things like chroot/SELinux/permissions

    Also, write better code than Re^2: Perl can't write files, that could never do anything, its incomplete

      How it could not do anything? It was previously working on Windows.

        How it could not do anything? It was previously working on Windows.

        Like I said, its incomplete, its a fragment, many parts are missing , it is not self-contained , it doesn't compile, it doesn't run ...

        $ perl abob Unmatched right curly bracket at abob line 13, at end of line syntax error at abob line 13, near "}" Unmatched right curly bracket at abob line 14, at end of line Execution of abob aborted due to compilation errors. $ perl abob No such file or directory at abob line 2.

        Do you understand ?

Re: Perl can't write files
by locked_user sundialsvc4 (Abbot) on Apr 10, 2014 at 14:07 UTC

    It’s fairly-obviously a permissions problem:   the user-id for the web server is probably nobody, which for very obvious reasons “isn’t allowed to do squat!”   Windows handles permissions somewhat differently, but the principle and therefore the essential problem is the same.

    And of course, with regards to web servers you should be practicing the “principle of least privilege.”   Web servers are built to respond to (unpredictable, and quite possibly malicious) requests from the outside world-at-large.   You therefore want to strictly limit what they are capable of doing, touching, or even seeing.   That way, if someone somehow clocks your server in the head and fools it into asking the operating system to do something really nasty, the operating system will say:   “not only ‘no,’ but ...!!”   And there will be nothing that the web server (nor the person who secretly took it over) can do about it.

Re: Perl can't write files
by pvaldes (Chaplain) on Apr 10, 2014 at 14:34 UTC
    Check also the parent directories permits, this is a typical situation when you try to access a file that is inside a restricted directory.
Re: Perl can't write files
by Preceptor (Deacon) on Apr 10, 2014 at 16:19 UTC

    Check which user the httpd is running as.

    ps -ef | grep apache

    It is EXTREMELY unwise to run httpd and especially CGI scripts as root. So most people don't.