This guide explains how to configure Apache under FreeBSD in order to run PHP under the fastcgi
module using php-fpm
instead of mod_php
.
php-fpm
can be built by installing PHP from ports with the portglob lang/php
. To change directory to the PHP port, issue:
cd /usr/ports/lang/php
and then configure all dependencies, issue:
make config-recursive
At this point you should select the option for PHP to build PHP FPM and then proceed with the rest of the dependencies. Finally to install PHP, issue:
make install clean
Next, a file is created at /etc/apache2/Includes/php-fpm.conf
with the following contents:
<IfModule mod_fastcgi.c> AddType application/x-httpd-fastphp .php Action application/x-httpd-fastphp /php-fcgi Alias /php-fcgi /usr/local/www/apache24/cgi-bin/php-fcgi FastCgiExternalServer /usr/local/www/apache24/cgi-bin/php-fcgi -appConnTimeout 600 -idle-timeout 600 -socket /var/run/php-fpm.sock -pass-header Authorization <Directory /usr/local/www/apache24/cgi-bin> Require all granted </Directory> </IfModule>
this will configure PHP FPM to work through sockets and PHP FPM must now be configured to create the socket at /var/run/php-fpm.sock
. In order to do so, edit /usr/local/etc/php-fpm.conf
and amend or add the lines:
; user and group (set to Apache user and group) user = www group = www ; socket listen user and group (set to Apache user and group) listen.owner = www listen.group = www listen.mode = 0666 ; create the socket listen = /var/run/php-fpm.sock
Edit /etc/rc.conf
and add or amend the lines:
# Depends on your Apache version. apache24_enable="YES" php_fpm_enable="YES"
PHP-FPM has to be started by issuing:
/usr/local/etc/rc.d/php-fpm restart
and after that Apache2 can be started by issuing:
/usr/local/etc/rc.d/apache24 restart
Create a PHP file containing:
<?php phpinfo();
and open it in the browser. You should see on the first screen along the top:
Server API FPM/FastCGI
which indicates that it is working.
In case you see:
Server API Apache Handler 2.0
then it is not working - most likely you have forgotten to disable the standard PHP module (with a2dismod php5).