index.php not loading by default
Asked Answered
E

9

127

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.

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

Any idea how to fix that issue?

Enlarge answered 5/3, 2010 at 3:41 Comment(0)
A
166

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

The simplest way to accomplish this..

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

  2. Add the line...

DirectoryIndex index.php

Here is a resource regarding the matter...
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)

Arin answered 5/3, 2010 at 3:48 Comment(5)
It should probably be in the php.conf file that apache loads.Seymourseys
I think you mean php.ini. Regardless, his apache isn't recognizing index.php as a directory index file. Whether its handling php files is another apache config issue.Arin
dont forget to restart apache!! as i did! :/Defiant
It is best practice never to use .htaccess files unless necessary (i.e. if you are a user in a shared hosting environment and .htaccess is the only way to set your custom configuration options, then this is the only solution, but to do it when you have administrative access results in both a security risk and a significant performance hit. If you have access, that same directive can go in httpd.conf. There are few downsides to doing this either; if a project doesn't use php, the index.php file wouldn't exist there anyway.Nest
To add to @staticsan's comment, ensure too that you have php enabled: in httpd.conf uncomment the appropriate LoadModule line (e.g., LoadModule php7_module ...)Isometry
A
123

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

NOTE:

In general, you should never use .htaccess files

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.

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:

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

Edit: At the time of edit, the v1.3 documentation is down. The v2.4 documentation (current version at time of edit) has a similar stance:

In general, use of .htaccess files should be avoided when possible. Any configuration that you would consider putting in a .htaccess file, can just as effectively be made in a <Directory> section in your main server configuration file.

Avail answered 2/11, 2011 at 9:0 Comment(2)
the link is deadTimehonored
@Rhdr: Thanks for pointing that out. Post has been updated with a new link to v2.4 documentation, so should no longer be dead.Avail
B
51

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

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)

Bates answered 5/3, 2010 at 3:54 Comment(2)
Mine looks like this but is unfortunately downloading the index.php instead of executing it.Falange
@Webnet then you should consider changing Type and LoadModules to php so it read php [#5121995Ridden
F
20

This might be helpful to somebody. here is the snippet from httpd.conf (Apache version 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.

Fructify answered 2/11, 2014 at 14:5 Comment(0)
A
4

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

  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>
    

This way, it still picks index.html and index.php as the default index but giving priority to index.html because index.html came 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

I hope it helps someone... Happy Coding

Absher answered 16/10, 2018 at 16:30 Comment(1)
This is the only thing that worked for me out of all these answers. Thank you so much!Saran
C
3

Try creating a .htaccess file with the following

DirectoryIndex index.php

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

Copula answered 5/3, 2010 at 3:49 Comment(0)
K
3

I had a similar symptom. In my case though, my idiocy was in unintentionally also having an empty index.html file in the web root folder. Apache was serving this rather than index.php when I didn't explicitly request index.php, since DirectoryIndex was configured as follows in mods-available/dir.conf:

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

That is, 'index.html' appears ahead of 'index.php' in the priority list. Removing the index.html file from the web root naturally resolved the problem. D'oh!

Karoline answered 5/1, 2018 at 0:19 Comment(0)
F
2

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>
Finnish answered 24/10, 2018 at 14:39 Comment(0)
H
1

After reading all this and trying to fix it, I got a simple solution on ubuntu forum (https://help.ubuntu.com/community/ApacheMySQLPHP). The problem lies with libapache2-mod-php5 module. Thats why the browser downloads the index.php file rather than showing the web page. Do the following. If sudo a2enmod php5 returns module does not exist then the problem is with libapache2-mod-php5. Purge remove the module with command sudo apt-get --purge remove libapache2-mod-php5 Then install it again sudo apt-get install libapache2-mod-php5

Helenhelena answered 15/4, 2014 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.