Installing PHP 5.3 On Ubuntu
Out Of Date Warning
Languages change. Perspectives are different. Ideas move on. This article was published on July 5, 2009 which is more than two years ago. It may be out of date. You should verify that technical information in this article is still current before relying upon it for your own purposes.
With the release of PHP 5.3 to the world, I wanted to be one of the first to try it. The problem is that the typical package managers for Ubuntu won’t include PHP 5.3 for some time – perhaps as long as a year. This is a problem, since I really want to try PHP’s latest and greatest features for myself.
The problem is, there seems to be a lack of clear, coherent instructions online about compiling PHP on Ubuntu from source. Either it’s so insanely simple that anyone who does it figures everyone else knows how, or everyone relies on the pre-built binaries released by the world that any time I search for “Ubuntu PHP source” I get “why don’t you just use the built-in package manager?” And so, I wanted to write a set of instructions for how I configured and compiled PHP, on Ubuntu Jaunty.
For the most part we’ll use standard out-of-the-box packages that Ubuntu provides. Since the features I’m looking for are in PHP 5.3, I’m fine with having slightly outdated packages for other software as long as they’re compatible with PHP 5.3.
1. Install Apache.
The first step is to install Apache. You can feel free to use your own web server, but for this example I opted for Apache based on the simplicity of installing PHP as an Apache module. This command should also install the development tools, which PHP will need to properly configure itself for Apache.
2. Install MySQL and PostgreSQL
Most people use MySQL, but almost all PHP installations seem to have PostgreSQL installed as well. I think it’s always good to keep your options open, and you might take a look at Postgres at some point. If you opt not to install Postgres, that’s fine; just alter your configure statement to exclude it.
aptitude install mysql-client mysql-client-5.0 mysql-common mysql-server mysql-server-5.0 mysql-server-core-5.0
3. Install required libraries
PHP itself is very easy to configure and install. However, if you want to get the most out of it you’ll need to install the dependency libraries it uses for various functions like mcrypt and gd. This can be the most challenging part of installing PHP; many hours can be spent issuing a configure command only to have it fail, have to install another library, and repeat the process.
These libraries are the libraries that were needed to install the options in the configure statement below. You may need more or fewer libraries depending on your individual needs and desires.
4. Download the PHP Source Code
Visit http://www.php.net and download the latest version of PHP 5.3. The following commands may be helpful:
tar xvfz php-5.3.0.tar.gz
Change your working directory to the unpacked PHP source code. Execute the following command.
Edit: Many commenters suggested using the –prefix=PREFIX directive to place your PHP files and limit the potential problems should you decide to remove or replace PHP. This is a good idea.
The screen should scroll quickly as it does a check of the files and libraries it will need for compiling, and, if everything is installed correctly, it will build the makefiles for the next step. If it fails, it will give you an error message (usually related to a missing library) and tell you to try again.
6. Make & Make Install
In order to have PHP installed, we have to first compile it from the source code. This involves the compiler accessing the libraries it needs. Once compiled, we’ll be able to install it. The first command in this process is easy enough:
There will once again be a lot of information on your screen. Take your time, go for a walk. This takes a bit of time, and the more you compile into PHP the longer the process takes. PHP is an advanced, complicated language, so be patient.
Once it completes successfully, you should see a warning to run “make test”. You may run this, though you don’t have to at this stage.
The final step in the PHP compilation process is invoking the installer.
Why do we use -i on the installer? Ubuntu uses an unusual configuration for Apache which causes the installer not to know how to install PHP properly. This will produce a fatal error and cause the install to stop. The -i flag tells it to ignore the errors; we’ll set up Apache to use PHP 5.3 next.
7. Add the PHP Module to Apache
In order to have PHP run as a part of the web server, we’ll need to create a module for it. This is typically done automatically, but due to the unusual setup of the Ubuntu Apache installation, we’ll have to do it ourselves.
First, change directories:
Create a new file called php5.load and paste the following line into it:
Save that file. Now create another file called php5.conf and paste the following into it:
AddType application/x-httpd-php-source .phps
Now we’re all set! We just need to enable the module and restart Apache:
Enabling module php5.
Run ‘/etc/init.d/apache2 restart’ to activate new configuration!
# /etc/init.d/apache2 restart
* Restarting web server apache2
… waiting …done.
8. Test the PHP Installation
Now we just need to see that PHP is in fact installed!
First let’s check the PHP binary:
PHP 5.3.0 (cli) (built: Jul 1 2009 18:53:06)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
If you get output similar to the above, your PHP installation is correct. Note that the point release may be different since these directions were written; however, you should see PHP 5.3.x as the version.
Next, let’s get the info from PHP to see what’s installed. Find your webroot, create (or edit) an index.php file and add the following:
<?php phpinfo(); ?>
Type the URL of your web server into the browser and you should see something that looks like this:
If you do, congratulations! You’ve successfully installed PHP 5.3 on Ubuntu from source.
Write better object oriented PHP today.
Object oriented programming always leaves you with a headache. What if you could master it instead?Get the book now! »