Installing PHP on Ubuntu


ow that apache is running on your Ubuntu 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 aptitude install php5
Aptitude will want to nab some files to go with it, like “php5-common” and mod_php for apache, which is good. But if you started with a base apache install (which you have if you’re following these tutorials), it will also want to replace a package we installed with apache:
The following NEW packages will be installed:
apache2-mpm-prefork{a} libapache2-mod-php5{a} php5 php5-common{a}
The following packages will be REMOVED:
apache2-mpm-worker{a}
It’s okay that aptitude wants to do that, and you should let it. But let’s explain what that bit means, so you can come back to it later if you need to.

What’s an MPM?

The short explanation is that the MPM package that’s installed determines how the apache web server process handles a lot of work at once. “MPM” stands for “Multi-Processing Method”. The “prefork” method will spawn additional apache2 processes, so when your site gets busy you’ll see a bunch of apache2 processes running. The “worker” method will create a slew of threads inside just a few apache2 processes, so you won’t see the number of apache2 processes change much over time.
We’ll talk more about the MPM in a later article, so for now let aptitude do its thing. The PHP install swapped MPMs because it’s more compatible with the prefork MPM.

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 aptitude repository and install them.
To see what PHP modules are available try a:
aptitude search php5-
Note the “-” at the end of “php5″. 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 aptitude’s “show” argument to get details:
aptitude show php5-mysql
The output will include a longer description of the package, what other packages it requires, 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 aptitude install” with a list of modules as the arguments. As an example, here’s a command to install several commonly-used PHP libraries (handy if you aren’t sure what you’ll need):
sudo aptitude install php5-mysql php5-dev php5-curl php5-gd php5-imagick php5-mcrypt php5-memcache php5-mhash php5-pspell php5-snmp php5-sqlite php5-xmlrpc php5-xsl

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/php5/apache2/php.ini
The initialization files for additional PHP modules you may have installed should usually show up in this directory:
/etc/php5/conf.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/apache2ctl 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

Related Posts

Subscribe Our Newsletter