PHP/en

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

USE flags
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 emerged 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 system administrators or application developers to quickly test an application on different versions of PHP.

To list the available versions for the cli SAPI module use the following syntax:

The  (asterisk) marks current active version for the selected module. To check the other SAPIs simply replace  with ,  or   modules.

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 Server API (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.

When running OpenRC as the service manager:

For systemd:

lighttpd
Make sure that lighttpd has 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 with mod_php. Support for multiple PHP versions on Apache is available using fpm. The command is used to switch between active mod PHP versions.

To change the version of PHP handled by Apache, first list the available versions for the  Server Application Programming Interface (SAPI):

Change it to the version of choice:

Substitute  in the example above to the requested number in the output of  as displayed earlier on.

To verify the PHP module works, create a test page:

Now, suppress or rename and open the test page:

A table describing the PHP settings should be visible.

Apache (fcgid)
See enabling PHP through fcgid in the Apache article.

Apache (mod_proxy_fcgi)
See enabling PHP-FPM through mod_proxy_fcgi in Apache 2.4.

External resources

 * Running Multiple Versions of PHP Using Apache on Gentoo