The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

php-fpm and fastcgi and apc

Discussion in 'Workarounds and Optimization' started by Atrix, Sep 7, 2012.

  1. Atrix

    Atrix Member

    Joined:
    Sep 7, 2012
    Messages:
    9
    Likes Received:
    2
    Trophy Points:
    3
    cPanel Access Level:
    Root Administrator
    I followed some instructions posted here to try and get php-fpm working with easyapache. However the mod_fastcgi module isn't available in the easyapache configuration when I check the exhaustive list.

    http://forums.cpanel.net/f145/case-...api-new-php-5-3-3-a-161210-p3.html#post936492

    All of his instructions don't work for me, but i did get php-fpm's flag set and the init.d script running. My problem is using php fastcgi as a handler in apache2.2, the configuration option in easyapache just isn't there at all which is weird because it says enabling some modules disables FastCGI, and even though those modules aren't enabled FastCGI doesn't show up at all, only Mod FCGID shows up and that isn't what I need it's a different, slower, and from what I read less secure module and doesn't work nearly as well with the php-fpm and apc combo.

    APC is opcode caching and can give php up to 90% speed increases and is the recommeneded opcode system.
    php-fpm allows using fastcgi instead of dso to use opcode caching. The reason I need fastcgi is because the worker MPM does not work with DSO, only the prefork MPM does. The worker MPM is a much improved MPM for busy and high traffic sites that uses better memory management among other things. Prefork is only default on apache for compatibility.

    The best setup would then be MPM Worker, FastCGI with php-fpm (as worker and dso/suphp are not compatible), and APC for a fast functioning webserver that has low response times and good memory management utilizing the latest technology and threading. Can anyone assist me?

    Below is a screenshot fastcgi simply isn't listed.

    hostcontrol-no-fastcgi.png

    Additionally I can't select *any* handler for php with this --enable-fpm flag set. I get the following error:

    select.png

    error_whm.png

    I've also tried adding too the --enable-fastcgi flag to the php5_all since when I used php -i I noticed that easyapache was putting --disable-cgi flag on. This resulted in the same problem when I select cgi for the handler in whm. And doing this I doubt I can select fastcgi as the handler.

    I have read other posts via google that say you can select fastcgi as a module so what gives here? :( Is it my version of whm?
     

    Attached Files:

    #1 Atrix, Sep 7, 2012
    Last edited: Sep 7, 2012
  2. Atrix

    Atrix Member

    Joined:
    Sep 7, 2012
    Messages:
    9
    Likes Received:
    2
    Trophy Points:
    3
    cPanel Access Level:
    Root Administrator
    I got it working.

    Code:
    /var/cpanel/easy/apache/rawopts/all_php5
    contains
    Code:
    --enable-fastcgi
    --enable-fpm
    
    Then you recompile php and apache using easyapache with the mpm worker selected.

    Then you have to set up your
    Code:
    /etc/init.d/php-fpm 
    script which you copy from
    Code:
    /home/cpeasyapache/src/php-5.3.16/sapi/fpm/init.d.php-fpm.in
    Then you have to fill in your variables at the top of the file:
    Code:
    #! /bin/sh
    
    ### BEGIN INIT INFO
    # Provides:          php-fpm
    # Required-Start:    $remote_fs $network
    # Required-Stop:     $remote_fs $network
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: starts php-fpm
    # Description:       starts the PHP FastCGI Process Manager daemon
    ### END INIT INFO
    
    prefix=
    exec_prefix=
    
    php_fpm_BIN=/usr/local/sbin/php-fpm
    php_fpm_CONF=/usr/etc/php-fpm.conf
    php_fpm_PID=/var/run/php-fpm.pid
    
    php_fpm_BIN
    you might have to locate or create each of these files. php-fpm was in the path for me.
    Code:
    type php-fpm
    gave me that.

    php_fpm_CONF
    i copied
    Code:
    /home/cpeasyapache/src/php-5.3.16/sapi/fpm/php-fpm.conf.in
    to
    Code:
    /usr/etc/php-fpm.conf
    I had to edit it too for my user and group that apache runs under, this will vary server to server so you'll have to see what permissions your apache server needs
    Code:
    ; Unix user/group of processes
    ; Note: The user is mandatory. If the group is not set, the default user's group
    ;       will be used.
    user = apache
    group = nobody

    php_fpm_PID=/var/run/php-fpm.pid

    You will have to create this file it won't have permissions to create it itself even ran as root for some reason:
    Code:
    touch /var/run/php-fpm.pid

    Having fun yet? K now you need to make apache use it for php.
    Code:
    vim /usr/local/apache/conf/includes/post_virtualhost_global.conf
    you're going to need to add this to the end of the file:

    Code:
    LoadModule fastcgi_module modules/mod_fastcgi.so
    
    <IfModule mod_fastcgi.c>
            FastCGIExternalServer /usr/sbin/php-fpm -host 127.0.0.1:9000
            AddHandler php-fastcgi .php
    
            #<LocationMatch "/status">
            #  SetHandler php-fastcgi-virt
            #  Action php-fastcgi-virt /usr/sbin/php-fpm.fcgi virtual
            #</LocationMatch>
    
            Action php-fastcgi /usr/sbin/php-fpm.fcgi
            ScriptAlias /usr/sbin/php-fpm.fcgi /usr/sbin/php-fpm
    
            <Directory /usr/sbin>
            Options ExecCGI FollowSymLinks
            SetHandler fastcgi-script
            Order allow,deny
            Allow from all
            </Directory>
    </IfModule>
    Don't get ahead of yourself yet, we still haven't compiled mod_fastcgi from source:
    For which I followed this guide:
    FastCGI for Apache2

    Obviously ignore the bit about installing apache, as we already did that start with the part you download fastcgi.

    You'll have to change the source a bit differently than he lists tho here was my makefile:
    Code:
    #
    #  Makefile for Apache2
    #
    
    builddir     = .
    
    top_dir      = /home/cpeasyapache/src/httpd-2.2.22
    
    top_srcdir   = ${top_dir}
    top_builddir = ${top_dir}
    
    include ${top_builddir}/build/special.mk
    
    APXS      = /usr/local/apache/bin/apxs
    APACHECTL = /usr/local/apache/bin/apachectl
    
    #DEFS=-Dmy_define=my_value
    #INCLUDES=-Imy/include/dir
    #LIBS=-Lmy/lib/dir -lmylib
    INCLUDES=-I /home/cpeasyapache/src/httpd-2.2.22
    
    all: local-shared-build
    
    clean:
            -rm -f *.o *.lo *.slo *.la
    
    Then I ran into a problem with it not being able to create/read the log which would make nothing work:

    Code:
    touch /usr/var/log/php-fpm.log
    chmod 777 /usr/var/log/php-fpm.log
    
    chmod 777 is taboo and I'll fix it soon but it got it able to read it when nothing else seemed to.

    Lastly restart/start both of them:
    Code:
    /etc/init.d/php-fpm restart
    /etc/init.d/httpd restart
    Result: Well I've been optimizing our server for 2 weeks and this was the last thing. I'd already gotten the server response/page load time for our ecommerce php store to 500 to 600ms per product detail page according to firebug from times over 1.5 seconds. Now it's down to no larger than 437ms. You can blink and miss it seriously. It's well worth the effort I've never seen an ecommerce site this fast that runs off a php cart. It took me about 6 or 7 hours of researching around looking up info. Hopefully this will let someone else do it in far less time (my prior post was made a while after I'd started).
     
    #2 Atrix, Sep 7, 2012
    Last edited: Sep 7, 2012
    gwDeniz and eva2000 like this.
  3. Simsim

    Simsim Member

    Joined:
    Jan 25, 2012
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Oh . I wish if I can understand all that to get php fpm working on my server so I can use worker MPM. My server serve both PHP and static files and I am the only user in my server.

    Do you think I can apply all that ?
     
  4. Simsim

    Simsim Member

    Joined:
    Jan 25, 2012
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    I did it .. I did it .. php 5.4 with APC 3.1.13 + php FPM and Apache event mpm .. thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaank you .. you are great

    the server is very stable now (since two weeks) - max php fpm child reached is 36 with apache mpm event serve the static resources.

    even with huge online users .. the server cpu is low and memory is always ~ 42%



    But I have several points:

    1- We need to make mod_fastcgi installed by default when make update in easyapache .. I wish if somebody make it an "EasyApache Custom Module" so we don't have to install it manually after each apache build

    2- Steps to complete installing PHP-FPM should be done automatically after finishing updating . I wonder if somebody can do it for us. I think there is a hook in easy Apache can be utilized for this purpose.

    3- What about those people with many users on the same server ?

    4. we need to check if php-fpm is always running. and if something kill it, and it will start when system statrup.

    in any way .. I love you
     
  5. LeadDogGraphics

    LeadDogGraphics Well-Known Member

    Joined:
    Feb 25, 2012
    Messages:
    97
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    West Palm Beach, FL
    cPanel Access Level:
    Root Administrator

    Please advise the steps you took to get php FPM working. Also are you using apache 2.2.23 or apache 2.4?
     
  6. Simsim

    Simsim Member

    Joined:
    Jan 25, 2012
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hi .. I have followed the above steps by Atrix

    use php suexec as handler, Apache event mpm
    install APC via WHM , update it (pecl upgrade APC-beta) and make sure it is disabled in php.ini
    add fpm support to php build
    configure php fpm
    install mod_fastcgi
    update php.ini to enable APC (manually)
    start fpm master process
    add the suitable directives in Post VirtualHost Include to execute php files via php fpm, as stated above

    restart Apache

    without a single moment downtime!

    I don't know if this solution is suitable for multi-user server. I am the only user in my server. But I believe it can be easily modified to make it suitable and secure for servers with many users.


    Apache 2.4 is not an option in EasyApache, as far as I know
     
    #6 Simsim, Oct 23, 2012
    Last edited: Oct 23, 2012
  7. LeadDogGraphics

    LeadDogGraphics Well-Known Member

    Joined:
    Feb 25, 2012
    Messages:
    97
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    West Palm Beach, FL
    cPanel Access Level:
    Root Administrator
    Thank you for the details. You are correct, apache 2.4 is not yet available, but I am hoping soon.

    Right now i run my setup with DSO & mod_ruid2 with APC and Varnish Cache. I too am the only user on my server, but I host many managed client domains from my server. It works well, but I am looking to try out a similar setup to yours once apache 2.4 comes to easy apache.
     
  8. aww

    aww Well-Known Member

    Joined:
    Feb 10, 2005
    Messages:
    152
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Thanks for the great writeup.

    With this configuration does php run as the user or like mod_php does it run as nobody?

    What happens with existing multi-user setups where there are existing directories created as nobody?

    Does APC maintain a single instance when there are multiple php processes?

    Looks like this doesn't exist anymore in the newest easyapache
    Code:
    /var/cpanel/easy/apache/rawopts/all_php5
    Oh wait, I see, you have to create it from scratch http://docs.cpanel.net/twiki/bin/view/EasyApache3/CustomConfigureFlags


    Also you should switch from 127.0.0.1 (tcp/ip) to sockets for extra performance.
     
    #8 aww, Dec 2, 2012
    Last edited: Dec 2, 2012
  9. aww

    aww Well-Known Member

    Joined:
    Feb 10, 2005
    Messages:
    152
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Got this working extremely well now, thanks op.

    Only catches from the original instructions were using user "nobody" instead of "apache" and when you create the Makefile you have to be careful with tabs vs spaces as make doesn't like that, so copy and paste may not be clean. I also recommend switching to sockets instead of tcp/ip, which is also tested working well.

    Can I now compile PHP independently and overwrite the easyapache installed copy? I should be able to, just copy the resulting configure line from php.ini ? Of course any future use of easyapache will overwrite it again. But apache updates far less regularly than PHP.
     
  10. aww

    aww Well-Known Member

    Joined:
    Feb 10, 2005
    Messages:
    152
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    I wanted to point out one mistake I found in Atrix's write-up.

    Because easyapache builds two identical copies of php for some weird reason (see my post here) it is easy to start confusing paths and Atrix has made that mistake

    Half his instruction use /usr/bin and /usr/sbin and the other half use /usr/local/bin and /usr/local/sbin

    I would recommend using /usr/local/bin and /usr/local/sbin (note the "local") because that's where the stock php for apache typically executes from.
     
  11. swany

    swany Member

    Joined:
    Sep 21, 2011
    Messages:
    23
    Likes Received:
    1
    Trophy Points:
    3
    cPanel Access Level:
    Website Owner
    After messing around with mod_ruid2, fcgi and many other forms of running php with apache i have to say this is by far the best!

    php-fpm is very fast and my sites are incredibly responsive. Thanks for the guide!
     
  12. LeadDogGraphics

    LeadDogGraphics Well-Known Member

    Joined:
    Feb 25, 2012
    Messages:
    97
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    West Palm Beach, FL
    cPanel Access Level:
    Root Administrator
    Are there any updates or changes to get this working now with the supported Apache 2.4?

    Also now that php 5.4 is released in Easy Apache, it has php-fpm already installed it just needs to be enabled, so how do these instructions differ for that?

    I am thinking of giving this a go but don't want to get stuck in the middle with it not working.
     
    #12 LeadDogGraphics, Apr 1, 2013
    Last edited: Apr 1, 2013
  13. sonicthoughts

    sonicthoughts Well-Known Member

    Joined:
    Apr 4, 2011
    Messages:
    61
    Likes Received:
    3
    Trophy Points:
    8
    Any luck on getting this to work with native php-fpm (PHP 5.4 + Apache 2.4?) I realize there is a feature request: FastCGI Process Manager (FPM) SAPI | cPanel Feature Requests but would be great if we could update these steps. I'm a bit intimidated to much too much, but heard great things about apache 2.4 and php-fpm (with Events...) Heard you don't need to muck with Ngix or varnish. this is another route: cpXstack but would like to keep things simple ....
     
  14. aww

    aww Well-Known Member

    Joined:
    Feb 10, 2005
    Messages:
    152
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    I assume y'all want 2.4 for mpm event, because mpm worker on 2.4 is not going to have any performance benefit over 2.2

    According to this, it will work PHP-FPM - Httpd Wiki

    But they switch from mod_fastcgi to mod_proxy + mod_proxy_fcgi and that might require additional configuration.

    Also if you reverse proxy with nginx on top of all this, mod_rpaf will not work as is with Apache 2.4 you have to edit the mod_rpaf-2.0.c source and search/replace "remote_" with "client_"
    and recompile /usr/local/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

    But before you get crazy about doing this - check out this benchmark Web server performance benchmark | Root Users (scroll down to the graphs under "results")
    On those graphs apache 2.2 is in worker, and apache 2.4 is in event, and there is at best a 10% performance improvement.

    If this was an effortless upgrade, probably worth it, but there are going to be issues.

    This of course assumes mod_proxy+mod_proxy_fcgi is just as fast as mod_fastcgi and not slower. If it's slower, not worth it at all.
     
    #14 aww, Apr 6, 2013
    Last edited: Apr 6, 2013
  15. LeadDogGraphics

    LeadDogGraphics Well-Known Member

    Joined:
    Feb 25, 2012
    Messages:
    97
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    West Palm Beach, FL
    cPanel Access Level:
    Root Administrator
    I am looking to achieve speed and security.

    With using mod_ruid2, mod_security does not work properly and often errors due to threads changing, therefore filling the logs with mutex errors.

    I figure with a switch to apache 2.4 event with php-fpm and using sockets, I can keep the speed of mod_ruid2, regain the ability to properly use mod_security and also keep the ability to use APC.
     
  16. sonicthoughts

    sonicthoughts Well-Known Member

    Joined:
    Apr 4, 2011
    Messages:
    61
    Likes Received:
    3
    Trophy Points:
    8
    This seems to be the only real apache solution: /http://pbweb.co.uk/php-handlers/
    PHP Handlers - FastCGI, Mod_PHP, CGI, Ruid2, suPHP and PHP-FPM - shame this is hidden in forums like these.
     
  17. yannisc

    yannisc Member

    Joined:
    Oct 10, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    1
    I followed the guide, but I get this error when trying to start php-fpm:

    Starting php-fpm [16-Jul-2013 10:08:03] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
    [16-Jul-2013 10:08:03] ERROR: FPM initialization failed
    failed

    Any hints?
     
  18. thinkbot

    thinkbot Well-Known Member

    Joined:
    Oct 30, 2012
    Messages:
    326
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    port is already taken, seems like something else is running there, or php-fpm is already started
     
  19. STS Admin

    STS Admin Well-Known Member

    Joined:
    Jul 8, 2012
    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    India
    cPanel Access Level:
    Root Administrator
    You can use the below command to see which application is already listening on that port. Then kill it and restart the php-fpm
     
  20. yannisc

    yannisc Member

    Joined:
    Oct 10, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    1
    Thank you for your help!
     
Loading...

Share This Page