M. Barbieri
M. Barbieri

Reputation: 542

Why am I getting a "permission denied" error with this Perl CGI script?

I am trying to make a Perl script that takes a remote host's IP address when they visit a certain website. However I can't seem to get past this apache error:

Permission denied at path_to_perl_script line 19

I am running a website on an Ubuntu server and I have configured Apache2 and CGI properly.

Here is the login.pl script:

#!/usr/bin/perl -T
use CGI;
use DBI;
use strict;
use warnings;
use Path::Class;
use autodie;    

# read the CGI params
my $cgi = CGI->new;
my $username = $cgi->param("username");
my $password = $cgi->param("password");

my $port = $cgi->remote_host();

my $dir = dir("var/www/html");
my $file = dir->file("testingPerl.txt");
my $file_handle = $file->openw();
$file_handle->print($port);

I am fairly new to Perl and I don't quite understand why I am getting this error.

Upvotes: 1

Views: 1686

Answers (1)

Sinan Ünür
Sinan Ünür

Reputation: 118166

You are getting a "permission denied" error because of this statement:

my $dir = dir("var/www/html");

The path var/www/html is relative to the script's current working directory and it is unlikely that it exsits. What you likely want is /var/www/html.

However, your script runs with the privileges of the user id under which the web server is running. In normal configurations, that user often is not allowed to write to /var/www/html. So, fixing that may not fix your problem.

Further, note that you don't need or want autodie if you use Path::Class or Path::Tiny: They both croak on error.

You can try this simple script to see if everything is working:

#!/path/to/perl -T

use strict;
use warnings;
use CGI;

my $cgi = CGI->new;
print $cgi->header('text/plain'), $cgi->remote_host, "\n";

Finally, it looks like you are going to overwrite the output file for each visitor.

Upvotes: 2

Related Questions