【问题标题】:How to make Apache serve index.php instead of index.html?如何让 Apache 服务 index.php 而不是 index.html?
【发布时间】:2013-04-18 00:10:58
【问题描述】:

如果我将以下行放入 index.html 文件中,以使 Apache 包含 index.php 文件:

  <?php include("/Live/ls_client/index.php"); ?>    

访问index.html 页面告诉我:

<!--?php include("/Live/ls_client/index.php"); ?-->

这是为什么呢?为什么它实际上不包含 PHP 文件?

【问题讨论】:

    标签: php apache include


    【解决方案1】:

    您可以通过两种方式设置优先顺序,例如 (index.php index.html ...) 以在文件中设置优先级转到此目录-/etc/apache2/mods-enabled/nano dir.conf
    并根据需要更改顺序 2-insecond 的方式你可以去/etc/apache2/sites-available/nano abc.com.conf 并在 Documentroot 目录中更改 -/var/www/abc.com/public_html/(给出你想要的文件,如 index.html 或 index.php)

    【讨论】:

      【解决方案2】:

      截至今天(2015 年 8 月 1 日),Apache2 在Debian Jessie,您需要编辑:

      root@host:/etc/apache2/mods-enabled$ vi dir.conf 
      

      并更改该行的顺序,将 index.php 带到第一个位置:

      DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
      

      【讨论】:

      • 2018 年依然强劲
      • 是的,3 岁生日快乐,亲爱的答案(迟到了 2 天)!
      【解决方案3】:

      正如其他人所指出的,您很可能没有设置 .html 来处理 php 代码。

      话虽如此,如果您所做的只是使用index.html 来包含index.php,那么您的问题可能应该是'如何使用index.php 作为索引文档?

      在这种情况下,对于 Apache (httpd.conf),搜索 DirectoryIndex 并将该行替换为该行(仅当您启用了 dir_module 时才有效,但这是大多数安装的默认设置):

      DirectoryIndex index.php
      

      如果您使用其他目录索引,请按优先顺序列出它们,即

      DirectoryIndex index.php index.phtml index.html index.htm
      

      【讨论】:

      • 您也可以使用DirectoryIndex index.php index.html - 如果它们混合在一台服务器上会很有用。
      • 我不喜欢冒泡这些老问题(而且答案很好),但这个解决方案对我不起作用。正如 "grep -ri 'DirectoryIndex' /etc/apache2/*" 所证明的,我的配置只包含一个 DirectoryIndex 索引行。不加注释,值为 DirectoryIndex index.php index.html。然而即使在重新启动后,服务器仍然提供 index.html 文件——我必须输入 hostname/index.php 才能让它使用该文件。如有必要,我将提出一个新问题。 UBUNTU-20.04 LTS
      • 我遇到了完全相同的问题,但找不到 DirectoryIndex 的位置。我所做的解决方法是将我的 index.html 文件重命名为 index.html.org,接下来就是我的 index.php 被浏览器拾取。我仍然想知道这个 DirectoryIndex 行在哪里。我记得在站点的 000-default.conf 中看到它 - 在旧 apache 中可用,但在 ubuntu 20.04 上的 apache2 中没有。
      • 大声笑,我没有阅读整个线程。 DirectoryIndex 在 ubuntu20.04 上的 /etc/apache2/mods-available 目录中的 dir.conf 文件中
      【解决方案4】:

      PHP 仅适用于 .php 文件扩展名。

      如果您使用的是 Apache,您还可以在 httpd.conf 文件中设置 PHP 的扩展名。你必须找到这条线:

      AddType application/x-httpd-php .php .html
                                           ^^^^^
      

      并根据需要添加多少扩展,应该使用 PHP 解释器读取。

      【讨论】:

      • 虽然您可以在您的 apache 配置中将 .html 扩展添加到 php 处理程序
      • 另外,可以将服务器配置为接受 index.php 作为欢迎页面,从而无需多余的 index.html。
      • @MarioP - 是的,这也是我想到的第一件事。 :P 有时你必须明白为什么有人要问一个问题才能理解如何回答。
      猜你喜欢
      • 2012-06-29
      • 2012-12-21
      • 1970-01-01
      • 2014-09-26
      • 2014-09-06
      • 2011-07-31
      • 1970-01-01
      • 2013-03-24
      相关资源
      最近更新 更多