How to configure PHP and Apache in Linux and Mac?

You have installed PHP and Apache (Check the installation procedure). But you are facing with the following problems

  • You are not able to configure PHP and apache so that apache can execute the .php files
  • Instead of executing the php files, the browser simply displays the php files like a text
  • Instead of executing the .php files, the browser shows the download option

If you are facing with any of the above problems, it is because the apache needs to load the PHP module. To load the PHP module, you must know the current version of PHP you are running

$ php -v
PHP 5.2.11 (cli) (built: Sep 28 2009 16:48:57)

As you can see, I am using PHP version 5. You may be using version 4 or 5. Next step is to find the or depending on the version you are running. I will continue the remaining post taking into account version 5. You can simply substitute 4 instead of 5 if you are running version 4

$ find / -iname "*libphp*"


You may get different path depending on your machine and installation

Now the next step is to stop apache

The apache I have installed is in /usr/local/apache2. So I run the following command

/usr/local/apache2/bin/apachectl stop

Copy the in the modules directory of your apache installation directory (/usr/local/apache2)

sudo cp /usr/libexec/apache2/ /usr/local/apache2/modules

Now open httpd.conf. It is present in the conf directory. Add the following lines in /usr/local/apache2/conf/httpd.conf

LoadModule php5_module modules/
DirectoryIndex index.html index.php
AddType application/x-httpd-php php
AddType application/x-httpd-php-source phps

There you are, write a small php file in htdocs directory

$ vim /usr/local/apache2/htdocs/test.php


echo "PHP is working fine"


And enter the following path in your browser http://localhost/test.php

If everything is fine, you can see the following output

PHP is working fine

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s