Installing PHP on CentOS

Now that apache is running on your CentOS server you might want to add PHP support to it. Here's how.


Adding mod_php

Once you have a basic apache installation in place, it might behoove you to install any modules and dependencies you know you'll need as well. For most users this will at least mean installing PHP. In this article we walk you through that process.

Install the PHP base

Let's start with an install of the basic PHP package:

sudo yum install php

Yum will want to nab some files to go with it, like "php-common", which is good.

More PHP libraries

Your web application may require more than the basic PHP installation to function. In that case you'll want to find any required PHP libraries in the yum repositories and install them.

To see what PHP modules are available try a:

yum search php-

Note the "-" at the end of "php". This will show any packages that start with "php5-", along with a brief description.

If you want to get more information about one of the available packages, use yum's "info" argument to get details:

yum info php-mysql

The output will include a longer description of the package, the version of the library in the package, and more.

Once you've decided what extra PHP modules you need to install (if any), run "sudo yum install" with a list of modules as the arguments. As an example, here's a command to install several commonly-used PHP libraries at once (handy if you aren't sure what you'll need):

sudo yum install php-mysql php-devel php-gd php-mcrypt php-pecl-memcache php-pspell php-snmp php-xmlrpc php-xml

The php.ini file

You may need to make changes to your php.ini file for your application. If so, you can find the php.ini used by the apache module here:

/etc/php.ini

The initialization files for additional PHP modules you may have installed should usually show up in this directory:

/etc/php.d/

Restart apache

With PHP and its extras installed, you'll need to restart apache to get it to load the new module:

sudo /usr/sbin/apachectl graceful

Summary

With that, you have not just an apache web server, but one that runs PHP. Neat! Your work isn't done, however. In the next article we'll discuss apache's configuration files and where to find them, so you can start customizing apache for your slice.

  • -- Jered

Article Comments:

Sam commented Tue Aug 17 07:52:05 UTC 2010:

Running an updated CentOS 5.5 returns 'No package php-pecl-imagick available.' on your yum command of installing the extra PHP modules.

I searched and saw 'php-pecl-Fileinfo.i386 : Fileinfo is a PHP extension that wraps the libmagic library'.

Jered commented Tue Aug 17 16:25:09 UTC 2010:

Thanks for pointing that out, Sam. I went ahead and removed it from the example for now. The Imagemagick extension allowed for graphics manipulation in PHP. The Fileinfo extension isn't quite the same thing, but is still useful (it can be used to get a lot of information about files, like the length of a video, or get its MIME type for web serving).

Samuel commented Wed Apr 13 15:06:51 UTC 2011:

To install PHP 5.3, use yum install php53. To install the MySQL extension for PHP 5.3, use yum install php53-mysql.

CC commented Fri Jun 17 12:01:41 UTC 2011:

You have such good articles on install nginx. Could you please write an article about installing php for nginx. It is quite more complex than the simple apache install.

Keith commented Tue Jun 12 00:15:31 UTC 2012:

Hello, Jered. I was just browsing this article, and notice something. You have a refference to aptitude in this article, that I think whent unnoticed. I suggest you consider fixing that to say Yum instead. Not that I personally mind it, but some future newbie might think: "What?" Thanks!

Jered commented Thu Jun 28 13:20:51 UTC 2012:

Thanks Keith. The hazards of trying to keep articles similar for easier maintenance. Fixed the reference.

Luz commented Fri May 03 20:31:17 UTC 2013:

It's in reality a nice and useful piece of information. I'm satisfied that you shared this useful info with us. Please stay us up to date like this.

Thank you for sharing.

My blog; Luz

Luz commented Fri May 03 20:32:00 UTC 2013:

It's in reality a nice and useful piece of information. I'm satisfied that you shared this useful info with us. Please stay us up to date like this.

Thank you for sharing.

My blog; Luz

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)