Installing PHP
1.
Installing PHP FPM
Important: 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
Code Listing 1.1: /etc/portage/package.use |
dev-lang/php fpm
|
Setting up FPM with lighttpd
Warning: Interestingly 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
Code Listing 1.2: /etc/portage/package.use |
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:
Code Listing 1.3: /etc/lighttpd/mod_fastcgi.conf |
server.modules += ("mod_fastcgi")
fastcgi.server = ( ".php" =>
( "localhost" =>
(
"host" => "127.0.0.1",
"port" => "9000"
)
)
)
|
Note the host and port parts.
Since Gentoo ships with a perfectly working copy of a php-fpm.ini file,
located in /etc/php/fpm-php5/php-fpm.ini, and init-script,
all you need to do now is to start the services:
Code Listing 1.4: Starting PHP |
/etc/init.d/php-fpm start
/etc/init.d/lighttpd start
|
Nginx
To install Nginx is as easy as emerging it:
Code Listing 1.5: Installing nginx |
emerge -av nginx
|
Warning:
If you have configured a custom NGINX_MODULES_HTTP, make sure that the fastcgi module is enabled.
|
Once Nginx has been installed, modify the server section of /etc/nginx/nginx.conf to look something
like this:
Code Listing 1.6: /etc/nginx/nginx.conf |
<snip>
server {
listen 127.0.0.1;
server_name localhost;
access_log /var/log/nginx/localhost.access_log main;
error_log /var/log/nginx/localhost.error_log info;
root /var/www/localhost/htdocs;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
}
<snip>
|
Now just start the related services and you should have a working site
Code Listing 1.7: Starting nginx and php-fpm |
/etc/init.d/nginx start
/etc/init.d/php-fpm start
|
2.
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
Code Listing 2.1: /etc/portage/package.use |
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
Code Listing 2.2: /etc/conf.d/apache2 |
APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D LANGUAGE -D SSL -D SSL_DEFAULT_VHOST -D USERDIR -D PHP5"
|
Warning: If you do not complete this step, your php source code will be outputted instead of interpreted |
3.
Nice to know
Development 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:
Code Listing 3.1: Choosing between development and production php.ini |
PHP_INI_VERSION="development"
PHP_INI_VERSION="production"
|
Important: Note that the PHP herd also does additional patching of the php.ini file
regardless of the version you set above. |
Warning: 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.
4.
Troubleshooting
My 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.
Code Listing 4.1: php.ini |
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.
5.
Further reading
The contents of this document, unless otherwise expressly stated, are licensed under the CC-BY-SA-2.5 license. The Gentoo Name and Logo Usage Guidelines apply.
|