I'm relatively new to the world of unix command line web server management/administration.

Many hosts with control panel administration options allow you to change how PHP is run with a simple option.

The most common options being:

  • apache module
  • CGI application
  • FastCGI application

My question is simply, how do you change this via the command line? I know there are many configuration files for apache.

The closest thing I have found is this question, however the directory structure does not seem to match for my OS (Ubuntu 12.04).

I'm quite bewildered how there does not seem to be a clear guide that I can find that details this process for something that seems to be so common. Forgive me if this exists... if so, please point me in the right direction.

migrated from stackoverflow.com Nov 18 '13 at 7:05

This question came from our site for professional and enthusiast programmers.

  • The closest thing I have found is this question on SO That website is not Stack Overflow. – ta.speot.is Nov 17 '13 at 2:40
  • 1
    This on fastcgi explains it pretty well. – Matt Green Nov 17 '13 at 2:44
  • 1
    @ta.speot.is forgivaness, pleaassuh.. Thanks Matt, of course I looked at fastcgi.com, but I found those instructions a little difficult to follow. – Evan Mattson Nov 17 '13 at 3:17
  • @ta.speot.is, Simple typo. He meant "SE" not "SO". – Pacerier Jun 25 '15 at 5:14

Thanks for previous answers they got me most of the way, but to get things working I had to combine instructions from a few places, so thought I would write up a complete set of commands.

FYI I'm running Ubuntu 14.04, Apache 2.4, and also had modphp running by default, previous instructions also left out the need to disable modphp.

I also found http://blog.starcklin.com/2013/08/install-mod-fastcgi-and-php5-fpm-on-ubuntu/ to be very informative and straightforward.

Just run the following commands in a terminal one after the other.

