Sunday, October 25, 2009

Setting Apache to read index.php as default page

Hi there,


This is my first post and very much in the wrong order... you see, what I've done is realise it would be useful to write a blog about the whole process.. but I've gone and already started. So what I'm going to do is blog as I go, but fill in the blanks for the initial steps at the same time. I'll go back later and number the steps to make is a lot more simple to follow :)


Right... on to this post


Why do I need to do this?
By Default, the installation of Apache on the Mac Snow Leopard only picks up index.html as the default file in the WebServer Documents directory. This means if you have any index.php files in there, Apache won't read them by default and would just return Error Page 404 - page could not be found etc. etc.


As I'm looking to use the OpenSource Code Igniter PHP Framework, I want index.php to be loaded up by default when I open http://localhost/ in my web browser (Safari in my case).


Where are the files saved?
As a new user to a Mac and Apache installation, I spent a little while trying to find out where all the files where saved. In Windows I was quite experienced with this whole folder structure and new I could find the files in my Apache config folder, generally near the c:\apache\conf\ type area :) However, in a Mac, things are a little more spread around, however, once you're used to this, it gets a lot more sensible and seems more organised.


The file we need to change to set index.php as the DirectoryIndex is httpd.conf. Apache on a Mac supports global and customised config files. The global file is located in:
/etc/apache2/httpd.conf


The Per-User configuration file is in:
/etc/apache2/users/<username>.conf


For the purposes of setting the global DirectoryIndex I want to use the file  /etc/apache2/httpd.conf


Open this in your favourite text editor. I like to use TextMate as it supports lots of syntax highlighting and also has a nice document organiser if you drop a folder on the window... it pops out with a little directory structure...very handy!


Now we've got the file open, scroll down to a section that looks like this:

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

A couple of important things to think about are:
1. You can have several entries, so that Apache will check for any of the files listed and use it. This is what we're going to do, so that either index.php or index.html are loaded.
2. If you have several entries, Apache will search in the order from left to right, so the ordering of the entries on the line is important.

As I say, we're going to use 2 Indexes, to do this, we just add another filename on the same DirectoryIndex line.

To add index.php as the default file, and to take presedence, we simply put it before index.html, so that our new section looks like:


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

Now, save the file and restart apache using the instructions here http://only1ste.blogspot.com/2009/10/restarting-apache-on-mac-snow-leopard.html

So, that's about it. All that is left is to check if it has worked. So just open your browser and browse to http://localhost/ and providing you have created a file index.php in your DocumentRoot folder, this will be opened up. A good one to try this out with is just a couple of lines of php to show the PHP Info details.

<?php
  phpinfo();
?>


No comments:

Post a Comment