Apache 2.4 with php5-fpm

April 24, 2014 86.3k views
I've spent so many hours trying to make the following setup: Apache v2.4 php-fpm to work, but without any success. I am using fully updated Ubuntu Server 14.04 TLS. Most of the tutorials online are for nginx with php-fpm or Apache 2.2, but I know things have changed.
16 Answers
Assuming that you're starting with a basic LAMP stack, first you need to install the needed packages:


sudo apt-get install libapache2-mod-fastcgi php5-fpm


Then create the file "/etc/apache2/conf-available/php5-fpm.conf" with the contents:


<IfModule mod_fastcgi.c>
AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
</IfModule>


Then enable the new mods and config:


a2enmod actions fastcgi alias
a2enconf php5-fpm
service apache2 reload


You should be up and running. To verify, create a file called "/var/www/html/info.php" with the contents:


<?php
phpinfo();
?>


Now you should be able to browse to http://your.ipaddress.com/info.php

You should see a lot of information about your php set up including: "Server API FPM/FastCGI "
  • I have installed Apache2.4 with PHP5-FPM and libapache2-mod-fastcgi and configure it as per this above configuration.
    PHP5-Fpm is running but when i checked using phpinfo it's not showing FPM.
    Current Status - >
    Apache 2.0 Handler
    PHP5-FPM is running
    tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 16489/php-fpm.conf)

    Apache error logs
    FastCGI: process manager initialized (pid 16454)
    [mpmprefork:notice] AH00163: Apache/2.4.7 (Ubuntu) modfastcgi/mod_fastcgi-SNAP-0910052141 PHP/5.5.9-1ubuntu4.3 configured -- resuming normal operations

    Please help me to configure it properly or let me know what i am missing.

  • Thanks a lot it worked...can you elaborate your answer in brief (this did that)...thanks again

I've managed to launch it. Actually I did the same by myself the last few times, but I always received the following error:
`client denied by server configuration: /usr/lib/cgi-bin/php5-fcgi`

The solution is to add this code to **/etc/apache2/conf-available/php5-fpm.conf**:
`
Require all granted

`

Credits: http://stackoverflow.com/a/23000981/1156999

PS: I accidentally added **Ruby on Rails** tag to the question. It will be nice if a moderator can remove it, please.
Glad you got it working! I fixed up the tags.


For anyone else, here's Ivan's addition to php5-fpm.conf with the formatting fixed:


<Directory /usr/lib/cgi-bin>
Require all granted
</Directory>


You may also need to switch from the apache MPM non-threaded module mpm_prefork to a threaded one.

a2dismod mpm_prefork

then

a2enmod mpm_worker
or
a2enmod mpm_event

If it's not installed:

apt-get install apache2-mpm-worker
or
apt-get install apache2-mpm-event

Hi, thanks for the info, does any one know why I get this info back from php_info()
Server API	Apache 2.0 Handler

PHP API	20121113

PHP Extension	20121212

Zend Extension	220121212
  • I think you need to ensure that mod_php is disabled first: $ sudo a2dismod php5 Module php5 disabled. To activate the new configuration, you need to run: service apache2 restart

Thanks for this.
Worked like a charm for my very first attempt.

In my case it worked when i moved the php5-fpm.conf to
/etc/apache2/conf-enabled/php5-fpm.conf*:

Thank you very much. Worked like charm. Andrew and Richard, thank you both

another post about this - link to a unique site with awesome engineers

Thanks for the tutorial. I got PHP-FPM working but there are lots of issues, I had to point the default site to /usr/lib/cgi-bin/php5-fcgi which makes sense. And after that php-files in the directory load with FPM. But no other files, html, js, jpegs doesn't work and gives me an access denied?

I added the extensions to security.limit_extensions in php-fpm www.conf which makes html work. But not images. Shouldn't apache handle those static files without php-fpm somehow? Anybody know how to get this working?

Just got to this thread, managed to get FPM work for my self but for some reason i was getting 500 error when I was testing the page, upon checking the apache error logs I found this

Permission denied: FastCGI: failed to connect to server "/usr/lib/cgi-bin/php5-fcgi": connect() failed
FastCGI: incomplete headers (0 bytes) received from server "/usr/lib/cgi-bin/php5-fcgi"

so I thought socket must have permission issues, and thats why its not connecting, my solution was

chmod 666 /var/run/php5-fpm.sock

I defined this socket in /etc/php5/fpm/pool.d/www.conf (apache 2.2)

Hope this can help someone else

and to verify if FastCGI is working for you your <?php phpinfo(); ?> page should look like
phpinfo screenshot