First install the necessary packages (I leave out php5 as this assumes it's already installed, add it back in for a first time install). Also note from Apache 2.4 up you can use the event-mpm instead of worker see http://www.vps.net/blog/2013/04/08/apache-mpms-prefork-worker-and-event/. My example shows worker, but just replace the word worker with event if you'd rather use that.

sudo apt-get install apache2-mpm-worker

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

Now enable mods you need, and disable those you don't.

sudo a2dismod php5 mpm_prefork

sudo a2enmod actions fastcgi alias mpm_worker

Create the php5.fcgi file and give the webserver permission to use it.

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

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

Create a global config for php5-fpm

sudo nano /etc/apache2/conf-available/php5-fpm.conf

paste in the following (we'll use a socket instead of IP address)

<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 -idle-timeout 3600 
   <Directory /usr/lib/cgi-bin>
       Require all granted
   </Directory> 
</IfModule>

Enable the php5-fpm conf

sudo a2enconf php5-fpm

Restart apache and fpm

sudo service apache2 restart && sudo service php5-fpm restart

As per other instructions paste the following into a new browseable php file on your webserver.

<?php phpinfo();

Open the file you just edited in a web browser, If you see "FPM/FastCGI" next to Server API, you are now serving PHP with FastCGI!

  • 4
    This is a lifesaver. Instructions for the older apache did not work on Ubuntu 14.04, and apache 2.4; but this one worked like a charm. +100 !! – Bhavin Doshi Sep 27 '14 at 9:40
  • @bkd.online Couldn't have said it better. @ JDawgg Nice one :) – Gadgetroid Nov 5 '14 at 0:44
  • I followed this exactly, yet I get the error 'You don't have permission to access /php5.fcgi/index' ... And in the error log, 'client denied by server configuration: /usr/lib/cgi-bin/php5.fcgi' ... Any ideas? – Just Lucky Really Jun 10 '15 at 20:14
  • Figured it out ... I was using the old 'order deny,allow' configuration for root for some reason ... All fixed now :D – Just Lucky Really Jun 10 '15 at 20:56
  • I got following error after installation of packages and configuration "apache2: syntax error on line 216 of /etc/apache2/apache2.conf: Syntax error on line 9 of /etc/apache2/conf-enabled/php5-fpm.conf: Expected </Directory> but saw </IfModule>" – Farooq Apr 23 '16 at 4:53
up vote 19 down vote accepted

I finally found a nice tutorial geared at doing just this. I will outline the steps I took as I already had my LAMP stack installed but the full tutorial can be found here.

Note for the new:

In the tutorial, it begins by switching to the root user with:

sudo su

In my case I simply prefixed those commands sudo instead of switching users, so I will be documenting my steps that way.

Begin

Step one: Install the Apache Worker MPM (Multi-Procesing Modules)

sudo apt-get install apache2-mpm-worker

This replaces the prefork I had installed which is the default when installing Apache.

Step 2: Install PHP5 and necessary modules

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

At this point you may get an error installing 'libapache2-mod-fastcgi':

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package libapache2-mod-fastcgi is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or is only available from another source

E: Package 'libapache2-mod-fastcgi' has no installation candidate

This part is not in the above tutorial

To reconcile this, the multiverse repository must be added to the apt sources.

To do this:

sudo nano /etc/apt/sources.list

To which I appended the following lines:

deb http://archive.ubuntu.com/ubuntu precise multiverse
deb http://archive.ubuntu.com/ubuntu precise-updates multiverse
deb http://security.ubuntu.com/ubuntu precise-security multiverse

precise in this case refers to my version of Ubuntu "Precise Pangolin".

So now, save those changes and back to terminal:

sudo apt-get update

and again:

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

which will (should) now work.

Now enable these Apache modules:

sudo a2enmod actions fastcgi alias

restart apache

sudo service apache2 restart

Step 3: Apache configuration

To make Apache work with PHP-FPM, we need the following configuration:

<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 -host 127.0.0.1:9000 -pass-header Authorization
    <Directory /usr/lib/cgi-bin>
        Require all granted 
    </Directory>
    #directory statement mult be on multiple lines
</IfModule>

Note:

If you're using an older version of Apache (2.3.x or older), leave out the line

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

You can check your installed version with the command

apache2 -version

You can put it in the global Apache configuration (so it's enabled for all vhosts), for example in /etc/apache2/conf.d/php5-fpm.conf (this file does not exist, so you must create it), or you can place it in each vhost that should use PHP-FPM.

I choose to go the global route, so:

sudo nano /etc/apache2/conf.d/php5-fpm.conf

paste in the code block above, and save, exit. This new file will be automatically loaded by Apache's default configuration which loads all files in the /etc/apache2/conf.d/ directory.

restart Apache:

sudo service apache2 restart

Now create the following PHP file in the document root /var/www:

sudo nano /var/www/info.php

Add:

<?php phpinfo();

save & exit.

Now we call that file in a browser (e.g. http://your-server-ip/info.php)

Under Server API at the top you should see FPM/FastCGI.

Success!

For more information like how to change PHP-FPM to use a unix socket instead of the default TCP port or how to configure this for individual virtual hosts instead of all of them, see the source tutorial linked at the top.

  • I had to add this before </IfModule>: <Directory /usr/lib/cgi-bin> Require all granted </Directory> – Rimu Atkinson May 12 '14 at 4:29
  • @RimuAtkinson is correct, by default all directories will be forbidden in newer versions, so you'll have to specifically grant access to the cgi-bin directory. This should be added to the answer, as without it you'll get a 403. – johnrom Jul 15 '14 at 19:27
  • Thanks for this howto! Note that the Require all granted thing is only required since Apache 2.4. If I put it in the configuration file with Apache 2.2, it fails. – leemes Aug 7 '14 at 18:11
  • I've added a note. (Feel free to fine-edit it; it's your post. ;)) For details, see stackoverflow.com/a/22040167/592323 – leemes Aug 7 '14 at 18:30
  • 1
    This answer does not work on ubuntu 14.04. Refer answer by JDawgg to do it in 14.04. – Bhavin Doshi Sep 27 '14 at 9:41

In Ubuntu 14.04 after doing the steps of the accepted answer, edit /etc/apache2/conf-available/php5-fpm.conf

<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 -idle-timeout 3600
    <Directory /usr/lib/cgi-bin>
        Require all granted
    </Directory>
</IfModule>

Then execute

#sudo a2enconf php5-fpm
#sudo apache2 restart

In case of somebody is doing this on Ubuntu 16.04 and has downgraded from php 7 to php 5.6 (Ondrej), you have to replace some lines (based on JDawgg's answer):

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

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

sudo a2dismod php5 mpm_prefork to:

sudo a2dismod php5.6 mpm_prefork

sudo nano /etc/apache2/conf-available/php5-fpm.conf to:

sudo vim /etc/apache2/conf-available/php5.6-fpm.conf

Installation process of php5.6-fpm already created the file, so there is no need to create one. However, I removed all existing text on that file as it was meant for mod_php5.

<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/php/php5.6-fpm.sock -pass-header Authorization -idle-timeout 3600 
   <Directory /usr/lib/cgi-bin>
       Require all granted
   </Directory> 
</IfModule>

I changed path from /var/run/php5-fpm.sock to /var/run/php/php5.6-fpm.sock as the file was located there.

sudo a2enconf php5-fpm to:

sudo a2enconf php5.6-fpm

sudo service apache2 restart && sudo service php5-fpm restart to:

sudo service apache2 restart && sudo service php5.6-fpm restart

I hope this helps other newbies like me :)

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.