PHP

PHP is an interpreted language used in generating dynamic web pages. It is supported by many major web servers, including Apache, and is available through Gentoo portage in the package.

Installation
The installation guide of PHP on Gentoo written by the PHP herd covers how to install PHP on your Gentoo system.

Before you begin, check your USE flags. Depending on what you will be doing, you can add some of the following USE flags to for :


 * -- Apache Support.
 * -- MySQL database support.
 * -- Improved support for MySQL 4.1 and above.
 * -- PHP hardening extension
 * -- Secure Sockets Layer (for encrypted connections).
 * -- GD image rendering library (often use by PHP applications to generate dynamic graphics, e.g. thumbnails).
 * -- JPEG image support.

Noteworthy is the flag, which installs a command line tool, which will allow you to run code, and with the -l switch, check syntax (useful during development):

Now to see if the system recognizes php do:

and then open your browser and go to localhost. If you see php informations here then the system works. If not, you have to go in and add " -D PHP5 " to the APACHE2_OPTS parameters to have a line like this:

Now save the file, reload apache2 server and it should work.

Running multiple versions of PHP
One of the great advantages of using Gentoo for PHP development is the version slotting. It is very simple to swap between multiple versions of PHP as well as run multiple versions simultaneously. This is all done with the eselect command provided by the package which should get pulled in automatically with your installation. This allows you to run, for example, PHP 5.3 for your SAPI but PHP 5.4 on your web server. It also allows you to quickly test your application on different versions of PHP.

To list the available versions for the SAPI use: [1]  php5.3 [2]  php5.4 * [3]  php5.5

The version marked with the * is the current active version, and to check the other SAPIs simply replace with ,,.

To swap versions use:

Where "1" corresponds to the number in the output of "show".

You are now running PHP 5.3 on command line. PHP 5.3.9-pl0-gentoo (cli) (built: Jan 30 2012 13:21:46) Now, lets say you want to run two php-fpm servers, with two different versions, for two different websites. There is a nifty feature in the php-fpm init script which will do this for you. Symlink the global php-fpm script to all the versions you would like to run simultaneously. So if I wanted to run both PHP 5.3 and PHP 5.4 I would do this.

Now, I will need to stop any running instances on the global PHP as we don't want the global and slotted version fighting for the same port.

Also, because php-fpm binds itself to a port we need our two versions to use two separate ports so let's update the 5.4 config.

I can now use my new slotted init scripts to launch the different versions.

From here you just need to adjust the port setting in your web server configuration (apache, nginx, lighttpd etc.) to use the port of the version you are interested in for each of your server's virtual hosts.