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 libphp5.so or libphp4.so 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*" /usr/libexec/php/libphp5.so
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 libphp5.so in the modules directory of your apache installation directory (/usr/local/apache2)
sudo cp /usr/libexec/apache2/libphp5.so /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/libphp5.so 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 <?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
Comments: