Installing PHP on Gentoo

Now that apache is running on your Gentoo server you might want to add PHP support to it. Here's how.

Adding mod_php

Once you have a basic apache installation in place it might behoove you to install any modules and dependencies you know you'll need as well. For most users this will at least mean installing PHP. In this article we walk you through that process.

Before installing PHP make sure you have a USE flag set that pertains to your web server (like "apache2").

Install the PHP base

Let's start with an install of the PHP metapackage:

sudo emerge php

If you're installing PHP to use it with apache you'll need to make sure apache will load the PHP module. Edit the file:


Look for the "APACHE2_OPTS" line. That line passes keywords to apache telling it what features to enable when it launches, including modules. Make sure that line contains:


For example, the full line might look like:


More PHP libraries

You'll use USE flags to tell programs to work with PHP or to tell PHP to work with other programs. The USE flag for PHP is "php".

If you want to make sure all your programs have USE flags set for apache and for PHP, for example, your USE flag entry in /etc/make.conf could look like:

USE="-gnome -kde -alsa -cups -qt3 -qt4 apache2 php"

Note that you won't need to rebuild apache to add PHP support. That end of things is handled by the PHP package.

You may need other USE flags in there for certain features. If you're doing something fancy be sure to familiarize yourself with the basics of USE flags and where else you can define them (like application-specific flags in the /etc/portage/package.use file) by checking the Gentoo documentation.

One example of a general flag is the "threads" USE flag. If "threads" is enabled when you build PHP you should make sure it was also enabled for apache (so it will use the worker MPM, which we'll discuss later). If you do not have "threads" set for PHP, apache will need to use the "prefork" MPM (which it uses by default). You'll run into problems if one of those is threaded and the other isn't.

If you do make a change to a general USE flag like that you may need to rebuild apache (with another "sudo emerge apache") or PHP (with "sudo emerge php").

Furthermore, USE flags are used to enable additional library support for PHP. Some of those features you may only want to enable for PHP. In that case you could create an entry for PHP in the application-specific portage file:


You can find a complete list of USE flags either online at the official index of USE flags or you can look inside "/usr/portage/profiles/use.desc".

To enable some common add-ons for PHP add this line to /etc/portage/package.use:

dev-lang/php gd jpeg png gif mysql crypt snmp xml xmlrpc

You can look for other packages that extend PHP by looking in the dev-php5 portage tree:


There you'll find some commonly-used packages like "dev-php5/pecl-memcache".

The php.ini file

You may need to make changes to the php.ini file for your application. If so you can find the php.ini used by the apache module here:


The initialization files for additional PHP modules you may have installed should usually show up in their own directories here:


Restart apache

You'll need to restart apache to get it to load the new module:

sudo /usr/sbin/apache2ctl graceful


With that, you have not just an apache web server, but one that runs PHP. Neat! Your work isn't done, however. In the next article we'll discuss apache's configuration files and where to find them, so you can start customizing apache for your slice.

  • -- Jered
Want to comment?

(not made public)


(use plain text or Markdown syntax)