@richard-mckenna was right! Must first disable mod_php before use these settings... damn, it took me hours until find this comment to save me from lost more time.

  • Can you help me? I've installed all of things.. How can now return back and start disabling mod_php?
    Thanks

I haven't php-fpm on my info.php file, i've configured all the things explaned..
thanks

php-fpm is apache module, there is nothing to do with info.php.

To comeback to normal just uninstall mpmwork and/or mpmevent and install mpmprefork which modphp was made of to work.

Take a learn on how apache mpm workers, it took me a while to figure this out.

Hi,

I have installed php5-fpm with apache24 sucessfully. I can load phps through apache fast-cgi but when I execute a Joomla o Wordpress, for example, a I have this random error and the web is not loaded (sometimes it loads incomplete and somtimes it shows a 500 error)

Could you help me, please?

The errors are

Aug 11 09:00:14 ubuman000 kernel: [83428.495213] traps: php5-fpm[18502] general protection ip:6c6491 sp:7fffffffbe90 error:0 in php5-fpm[400000+800000]
Aug 11 09:00:17 ubuman000 kernel: [83431.615852] traps: php5-fpm[18503] general protection ip:6c6491 sp:7fffffffbe90 error:0 in php5-fpm[400000+800000]
Aug 11 09:00:18 ubuman000 kernel: [83432.470517] traps: php5-fpm[18504] general protection ip:6c6491 sp:7fffffffbe90 error:0 in php5-fpm[400000+800000]
Aug 11 09:00:19 ubuman000 kernel: [83433.324105] traps: php5-fpm[18505] general protection ip:6c6491 sp:7fffffffbe90 error:0 in php5-fpm[400000+800000]
Aug 11 09:00:20 ubuman000 kernel: [83434.146597] traps: php5-fpm[18506] general protection ip:6c6491 sp:7fffffffbe90 error:0 in php5-fpm[400000+800000]
Aug 11 09:00:51 ubuman000 kernel: [83465.346084] traps: php5-fpm[18476] general protection ip:6c6491 sp:7fffffffbe90 error:0 in php5-fpm[400000+800000]
Aug 11 09:04:21 ubuman000 kernel: [83675.351981] php5-fpm[18508]: segfault at 2020 ip 00000000006fb4b9 sp 00007fffffffbe80 error 4 in php5-fpm[400000+800000]
Aug 11 09:04:51 ubuman000 kernel: [83705.370273] php5-fpm[18509]: segfault at 2020 ip 00000000006fb4b9 sp 00007fffffffbe80 error 4 in php5-fpm[400000+800000]
Aug 11 09:05:01 ubuman000 CRON[18519]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Aug 11 09:05:21 ubuman000 kernel: [83735.343396] php5-fpm[18510]: segfault at 2020 ip 00000000006fb4b9 sp 00007fffffffbe80 error 4 in php5-fpm[400000+800000]
Aug 11 09:06:21 ubuman000 kernel: [83795.343665] php5-fpm[18511]: segfault at 2020 ip 00000000006fb4b9 sp 00007fffffffbe80 error 4 in php5-fpm[400000+800000]
Aug 11 09:07:22 ubuman000 kernel: [83855.888170] php5-fpm[18514]: segfault at 2020 ip 00000000006fb4b9 sp 00007fffffffbe80 error 4 in php5-fpm[400000+800000]

I have Ubuntu 14.04 64 bits with php5-fpm 5.5.9-1ubuntu4.11

The fast-cgi configuration

<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  #FastCgiWrapper /usr/lib/apache2/suexec
  FastCgiIpcDir /var/lib/apache2/fastcgi

  # Bloque de configuración de FastCGI para PHP-FPM
  Alias /php5.fastcgi /usr/local/fastcgi/php5.fastcgi
  AddHandler php-script .php .phtml

FastCGIExternalServer /usr/local/fastcgi/php5.fastcgi -idle-timeout 300 -socket /var/run/php-fpm.sock -pass-header Authorization

  Action php-script /php5.fastcgi virtual

  # Para usar otro servidor como php server usamos host en vez de socket.
  #FastCGIExternalServer /usr/local/fastcgi/php5.fastcgi -idle-timeout 300 -host 127.0.0.1:9000 -pass-header Authorization

  # This part is not necessary to get it to work, but it stops anything else from being
  # accessed from it by mistake or maliciously.
  <Directory "/usr/local/fastcgi">
    Require all granted
    # DEPRECATED
    #Order allow,deny
    <Files "php5.fastcgi">
      # DEPRECATED
      #Order deny,allow
      Require all granted
    </Files>
  </Directory>
</IfModule>

I dont know what is happening

Have another answer? Share your knowledge.