Question

Apache 2.4 with php5-fpm

  • Posted April 24, 2014

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.

Subscribe
Share

Quick tip - you can avoid having to create a file in /usr/lib/cgi-bin/php7.fcgi by adding ‘virtual’ to the end of Action in the block above:

Action php7-fcgi /php7-fcgi virtual

For those trying to get PHP FPM to work with PHP 7.0, here’s what I had to do. I’m running a 512MB droplet with Apache 2.4.x and Ubuntu 14.04 LTS. Disabling mod_php and handing this work off to PHP FPM has made a HUGE improvement in my memory utilization and this is very important to me given I only have 512MB to work with! Prior to making the changes below, I was running >70% RAM usage (and often times >85%) and now I’m <50%.

I opted to edit my global configuration file “/etc/apache2/conf-available/php7.0-fpm.conf” as I wanted PHP FPM enabled for all of my virtual hosts:

       <IfModule mod_fastcgi.c>
                AddHandler php7-fcgi .php
                Action php7-fcgi /php7-fcgi
                Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
                FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-heade$
                <Directory /usr/lib/cgi-bin>
                        Require all granted
                </Directory>
        </IfModule>

The <Directory …> bit was important was prior to this I was getting the following error:

You don’t have permission to access /php7-fcgi/phpinfo.php on this server.

I also had to create my php7.fcgi file:

sudo touch /usr/lib/cgi-bin/php7.fcgi

Then permission it so my web server can access it:

sudo chown -R www-data:www /usr/lib/cgi-bin

Couple of helpful resources I used: https://askubuntu.com/questions/378734/how-to-configure-apache-to-run-php-as-fastcgi-on-ubuntu-12-04-via-terminal and https://www.howtoforge.com/tutorial/apache-with-php-fpm-on-ubuntu-16-04/.

Good luck!

This comment has been deleted


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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

Glad you got it working! I fixed up the tags. <br> <br> <br>For anyone else, here’s Ivan’s addition to php5-fpm.conf with the formatting fixed: <br> <br><pre> <br><Directory /usr/lib/cgi-bin> <br> Require all granted <br></Directory> <br></pre> <br> <br>

I’ve managed to launch it. Actually I did the same by myself the last few times, but I always received the following error: <br>client denied by server configuration: /usr/lib/cgi-bin/php5-fcgi <br> <br>The solution is to add this code to /etc/apache2/conf-available/php5-fpm.conf: <br> <Directory /usr/lib/cgi-bin> <br> Require all granted <br> </Directory> <br> <br> <br>Credits: http://stackoverflow.com/a/23000981/1156999 <br> <br>PS: I accidentally added Ruby on Rails tag to the question. It will be nice if a moderator can remove it, please.

Assuming that you’re starting with a basic LAMP stack, first you need to install the needed packages: <br> <br><pre> <br>sudo apt-get install libapache2-mod-fastcgi php5-fpm <br></pre> <br> <br>Then create the file “/etc/apache2/conf-available/php5-fpm.conf” with the contents: <br> <br><pre> <br><IfModule mod_fastcgi.c> <br> AddHandler php5-fcgi .php <br> Action php5-fcgi /php5-fcgi <br> Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi <br> FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization <br></IfModule> <br></pre> <br> <br>Then enable the new mods and config: <br> <br><pre> <br>a2enmod actions fastcgi alias <br>a2enconf php5-fpm <br>service apache2 reload <br></pre> <br> <br>You should be up and running. To verify, create a file called “/var/www/html/info.php” with the contents: <br> <br><pre> <br><?php <br>phpinfo(); <br>?> <br></pre> <br> <br>Now you should be able to browse to http://your.ipaddress.com/info.php <br> <br>You should see a lot of information about your php set up including: "Server API FPM/FastCGI "

Assuming that you’re starting with a basic LAMP stack, first you need to install the needed packages: <br> <br><pre> <br>sudo apt-get install libapache2-mod-fastcgi php5-fpm <br></pre> <br> <br>Then create the file “/etc/apache2/conf-available/php5-fpm.conf” with the contents: <br> <br><pre> <br><IfModule mod_fastcgi.c> <br> AddHandler php5-fcgi .php <br> Action php5-fcgi /php5-fcgi <br> Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi <br> FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization <br></IfModule> <br></pre> <br> <br>Then enable the new mods and config: <br> <br><pre> <br>a2enmod actions fastcgi alias <br>a2enconf php5-fpm <br>service apache2 reload <br></pre> <br> <br>You should be up and running. To verify, create a file called “/var/www/html/info.php” with the contents: <br> <br><pre> <br><?php <br>phpinfo(); <br>?> <br></pre> <br> <br>Now you should be able to browse to http://your.ipaddress.com/info.php <br> <br>You should see a lot of information about your php set up including: "Server API FPM/FastCGI "

Assuming that you’re starting with a basic LAMP stack, first you need to install the needed packages: <br> <br><pre> <br>sudo apt-get install libapache2-mod-fastcgi php5-fpm <br></pre> <br> <br>Then create the file “/etc/apache2/conf-available/php5-fpm.conf” with the contents: <br> <br><pre> <br><IfModule mod_fastcgi.c> <br> AddHandler php5-fcgi .php <br> Action php5-fcgi /php5-fcgi <br> Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi <br> FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization <br></IfModule> <br></pre> <br> <br>Then enable the new mods and config: <br> <br><pre> <br>a2enmod actions fastcgi alias <br>a2enconf php5-fpm <br>service apache2 reload <br></pre> <br> <br>You should be up and running. To verify, create a file called “/var/www/html/info.php” with the contents: <br> <br><pre> <br><?php <br>phpinfo(); <br>?> <br></pre> <br> <br>Now you should be able to browse to http://your.ipaddress.com/info.php <br> <br>You should see a lot of information about your php set up including: "Server API FPM/FastCGI "

Wanted to give props to you and commenters as this is the missing piece in connecting php7.1-fpm to apache2.4.

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

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

To comeback to normal just uninstall mpm_work and/or mpm_event and install mpm_prefork which mod_php was made of to work.

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

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