Enabling php in Apache on Linux (CentOS 6) by Loading the Module in httpd.conf

Though installing different versions of php using yum should enable php automatically on Apache 2.2, in case there is a problem and it is not enabled this tutorial shows how to enable it.

Make Sure php Is Installed

We are assuming php has already been installed. If yum was used you can check this way:

[ahmed@amayem ~]$ yum list installed php

Should give us the following:

Installed Packages
php.x86_64       5.3.3-27.el6_5.1        @updates

If not installed using yum then we should check the existence of a php.ini file:

[ahmed@amayem ~]$ ls /etc | grep php
php.d
php.ini

Your php files do not have to be necessarily in this directory, but it is where the default yum repository places them.

Checking the php Apache Module

The most important thing we need to check is that there is a php module:

[ahmed@amayem ~]$ ls /etc/httpd/modules/ | grep php
libphp5.so

Looks like it is there.

Modifying the httpd.conf File

We need to add the following line somewhere in the httpd.conf file:

LoadModule php5_module modules/libphp5.so

Open the httpd.conf file for editing:

[ahmed@amayem ~]$ sudo vi /etc/httpd/conf/httpd.conf 

Notice that we have to use sudo to edit the file. Check here to see how to give a linux user sudo powers.

Find the place in the document where all the LoadModule commands are by entering /LoadModule and pressing enter. Go wherever you want in the list (or anywhere in the document, in fact) and enter insert mode by pressing i. Make a new line (the line must not start with a #, which would indicate a comment) and paste the following:

LoadModule php5_module modules/libphp5.so

For more on copying and pasting in vi check here. Exit insert mode by pressing Esc or Ctrl+c then save and exit by entering :x. Now that the config file has been modified we need to restart Apache

Restarting Apache

[ahmed@amayem ~]$ sudo apachectl restart

Testing php

Now that php has been installed and enabled let’s test it by make a php info page that outputs some information about the installed php.

References

  1. php.net Apache Install Manual

Ahmed Amayem has written 90 articles

A Web Application Developer Entrepreneur.

  • dskanth

    Thanks, it was very useful. It helped me.

  • abdessamad abba

    thank you, it was helpful

  • Phattius

    Upon starting Apache 2.4.28:

    Syntax error on line 6 of /opt/apache2/conf/extra/php.conf: Cannot load modules/libphp5.so into server: /opt/apache2/modules/libphp5.so: undefined symbol: unixd_config

    • http://ahmed.amayem.com ahmedamayem

      I’m not sure what the question is. Did you modify any other file other than httpd.conf?

      • Phattius

        Further research reveals that “unixd_config” is for Apache 2.2. For 2.4 they decided to rename it to “ap_unixd_config” for mysterious reasons.

        Bottom line, avoid compiling from scratch if at all possible. There are WAY too many dependencies to bother with. Eventually got it working with only a dozen or so downloaded RPM’s from various sources.

        • http://ahmed.amayem.com ahmedamayem

          Thanks for sharing your results.