Installing PHP on Ubuntu

Now 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:

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:


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


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


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:

Joju commented Sat Jul 24 07:17:52 UTC 2010:

where could I see php executable?

Jered commented Mon Jul 26 17:31:08 UTC 2010:

PHP doesn't have an executable as such, not when installing this way anyhow. Apache loads PHP as a module using the library, "/usr/lib/apache2/modules/".

Larry Applegate commented Tue Sep 21 17:06:45 UTC 2010:

The /etc/php5/apache2/php.ini command gives "Permission denied".

Jered commented Tue Sep 21 17:17:31 UTC 2010:

It's not actually a command - the php.ini file is a configuration file, which means it contains settings you can modify to change PHP's behavior. To change those settings you need to open the php.ini file in a text editor. For example: "sudo nano /etc/php5/apache2/php.ini".

Morgan commented Wed Oct 06 19:41:06 UTC 2010:

I guess I need to add the "php5-cli" package for a command line executable? I'm trying to set up so I can pipe mail from postfix into a php script...

David Yeiser commented Fri Oct 08 04:02:30 UTC 2010:

Thanks for the article. If I install PHP5 and it removes the apache2-mpm-worker package will that adversely affect my Django/mod_wsgi/PostgreSQL stack? I only ask because I remember the apache2-mpm being involved in the Django setup. Or something like it. Maybe I'm wrong (very likely!).


Jered commented Fri Oct 08 15:52:55 UTC 2010:

Morgan: It does sound like that's the package you want, yes. If you're trying to trigger something based on incoming mail, then something like procmail or fetchmail would likely help too.

David: As I understand it, prefork is considered safer for most simple mod_wsgi applications, but it can take a performance hit with multithreaded applications. You might look into the issue a bit more if you're using FastCGI with Django or have a threaded application, otherwise there should be no issues with prefork.

Bradley commented Sat Nov 06 16:18:42 UTC 2010:

I'm having trouble with a long polling script not returning until the script is finished processing. For example - works properly - does not

It's the exact same script, any idea what I need to change on my server to make it work?

Thor commented Mon Nov 22 21:28:45 UTC 2010:

Hmm. I have gone through "basic apache installation" and "Installing PHP on Ubuntu", but php scripts are being served as textfiles (not parsed by the php_engine). Should mod_php be installed with apache by now ? The the file /etc/php5/apache2/php.ini nor the directory /etc/php5/apache2 exist by now (/etc/php5 does).

Please help :-)

Thor commented Tue Nov 23 13:12:55 UTC 2010:

I think I have the fix for my previous comment :

Install mod_php5 sudo aptitude install libapache2-mod-php5

Activate mod_php5 sudo a2enmod php5

Restart Apache sudo /etc/init.d/apache2 force-reload

I was inspired by this article :

AND: I had to clear my Firefox history to get it working

newly domain commented Fri Jan 21 03:51:51 UTC 2011:

what is the difference between aptitude and apt-get ?

Jered commented Fri Jan 21 03:58:28 UTC 2011:

Aptitude is kind of the newer version of apt-get. The main difference is that aptitude handles package conflicts and dependencies more gracefully, but takes up a bit more disk space than apt-get alone. Either one works but make sure that you use only one of them consistently. Aptitude and apt-get don't keep everything in the same locations, so the one usually doesn't know everything that the other has installed or removed.

James commented Wed Mar 16 01:43:03 UTC 2011:

Can I install php on my linux based VPS (Ubuntu Distro) without installing Apache?

Jered commented Sun Mar 20 02:06:04 UTC 2011:

You can install php by itself. You can also use it with other web servers, like nginx. You'd usually use an intermediary program like fastcgi to get nginx talking to PHP. Unfortunately I don't think we have any articles on that subject right now (it's on my list, I swear). You should be able to find some decent help on that subject in our forums or with a couple web searches.

ブルガリ ネックレス commented Wed Jul 31 17:35:24 UTC 2013:

How could i change that surroundings in Thunderbird to make sure that I {choose the|discover the|the right gifts

ブルガリ 時計 commented Thu Aug 01 01:47:06 UTC 2013:

I JUST by now have always been jogging not one but two information sites, the first for layout & progression and I've got any pr for 4 onto it and I've got a huge amount superior articles and reviews.. In addition to I've got a further web site wherever I've got rants, well-being tips and picture taking... Must i merge them all or possibly what's hold it all seperate?.

Want to comment?

(not made public)


(use plain text or Markdown syntax)