Uploading files in CentOS – permission denied

michagal asked:

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:

[[email protected] cgi-bin]$ cat test.pl 

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";}


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.

My answer:

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.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.