126

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?

BiscuitBaker
  • 1,421
  • 3
  • 23
  • 37
001
  • 62,807
  • 94
  • 230
  • 350

9 Answers9

165

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)

John Himmelman
  • 21,504
  • 22
  • 65
  • 80
  • 6
    It should probably be in the php.conf file that apache loads. – staticsan Mar 05 '10 at 03:51
  • 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. – John Himmelman Mar 05 '10 at 04:25
  • 2
    dont forget to restart apache!! as i did! :/ – Navid Jul 02 '15 at 08:54
  • 2
    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. – cazort Aug 02 '21 at 21:47
  • 1
    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 ...) – Synexis Aug 27 '21 at 16:11
122

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.

M_M
  • 1,955
  • 2
  • 17
  • 23
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)

Ben Rowe
  • 28,406
  • 6
  • 55
  • 75
  • 1
    Mine looks like this but is unfortunately downloading the index.php instead of executing it. – Ben May 03 '13 at 13:46
  • @Webnet then you should consider changing Type and LoadModules to php so it read php [https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page] – Merey Nurlan Dec 09 '19 at 01:02
19

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.

Maruf
  • 199
  • 1
  • 2
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

Excellent Lawrence
  • 946
  • 11
  • 11
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?

animuson
  • 53,861
  • 28
  • 137
  • 147
2

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!

John Rix
  • 6,271
  • 5
  • 40
  • 46
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>
MelPogz
  • 67
  • 2
  • 9
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

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055