打开文件的权限问题 - Perl、CGI
Permissions issue on opening a file - Perl, CGI
我一直在尝试为一个应用程序创建一个日志文件,在 Apache 服务器中使用 Perl 和 CGI。为此,脚本需要保留一个日志文件,该文件必须在脚本启动时打开。从日志中我得到:Unable to open the file: Permission denied at /usr/lib/cgi-bin/generate_logs.cgi
,脚本如下:
#!/usr/bin/perl
use CGI qw(:standard);
print "Content-type: text/plain\n\n";
...
open (my $logs, ">", "/path/to/directory/logs.txt") || die "Unable to open the file: $!";
...
我一直在尝试为 .txt
日志文件使用非根路径,例如 ~/Desktop
,但脚本仍然无法 create/open 记录文件。如果您需要更多信息,请随时问我。提前致谢
Apache 网络服务器通常 运行 作为您系统上的独立用户。您的 ~/Desktop
目录将特定于 您的 用户。 Apache 写入的任何目录都需要由 Apache 用户写入。
要查找 Apache 在哪个用户名下运行,请检查您的配置文件。这可能默认为 /etc/httpd/conf/httpd.conf
。您可能会在该文件中找到带有 User
directive 的一行。它可能设置为用户 apache
。
Apache 已有一个日志目录。通常是 /var/log/httpd
。也许尝试在那里写你的日志。
请注意,您提供的示例代码存在很大问题。每次您的 CGI 脚本是 运行,您的日志文件将被擦除和覆盖。如果它是 运行 并发用户,你会遇到更大的问题。两个线程试图写入同一个文件、锁定问题等。您可能想看看 CGI::Log
以获得更多功能。
我一直在尝试为一个应用程序创建一个日志文件,在 Apache 服务器中使用 Perl 和 CGI。为此,脚本需要保留一个日志文件,该文件必须在脚本启动时打开。从日志中我得到:Unable to open the file: Permission denied at /usr/lib/cgi-bin/generate_logs.cgi
,脚本如下:
#!/usr/bin/perl
use CGI qw(:standard);
print "Content-type: text/plain\n\n";
...
open (my $logs, ">", "/path/to/directory/logs.txt") || die "Unable to open the file: $!";
...
我一直在尝试为 .txt
日志文件使用非根路径,例如 ~/Desktop
,但脚本仍然无法 create/open 记录文件。如果您需要更多信息,请随时问我。提前致谢
Apache 网络服务器通常 运行 作为您系统上的独立用户。您的 ~/Desktop
目录将特定于 您的 用户。 Apache 写入的任何目录都需要由 Apache 用户写入。
要查找 Apache 在哪个用户名下运行,请检查您的配置文件。这可能默认为 /etc/httpd/conf/httpd.conf
。您可能会在该文件中找到带有 User
directive 的一行。它可能设置为用户 apache
。
Apache 已有一个日志目录。通常是 /var/log/httpd
。也许尝试在那里写你的日志。
请注意,您提供的示例代码存在很大问题。每次您的 CGI 脚本是 运行,您的日志文件将被擦除和覆盖。如果它是 运行 并发用户,你会遇到更大的问题。两个线程试图写入同一个文件、锁定问题等。您可能想看看 CGI::Log
以获得更多功能。