PHP

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

USE flags
Portage knows the global  USE flag for enabling support for PHP in other packages. Enabling this USE flag will pull in the package automatically upon updating the deep dependencies in the @world set. First set the global use flag in :

The most important USE flags of the PHP package, the SAPI (Server API) USE flags, are, , and. Alongside these flags, there is an elaborate list of flags to enable various features of the PHP interpreter:

By default the ebuild installs the development version. To install the production version add the following to the :

Both versions of can be found in.

After changing the preferred version of, PHP should be reinstalled using the command in the Emerge section below.

Extensions
To install extensions, first decide which versions of PHP to compile the extensions for. This is done by setting the PHP_TARGETS variable:

More than one version can be defined. Note that the php slot is named "5.6" and the corresponding PHP_TARGETS value is. This is due to current restrictions on USE names.

Emerge
After making the above USE flag configurations it is necessary to update the system so the changes take effect:

Web servers
To use PHP in a server-side fashion, a web server needs to be installed and configured to use PHP. A number of popular web servers are briefly touched upon next.

Nginx
To install Nginx simply emerging it:

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

Now start the related services to have a working site

lighttpd
Make sure that lighttpd is built with support for  and that the   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 the  USE flag has been disabled, it will not be included, and rightly so. However it does provide a good foundation for a configuration file that can be used with FPM.

Edit to look something like this:

Note the  and   parts.

Since Gentoo ships with a perfectly working copy of a file, located in, and init script, starting the services is all that is needed:

Apache (mod_php)
To configure Apache to load the PHP5 module (mod_php), add  to APACHE2_OPTS variable in.

If for some reason the system is missing the PHP module integration file, currently provided by when installed with   USE flag set, manually insert it. Its current content is displayed below.

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 command provided by the  package which should get pulled in automatically by installing PHP. For example, eselecting different versions of PHP can allow a systems to run PHP 7.0 for the cli SAPI but PHP 5.6 for the system's 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  with , ,.

To swap versions use:

Where  in the output above corresponds to the number in the output of.

PHP 7.0 is now running on the 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.6 apache2 SAPI are installed in.

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

Updating to PHP 5.6
Set the PHP_TARGETS variable.

Installation
Install PHP 5.6:

Replacing APC
If you are building with the  USE flag (the default) but need APC user-land functions such as , 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:

For PHP 7 compatibility:

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 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.6. I recommend using for this task. Note that when piping to emerge, you cannot use interactive mode so  on the  command is not applicable here.

Be sure to migrate USE flags if you have specified versions in the 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.

Uninstall old PHP versions
For example, to remove PHP 5.5:

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