-1

I am trying to upload files to a web server running Apache/2.4.6 on CentOS 7 using a simple Perl script. I get a permission denied message. My code:

[user@machine cgi-bin]$ cat test.pl 
#!/usr/bin/perl

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;

print header;
print start_html(-title => "Test");

print "<center> <h1> Hello </center>\n";

my $filename = "/home/apache/test.txt";

open(my $FILE, ">$filename"); 
if(-e $filename) {
    print "$filename exists\n"; 
    print $FILE "Hello\n";}
else {
    print "$filename does not exist\n";}

close($FILE);

print end_html;

After reviewing previous answers I changed permission to the /home/apache directory and changed owner to apache.

drwxrwxrwx.  2 apache   root       37 May 30 18:54 apache

Still, when I run the test.pl script manually the file is created, and when I open it in web browser the file does not. Help will be much appreciated. Thank you.

michagal
  • 11
  • 1
  • 1
    Is SELinux active in this host? If so, you may need to pay attention to the security context of the process trying to write to the directory and the directory as well. Check the audit log. – dawud May 30 '16 at 16:05

1 Answers1

2

CentOS 7 includes SELinux, which doesn't allow web server processes to write to user home directories (though it can be set up to allow read from user home directories).

To resolve the problem, choose a different directory, and set its SELinux context to allow writing (httpd_sys_rw_content_t). Typically, to avoid issues with SELinux, web content should be placed in directories under /srv/www or /var/www.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940