PHP

PHP is a general-purpose server-side scripting language to produce dynamic Web pages.

dev-lang/php
Portage knows the global USE flag php for enabling support for PHP in other packages. Enabling this USE flag will pull in automatically:

The most important USE flags of php, the SAPI (Server API) USE flags, are:

Also check the other USE flags.

By default the ebuild installs the development version. If you want to install the production version add the following to your :

Both versions of can be found in.

After changing the preferred version of, you need to reinstall PHP:

Extensions
If you want to install extensions, you can decide which versions of PHP you want extensions compiled for. This is done by setting the PHP_TARGETS variable:

You can also set more than one. Note that the php slot is named "5.4" and the corresponding PHP_TARGETS setting is php5-4. This is due to current restrictions on USE names.

After setting this you want to update your system so the changes take effect:

Nginx
To install Nginx is as easy as emerging it:

Once Nginx has been installed, modify the server section of to look something like this:

Now just start the related services and you should have a working site

lighttpd
Make sure that lighttpd is built with support for fastcgi and that the php USE flag is disabled:

Lighttpd ships with a default FastCGI config file, but unfortunately, it is written to work with the old PHP FCGI SAPI only, instead of a general FCGI setup. Since we disabled the php USE flag, it won’t be included, and rightly so. However it does provide a good foundation for a configuration file that can be used with FPM.

Edit so that it looks something like this:

Note the  and   parts.

Since Gentoo ships with a perfectly working copy of a file, located in, and init-script, all you need to do now is to start the services:

Apache (mod_php)
To configure Apache to load your PHP5 module (mod_php), you have to add  to   variable in.

If for some reason you lack file PHP module integration file /etc/apache2/modules.d/70_mod_php5.conf currently provided by app-eselect/eselect-php[apache2], manually insert

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 cli 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 cli SAPI use:

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

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.

Configuration
The PHP configuration is in, which contains one subdirectory for each SAPI for each PHP version, e.g. configuration files for the PHP-5.4 apache2 SAPI are installed in.

To setup Apache to use PHP, see the Apache article.

Replacing PHP 5.4 with PHP 5.5
Set the PHP_TARGETS variable.

Installation
Install PHP 5.5:

Replacing APC
If you are building with the opcache USE flag (the default) but need APC user-land functions such as apc_fetch, install APCu, which emulates APC but only has user caching functions. This may be necessary for older projects that rely on APC for caching.

If necessary, unmask it:

Install APCu:

Switch to PHP 5.6
Switch to it using for all SAPIs:

If you have other SAPIs, be sure to run for those as well.

Re-install compatible extensions
Since changing PHP_TARGETS is technically a USE flag change, you will be able to simply emerge @world with flags checking for new USE flags and all extensions should re-build.

Uninstall incompatible extensions
Remove all old extensions that do not build for PHP 5.5. I recommend using eix for this task. Note that when piping to emerge, you cannot use interactive mode so --ask on the emerge command is not applicable here.

Be sure to migrate USE flags if you have specified versions in your package.use file(s).

Since you removed extensions, it may be useful to run a dependency clean up at this point for older ones that are no longer compatible.

Migrate the configuration files
The different slots have different configuration paths. Please take care to migrate the configuration files before making use of a new slot.

Restart PHP-FPM
Restart PHP-FPM if you are using it.