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:

The ebuild installs a modified "production" version of that has tighter security settings and default paths adjusted for Gentoo systems. Both upstream "production" and "development" files are installed to.

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; just add in the additional versions separated by a space. 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:

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, selecting different versions of PHP can allow a system to run PHP 7.0 for the cli SAPI but PHP 5.6 for the system's web server. It also allows to quickly test an 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 ,  or.

To swap versions, use:

The number  in this example corresponds to the number in the output of the   sub-command used above. PHP version 7.0 is now used for the command-line:

Configuration
The PHP configuration is at, which contains one subdirectory for each SAPI and for each PHP version. For instance, configuration files for the PHP-5.6 apache2 SAPI are installed in.

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
Simply emerge nginx to install 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. The init script starts the PHP FastCGI Process Manager. FastCGI allows web servers to offload the PHP calculations to this process manager.

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 working 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. Users might remember that previously, the PHP version had to be added as well (like ). However, since version 0.8.1, the variable is changed to just   to allow future major versions to be easily integrated.

Make sure that PHP is built with the  USE flag.

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.

Although multiple PHP versions can be installed on a system, Apache can only use a single PHP version. The command is used to switch between active PHP versions.