Gentoo - Installing PHP
In this article we’ll cover installing PHP and give an example set of USE flags to get started with.
This article assumes that you have completed the Slice Setup articles and the Installing Apache article.
PHP5 Install
First, let’s see what compile options and built in php libraries are available:
emerge php -vp
Remember, you can get more info on the USE flags with:
equery uses php
Update your USE flags to include the options and modules that you require. Personally, I chose the flags listed below, and added them all in one line in ‘/etc/portage/package.use’ like this:
dev-lang/php apache2 cgi ctype curl curlwrappers -doc exif fastbuild filter ftp
hash inifile json mysql mysqli pdo pic posix sharedext sharedmem
simplexml sockets spell truetype xml xmlreader xmlrpc xmlwriter xpm zip
I’ve word wrapped this example, but in the actual file, it’s important that it’s all on one line. If the file doesn’t exist, you can just create it.
In my flags I’ve included ‘mysql’ and ‘mysqli’; those will cause the install to depend on the mysql database server, and install that along with php. If you’re using postgresql, or another database you might want to use different USE flags.
Once you’re happy with the USE flags you’ll need, install it for real:
sudo emerge php
This took about 46 minutes for me. As usual, keep an eye on the output, and follow any advice or warnings it gives.
Don’t worry too much about getting the USE flags correct, you can always modify them and re-install later. Re-installing php on it’s own took 9 minutes for me.
Once done, restart Apache:
sudo /etc/init.d/apache2 restart
Test
Before going any further, it is always a good idea to test your setup and make sure that everything you’ve already done is working as expected.
You can test that Apache and PHP are playing nicely together very easily by creating a simple php file with a call to the phpinfo method and then loading it in your web browser.
First create the file:
sudo nano /var/www/localhost/htdocs/test.php
Now add some basic HTML and a call to the phpinfo method to the file:
<html>
<head>
<title> PHP Test Script </title>
</head>
<body>
<?php
phpinfo( );
?>
</body>
</html>
Great, now you should be able to load that script in your web browser using your Slice IP address:
http://123.45.67.890/test.php
If everything is installed properly, you should see a PHP generated page that displays all sorts of information about your PHP installation:

Great! No need to worry about what all that means for now. We just wanted to verify that PHP was working.
Now that we know it is, go ahead and remove that test script, you don’t need the whole world knowing about your PHP installation.
sudo rm /var/www/localhost/htdocs/test.php
Almost Done
Well, it’s almost ready to use.
In the next article we’ll go through installing external PHP modules.

