【问题标题】:Apache/httpd /var/www/html/ .cgi scripts throw 500 internal server errorApache/httpd /var/www/html/ .cgi 脚本抛出 500 内部服务器错误
【发布时间】:2014-09-12 02:06:06
【问题描述】:

我今天安装了一个新的 CentOS 7 x86_64 LAMP 服务器。

我用 c 编译了一个简单的 CGI 脚本并将其命名为 test.cgi,并为 .cgi 脚本启用了 AddHandler。但是,每次我尝试从 /var/www/html 目录加载 /test.cgi 页面时,任何简单的 .cgi 脚本都会向我抛出 500 内部服务器错误页面。

我在 /var/www/cgi-bin 目录中测试了该脚本工作正常。 我的服务器正在运行 selinux,而 apache/httpd 正在使用 suEXEC。

编辑:在灯泡安装后我也没有创建任何额外的用户,所以我现在使用 root 来做所有事情。但是,我尝试修复将 /var/www/html 目录所有权授予 apache 用户的问题,但遗憾的是没有解决。

这是错误日志,如您所见,它给了我一个“权限被拒绝”错误:

[Mon Jul 21 15:28:14.336626 2014] [core:notice] [pid 22704] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[Mon Jul 21 15:28:14.339766 2014] [suexec:notice] [pid 22704] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Jul 21 15:28:14.495631 2014] [auth_digest:notice] [pid 22704] AH01757: generating secret for digest authentication ...
[Mon Jul 21 15:28:14.498690 2014] [lbmethod_heartbeat:notice] [pid 22704] AH02282: No slotmem from mod_heartmonitor
[Mon Jul 21 15:28:14.765072 2014] [mpm_prefork:notice] [pid 22704] AH00163: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips PHP/5.4.16 configured -- resuming normal operations
[Mon Jul 21 15:28:14.765186 2014] [core:notice] [pid 22704] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Mon Jul 21 15:28:16.027553 2014] [cgi:error] [pid 22706] [client 192.168.0.68:52930] AH01215: (13)Permission denied: exec of '/var/www/html/index.cgi' failed
[Mon Jul 21 15:28:16.030595 2014] [cgi:error] [pid 22706] [client 192.168.0.68:52930] End of script output before headers: index.cgi
[Mon Jul 21 15:45:01.586229 2014] [mpm_prefork:notice] [pid 22704] AH00170: caught SIGWINCH, shutting down gracefully

这是我的 /var/www/html apache 配置:

<Directory "/var/www/html">
    #                                                                                                                                                                                
    # Possible values for the Options directive are "None", "All",                                                                                                                   
    # or any combination of:                                                                                                                                                         
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews                                                                                                      
    #                                                                                                                                                                                
    # Note that "MultiViews" must be named *explicitly* --- "Options All"                                                                                                            
    # doesn't give it to you.                                                                                                                                                        
    #                                                                                                                                                                                
    # The Options directive is both complicated and important.  Please see                                                                                                           
    # http://httpd.apache.org/docs/2.4/mod/core.html#options                                                                                                                         
    # for more information.                                                                                                                                                          
    #                                                                                                                                                                                
    Options ExecCGI FollowSymLinks

    #                                                                                                                                                                                
    # AllowOverride controls what directives may be placed in .htaccess files.                                                                                                       
    # It can be "All", "None", or any combination of the keywords:                                                                                                                   
    #   Options FileInfo AuthConfig Limit                                                                                                                                            
    #                                                                                                                                                                                
    AllowOverride All

    #                                                                                                                                                                                
    # Controls who can get stuff from this server.                                                                                                                                   
    #                                                                                                                                                                                
    Require all granted
</Directory>

当然,我使用以下方式激活了 CGI:AddHandler cgi-script .cgi .pl

这是我的简单 test.c 文件:

#include <stdio.h>

int main(void) {
  puts("Content-Type: text/html; charset=ISO-8859-1\n");
  fputs("Hello, World!", stdout);

  return 0;
}

输出正确:Content-Type: text/html; charset=ISO-8859-1\n\nHello, World!

我也用 gcc 编译它,然后给它权限 777 到 test.cgi 你知道我需要做什么来解决这个问题吗?

提前致谢, 佐加托内

