index.php 默认不加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2384423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:15:50  来源:igfitidea点击:

index.php not loading by default

phpapachecentosapache-config

提问by 001

I have just installed CentOS, Apache and PHP. When I visit my site http://example.com/myapp/, it says "forbidden". By default it's not loading the index.php file.

我刚刚安装了 CentOS、Apache 和 PHP。当我访问我的网站http://example.com/myapp/ 时,它说“禁止”。默认情况下,它不会加载 index.php 文件。

When I visit http://example.com/myapp/index.php, it works fine.

当我访问http://example.com/myapp/index.php 时,它工作正常。

Any idea how to fix that issue?

知道如何解决这个问题吗?

回答by John Himmelman

Apache needs to be configured to recognize index.php as an index file.

Apache 需要配置为将 index.php 识别为索引文件。

The simplest way to accomplish this..

实现这一目标的最简单方法..

  1. Create a .htaccess file in your web root.

  2. Add the line...

  1. 在您的 Web 根目录中创建一个 .htaccess 文件。

  2. 添加行...

DirectoryIndex index.php

目录索引 index.php

Here is a resource regarding the matter...
http://www.twsc.biz/twsc_hosting_htaccess.php

这是有关此事的资源...
http://www.twsc.biz/twsc_hosting_htaccess.php

Edit: I'm assuming apache is configured to allow .htaccess files. If it isn't, you'll have to modify the setting in apache's configuration file (httpd.conf)

编辑:我假设 apache 配置为允许 .htaccess 文件。如果不是,则必须修改 apache 配置文件 (httpd.conf) 中的设置

回答by M_M

While adding 'DirectoryIndex index.php' to a .htaccess file may work,

虽然将“DirectoryIndex index.php”添加到 .htaccess 文件可能会起作用,

NOTE:

笔记:

In general, you should never use .htaccess files

一般来说,你永远不应该使用 .htaccess 文件

This is quoted from http://httpd.apache.org/docs/1.3/howto/htaccess.html
Although this refers to an older version of apache, I believe the principle still applies.

这是引用自http://httpd.apache.org/docs/1.3/howto/htaccess.html
虽然这是指旧版本的 apache,但我相信这个原则仍然适用。

Adding the following to your httpd.conf(if you have access to it) is considered better form, causes less server overhead and has the exact same effect:

将以下内容添加到您的httpd.conf(如果您可以访问它)被认为是更好的形式,可以减少服务器开销并具有完全相同的效果:

<Directory /myapp>
DirectoryIndex index.php
</Directory>

回答by Ben Rowe

At a guess I'd say the directory index is set to index.html, or some variant, try:

猜测我会说目录索引设置为 index.html 或某些变体,请尝试:

DirectoryIndex index.html index.php

This will still give index.html priority over index.php (handy if you need to throw up a maintenance page)

这仍然会使 index.html 优先于 index.php(如果您需要抛出维护页面很方便)

回答by Maruf

This might be helpful to somebody. here is the snippet from httpd.conf (Apache version 2.2 windows)

这可能对某人有帮助。这是来自 httpd.conf 的片段(Apache 2.2 版 Windows)

# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.html
    DirectoryIndex index.php
</IfModule>

now this will look for index.html file if not found it will look for index.php.

现在这将寻找 index.html 文件,如果没有找到,它将寻找 index.php。

回答by animuson

Try creating a .htaccess file with the following

尝试使用以下内容创建 .htaccess 文件

DirectoryIndex index.php

Edit: Actually, isn't there a 'php-apache' package or something that you're supposed to install with both of them?

编辑:实际上,是不是有一个 'php-apache' 包或者你应该同时安装的东西?

回答by Phil Errington

I had same problem with a site on our direct admin hosted site. I added

我在我们的直接管理托管站点上遇到了同样的问题。我加了

DirectoryIndex index.php

as a custom httd extension (which adds code to a sites httpdfile) and the site then ran the index.phpby default.

作为自定义 httd 扩展(将代码添加到站点httpd文件),然后站点index.php默认运行。

回答by Martlark

Same issue for me. My solution was that mod_dir was not enabled and apache2 was not issuing an error when reading the directive in my VirtualHost file:

对我来说同样的问题。我的解决方案是没有启用 mod_dir 并且 apache2 在读取我的 VirtualHost 文件中的指令时没有发出错误:

DirectoryIndex index.html

Using the commands:

使用命令:

sudo a2enmod dir
sudo sudo service apache2 restart

Fixed the issue.

修复了问题。

回答by Dung

Step by step and Full instruction for Ubuntu 16.04.4 LTS and Apache/2.4.18

Ubuntu 16.04.4 LTS 和 Apache/2.4.18 的分步和完整说明

"sudo -s"

sudo -s

"cd /etc/apache2/mods-enabled"

cd /etc/apache2/mods-enabled

"vi dir.conf" and move index.php to right after DirectoryIndex like below and save file then restart apache server.

vi dir.conf”并将index.php移动到DirectoryIndex之后,如下所示并保存文件然后重新启动apache服务器。

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

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

"service apache2 restart"

service apache2 restart

If you do not see dir.conf then you will need to load it (google for how to)

如果你没有看到 dir.conf 那么你需要加载它(谷歌如何加载)

Done.

完毕。

回答by Excellent Lawrence

This post might be old but i am just posting it incase it helps some other person, I would not advise to Create a .htaccess file in your web root and change the index. I feel it is better to follow the steps

这篇文章可能很旧,但我只是发布它,以防它可以帮助其他人,我不建议在您的 Web 根目录中创建一个 .htaccess 文件并更改索引。我觉得按照步骤来比较好

  1. Go to the conf folder of your apache folder mine is

    C:\Apache24\conf

  2. Open the file named

    httpd.conf

  3. Go to the section

    <IfModule dir_module>
       DirectoryIndex index.html 
    
     </IfModule>
    
  4. Add index.php to it as shown below

     <IfModule dir_module>
      DirectoryIndex index.html index.php
    
    </IfModule>
    
  1. 转到我的 apache 文件夹的 conf 文件夹是

    C:\Apache24\conf

  2. 打开名为的文件

    httpd.conf

  3. 进入栏目

    <IfModule dir_module>
       DirectoryIndex index.html 
    
     </IfModule>
    
  4. 将 index.php 添加到其中,如下所示

     <IfModule dir_module>
      DirectoryIndex index.html index.php
    
    </IfModule>
    

This way, it still picks index.html and index.php as the default index but giving priority to index.html because index.htmlcame before *index.php. By this I mean in you have both index.html and index.php in the same directory, the index.html will be used as the default index except you write **index.php* before index.hml

这样,它仍然选择 index.html 和 index.php 作为默认索引,但优先考虑 index.html,因为index.html在 *index.php 之前。我的意思是,如果您在同一目录中同时拥有 index.html 和 index.php,则 index.html 将用作默认索引,除非您在index.hml之前写入 **index.php*

I hope it helps someone... Happy Coding

我希望它可以帮助某人...快乐编码

回答by MelPogz

This one works like a charm!

这个就像一个魅力!

First

第一的

<IfModule dir_module>
    DirectoryIndex index.html
     DirectoryIndex index.php
</IfModule>

then after that from

然后在那之后从

<Files ".ht*">
    Require all denied
</Files>

to

 <Files ".ht*">
    Require all granted
</Files>