Posts /

Enable & Configure Php on Apache Server

Twitter Facebook Google+
10 Feb 2015

Recently I have been writing Server Side Php Script and desiging UI in HTML5 and CSS, so to test and debug my code locally I needed to setup Apache Server and enable Php on it. Fortunately, apache server comes pre-installed in OSX. There are two ways to configure apache, either install clients like MAMP which will do the job for you or do it by yourself i.e Manually.

Configuring Server


In this post, I am going show how to configure Apache Server Manually. Launch Terminal on your mac and open httpd.conf file via following command. It is recommended to make a copy of httpd.conf for backup, incase you mess things up.

nano /etc/apache2/httpd.conf

In that file search for the line DocumentRoot. In nano editor to perform search use ctrl+w. By default it should be :

DocumentRoot "/Library/Webserver/Documents"

Change the directory to wherever you want your root folder be i.e wherever you want your Sites to host, or keep it default. Now search for <Directory "/Library/Webserver/Documents"> in httpd.conf. Change the Directory to wherever your new root directory is (which you defined in previous step or keep it default if you didn’t modify the Root Directory). Now add the following lines before the closing </Directory> tag :

Options Indexes FollowSymLinks Multiviews
AllowOverride None
Order allow, deny
Allow from all

Enabling Php on Apache Webserver


Now, since your apache server is configured you need to enable Php. In order to do so search for the following line in httpd.conf

LoadModule php5_module libexec/apache2/libphp5.so

and uncomment it by removing # in front of this line. This should enable Php execution on your apache server. Finally, save httpd.conf file using ctrl+o and then ctrl+x. Now in Terminal type :

sudo apachectl restart

Launch any browser, and navigate to “http://localhost” to very the server is running. If everything went well you will see “It Works!” message.

If unfortunately somehow your local Apache Webserver is not working then you can check for errors using :

sudo apachectl configtest

You can check Error Logs, by tailing the log file located in /var/log/apache2/error_log :

tail /var/log/apache2/error_log

Happy Coding :)


Twitter Facebook Google+