How To Enable php On Apache With OS X Mavericks

phpIf you’re an OS X user developing php based application, you’d have realised that the apache configuration shipped with OS X doesn’t have php enabled out of the box. If you try to open a php script using your browser, you’ll just see the php code as text and not the rendered HTML you’re expecting.

But, don’t fret. It’s actually pretty simple to enable php on OS X. Here is what you should do.

Use your favorite editor to open the apache configuration file – /etc/apache2/httpd.conf. Since this file is owned by the root user, you’ll have to drop down to the terminal and use sudo to open it with root privileges.

Apache httpd.conf

sudo will prompt you for your password and then open the editor command, vim in my case. If you’re not familiar with vim, just take a look at this cheat sheet for a few basic commands or use a simpler editor such as nano – sudo nano /etc/apache/httpd.conf.

Now, scroll down a bit and find the line that looks something like this

#LoadModule php5_module libexec/apache2/libphp5.so

See the ‘#’ in front of the line. This means that the line has been commented out and apache doesn’t execute this line when it’s reading the configuration file.

Now, all you have to do is uncomment the line, i.e. remove the ‘#’ in front of it and save the file (Tip: Place your cursor on the ‘#’ and use the ‘x’ command to delete it. Now press the escape key and type ‘:wq’ to save the file and quit the editor).

At this point, you should’ve changed the apache configuration file but the apache instance already running doesn’t know that and it’s still running with the old configuration. To change that, you’ll have to restart apache.

sudo apachectl restart

Restart Apache

That’s it! Now when you browse over to your php script, you should be able to see it in all it’s HTML rendered glory.