Installing PHPOle Markus With
This document will guide you through installing PHP on Gentoo
1.22012-02-11Installing PHP FPM This is only available for PHP 5.3.3 and newer. The PHP herds consider FPM as the recommended setup for PHP

In PHP 5.3.3, PHP is shipped with support for the FPM SAPI. The FPM SAPI is an
improved fcgi SAPI that allows for more advanced configuration than the
original fcgi SAPI. One of the most useful features is process management, which
makes it very useful for lightweight webservers, such as www-servers/nginx,
that does not handle process management of fcgi themselves.

Installing PHP

Make sure the fpm USE flag is enabled

dev-lang/php fpm

Setting up FPM with lighttpdInterestingly enough, you do NOT want the php USE flag
for www-servers/lighttpd. It could actually break the build.

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

www-servers/lighttpd fastcgi -php

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 /etc/lighttpd/mod_fastcgi.conf so that it looks something like this:

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

/etc/init.d/nginx start/etc/init.d/php-fpm start

Apache and mod_php

Apache is the trusted server part of the LAMP stack. Using mod_php, setting up PHP with Apache is rather trivial. The PHP Herd recommends that if you want to use PHP with Apache, you should use mod_php over any FastCGI/FPM setup. If you want FPM, we recommend that you use nginx as server.

Installing mod_php is as easy as enabling the apache2 USE flag for dev-lang/php

dev-lang/php apache2

This will also pull in www-servers/apache. After the install finish, make sure you add "-D PHP5" to /etc/conf.d/apache2

If you do not complete this step, your php source code will be outputted instead of interpretedNice to knowDevelopment vs Production php.ini files

PHP distributes two versions of their php.ini,
one for production environments and one for development.
In Gentoo, you can now chose which of these two files you want
installed on your system by default. This is particularly handy
when you update PHP and do not want your chosen environment settings
in php.ini to appear when you merge your coniguration files.

To tell portage which version of php.ini you want, add one
of the following lines to /etc/make.conf:

# Set default php.ini version to the development versionPHP_INI_VERSION="development"# Set default php.ini version to the production versionPHP_INI_VERSION="production"

Note that the PHP herd also does additional patching of the php.ini file
regardless of the version you set above.Always check and double-check your php.ini file after upgrade

If you want to change your php.ini after installation, you
can find both versions located in /usr/share/doc/php-<version>/php.ini-development.bz2

FPMm user/group

However, you might want to change the user and group parameters to something else
than nobody. Personally, I changed it to nginx, but you might want to consider
creating a new user for your web application.

TroubleshootingMy source code is showing

We do not really get that many support issues except for this one.

The most common reason is that short_open_tags is disabled by default. If you code make use of short tags, i.e <? instead of <?php, you have to either fix your code or make change your php.ini to enable short open tags.

short_open_tag = On

Make sure you restart the relvant servers/fpm after changing php.ini

The other common reason for source code to be displayed is that you forgot to add "-D PHP5" to /etc/conf.d/apache2 as described above.

Further reading

PHP Upgrade guide - Offers more information about how to deal with multiple instals of PHP and how to install extensions.

Concurrently running multiple versions of PHP - A guide describing how to set up webservers to work with parallel installs of PHP