index.php not loading by default

PhpApacheCentosApache Config

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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)

Solution 2 - Php

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<br /> 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.

Solution 3 - Php

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)

Solution 4 - Php

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.

Solution 5 - Php

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

Solution 6 - Php

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?

Solution 7 - Php

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

Solution 8 - Php

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!

Solution 9 - Php

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question001View Question on Stackoverflow
Solution 1 - PhpJohn HimmelmanView Answer on Stackoverflow
Solution 2 - PhpM_MView Answer on Stackoverflow
Solution 3 - PhpBen RoweView Answer on Stackoverflow
Solution 4 - PhpMarufView Answer on Stackoverflow
Solution 5 - PhpExcellent LawrenceView Answer on Stackoverflow
Solution 6 - PhpanimusonView Answer on Stackoverflow
Solution 7 - PhpRupak RokadeView Answer on Stackoverflow
Solution 8 - PhpJohn RixView Answer on Stackoverflow
Solution 9 - PhpMelPogzView Answer on Stackoverflow