【问题讨论】:

  • 我在 Centos 7 上的 cgi 脚本遇到了同样的问题。我正在尝试安装 Bugzilla。我已经尝试了所有不同的权限,包括删除写入权限和将所有者切换到 apache.apache,但似乎都不起作用。
  • 500 错误非常普遍...检查日志

标签: linux apache centos cgi


【解决方案1】:

我不确定这是否对您来说是一个可行的解决方案,但我通过将 SELinux 更改为 permissive 来实现它。以下是您感兴趣的步骤。

vi /etc/selinux/config

更改以下行:

SELINUX=enforcing

到:

SELINUX=permissive

【讨论】:

    【解决方案2】:

    我刚刚解决了重新安装服务器并重新开始,禁用了 selinux 和 iptables,因为我已经有一个外部防火墙。

    感谢任何帮助过我的人;)

    【讨论】:

      【解决方案3】:

      这很可能是 SELinux 问题(Tom Sweeney answer 提供了使用许可 SELinux 和您自己接受的 answer 的解决方案,您表示要完全禁用 SELinux)。另一种方法是为您的 CGI 文件(以及可能的其他策略更改)配置适当的 SELinux 类型。


      首先,安装 SELinux 策略管理工具(如果尚未安装):

      sudo yum install policycoreutils-python
      

      假设您希望允许 /var/www/html 目录中的所有基于 CGI 的文件,您可以使用以下命令将 httpd_sys_script_exec_t 上下文应用到您当前和未来的 CGI 文件:

      sudo semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html(/.*)?/.*\.cgi'
      

      接下来,恢复任何现有 CGI 文件的内容:

      sudo restorecon -Rv /var/www/html/
      

      您还需要允许 Apache 允许使用以下命令执行 CGI 脚本:

      sudo setsebool -P httpd_enable_cgi 1
      

      你应该完成了。请注意,如果您的 CGI 脚本需要从系统中的其他文件读取/写入内容,您还必须将 httpd_sys_rw_content_t 上下文应用到这些文件(参见下面的示例)。


      刚刚在 CentOS 7 (x86_64) 系统上尝试安装 Bugzilla(使用 CGI)时遇到了这个问题。监控我的 httpd 错误日志 (sudo tail -f /var/log/httpd/error_log) 时观察到以下错误:

      [cgi:error] [pid 1825] [client ...:56481] AH01215: (13)Permission denied: exec of '/var/www/html/bugzilla/index.cgi' failed
      [cgi:error] [pid 1825] [client ...:56481] End of script output before headers: index.cgi
      

      检查应用到我的 Bugzilla 安装的上下文,我看到以下内容:

      $ ls -Z /var/www/html/bugzilla/
      ...
      -rwxr-x---. apache apache unconfined_u:object_r:httpd_sys_content_t:s0 index.cgi
      ...
      

      然后我使用以下命令来允许执行 Bugzilla 的 CGI 脚本以及访问所述 CGI 脚本以读取 ./data 目录中的内容:

      sudo yum install policycoreutils-python
      sudo semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/bugzilla(/.*)?/.*\.cgi'
      sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/bugzilla/data(/.*)?'
      sudo restorecon -Rv /var/www/html/bugzilla/
      sudo setsebool -P httpd_enable_cgi 1
      

      检查应用的上下文显示所需的结果:

      $ ls -Z /var/www/html/bugzilla/
      ...
      -rwxr-x---. apache apache unconfined_u:object_r:httpd_sys_script_exec_t:s0 index.cgi
      ...
      

      Bugzilla 现在应该可以使用了。对于 Bugzilla 提供的所有功能,可能有额外的政策适用;但是,我不知道是否需要任何其他政策。

      【讨论】:

        【解决方案4】:

        这是 SELinux 权限问题,不建议禁用 SELinux。 请检查您的应用程序是否在 SELinux 的 permissive 模式下工作,那么它是 SELinux 问题。

        找到下面的 URL 以检查确切的问题,我们将从 /var/log/audit.log 获取阻止内容

        https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/using_selinux/troubleshooting-problems-related-to-selinux_using-selinux

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-30
          • 2011-10-09
          • 2013-11-13
          • 2016-04-24
          • 2013-05-30
          • 1970-01-01
          相关资源
          最近更新 更多