Installing PHP on Ubuntu Desktop

0019-fi-phplogo

PHP is one of the most popular Web programming languages and is a component of the popular open-source LAMP architecture (Linux, Apache, MySQL, PHP). In this article I will demonstrate how to install PHP on Ubuntu Desktop and provide an overview of configuring the interpreter. In a future article, I’ll show how to install MySQL on the platform to complete the build-out of a LAMP development environment on Ubuntu Desktop.

This article assumes that you have already installed Apache on Ubuntu as described in the article Installing Apache Web Server on Ubuntu Desktop. I also assume that your Ubuntu VM is running and that you are logged in as the administrative user.

Installing PHP

The process for installing PHP and integrating it with Apache is simple. In fact, a single command is all it takes. In a terminal window enter the following command: sudo apt-get install php5. Enter your password when prompted and press Enter. When you see the prompt Do you want to continue [Y/n]?, type y and press Enter. You should see output similar to that shown below.

Install PHP5 on Ubuntu

The apt-get command will download and install PHP and associated Apache components. It also changes the Apache process architecture from worker to prefork.

The apt-get utility tells you what packages will be installed and removed during the installation process. Notice that the apache2-mpm-worker package will be removed and the apache2-mpm-prefork package will be installed. This swaps out the multi-process, multi-threaded MPM architecture for the multi-process, single-threaded architecture. This is because PHP5 programs are not guaranteed to be thread-safe.

Notice also that apt-get is suggesting the php-pear package also be installed. Since we don’t need PEAR at the moment, I’ll skip this package for now. However, you can install it later if you need to.

Once the installation completes, PHP will be installed and integrated with Apache (gee, that was easy). You can test the configuration by creating a small test program in the DocumentRoot, /var/www. In the terminal window enter the command sudo nano /var/www/phpinfo.php. Enter the code shown below into the editor and save your file.

<?php
   phpinfo();
?>

PHP code can be embedded in a web page (HTML file) by surrounding it with <?php and ?> tags. The code within the tags is sent to the PHP interpreter for execution. This test program just has a single call to the phpinfo() function. This function produces a lot of information about the Apache and PHP environments.

Launch the Firefox browser from the Launcher and enter the URL http://localhost/phpinfo.php into the address bar. You should see a page similar to the one below.

phpinfo() page

The phpinfo() function confirms that PHP works with Apache and provides a lot of configuration and diagnostic information about your server.

The fact that the page displayed means that the PHP interpreter is working and it is integrated into Apache (via the mod_php5 module). If you scroll through the phpinfo.php page you see a ton of useful information.

You can run the same program from the command-line by entering the command sudo php /var/www/phpinfo.php. In this case, phpinfo() produces text output instead of HTML.

Configuring PHP

There are several PHP configuration files that you need to be aware of in order to configure PHP. The first of these is the php.ini file. This file sets many parameters that configure the PHP interpreter. You can run PHP scripts in two ways: from the command-line and from the web server. For the command-line environment, the php.ini file is located in the /etc/php5/cli directory. For the web server configuration, look in /etc/php5/apache2.

After installation the php.ini file is configured for use in a production environment. However, there is a version that’s more suitable for development use available to you. Mostly, the development version enables error messages. This alternate version is installed in /usr/share/php5/php-ini-development. If you want to use the development configuration instead of the production configuration, backup your current php.ini file, create a symbolic link to the development version of the configuration file, and restart Apache as shown below.

Change to development PHP settings

You can re-configure PHP with development settings instead of production settings to get PHP error messages displayed in your browser.

The PHP installation also enabled the mod_php5 Apache module that integrates Apache and PHP. This module causes Apache to execute a PHP script and return the output to the browser. If you examine the /etc/apache2/mods-enabled directory, you will notice that there is now a file called php5.load. As shown below, this file contains a directive to load the shared object library that implements the module.

php5.load configuration fle

The php5.load configuration file contains this directive to load the mod_php5 shared object library.

Also in the /etc/apache2/mods-enabled directory you will find the configuration file php5.conf. This file contains configuration directives for the mod_php5 module as shown below. The first section uses the <FilesMatch> directive and the power of regular expressions to match PHP files with the extensions .php, .php3, .php4, .php5, .pht, and .phtml. For any files matching these extensions the directive SetHandler application/x-httpd-php will be in effect. This handler will cause the PHP interpreter to parse the file and execute it.

The next two <FilesMatch> directives deny access to PHP source files with the extension .phps and to any PHP files without a name before the extension. The final <IfModule> directive disables PHP from executing any files in user directories. If you want to change this behavior you can make the necessary changes to the php5.conf file and restart Apache.

php5.conf configuration file

The mod_php5 Apache module is configured with directives in the php5.conf file.

In this article I showed you how to install PHP on Ubuntu desktop. The installer automatically integrated PHP with Apache by installing mod_php5, along with the necessary directives to recognize PHP files and execute them to produce HTML output. We also looked at a few of the configuration files that you may need to edit to change the way the PHP module functions.

In future articles, I’ll show how to install MySQL in order to build out a complete LAMP development environment on Ubuntu Desktop. We’ll also explore how to create dynamic Web applications that use PHP and MySQL together.

Speak Your Mind

*