CentOS 5.5 + PHP 5.3.3 + PHP-FPM + APC + nginx (yum RPMs)

(2011) Update: IUS renamed the packages to have a suffix of “u”. This was done to avoid conflicts with CentOS’s recently released PHP 5.3 packages. I prefer IUS since it maintains PHP’s version numbers and there’s some handy PECL extensions.
(2010-12-03) Update: EPEL updated nginx to 0.8.53 around early November. This post has been updated to use that package instead of IUS 0.7.
(2010-09-22) Update: IUS has fixed the repo: https://bugs.launchpad.net/ius/+bug/645409
(2010-09-22) Update: IUS updated the PEAR and APC packages and their repo appears to have come out of sync so you’ll experience errors until it’s fixed. Download the packages manually or use APC from repo 5 and FPM from repo 5.5 while making sure to “yum clean all” in between.
(2010-09-16) Update: I completely forgot that PHP-FPM also solves the APC cache issues that were present with php-fcgi where every php-fcgi process had it’s own APC cache. I include those steps.

IUS Community recently published PHP 5.3.3 PHP-FPM package but it isn’t in the default repo (which appears to be CentOS 5.4 and not 5.5). If you cannot install php53u-fpm then try changing the repo as detailed here:
https://bugs.launchpad.net/ius/+bug/591609

This post assumes that PHP is not already installed. If it is you’ll possibly get conflicts with some of the PHP packages when installing the IUS ones. IUS has a “yum replace” script but I have not used it. Otherwise, uninstall the old PHP RPMs before proceeding.

Install EPEL and IUS-Community repo packages:

#EPEL
wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/epel-release-1-1.ius.el5.noarch.rpm
#IUS Community
wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/ius-release-1.0-6.ius.el5.noarch.rpm
#Install repos
rpm -Uvh epel-release-1-1.ius.el5.noarch.rpm ius-release-1.0-6.ius.el5.noarch.rpm

You’ll then need to edit the ius repo to force it to CentOS 5.5 (check if this is still necessary by trying the next step first):

vi /etc/yum.repos.d/ius.repo
#Comment out this line:
mirrorlist=http://dmirr.iuscommunity.org/mirrorlist?repo=ius-el5&arch=$basearch
#Add this line:
baseurl=http://dl.iuscommunity.org/pub/ius/stable/Redhat/5.5/$basearch

Now you can easily install PHP 5.3.x, the new PHP-FPM package, and APC (it works correctly with PHP-FPM, yay) via:

#IUS PHP 5.3 packages are prefixed with "php53u" to avoid name clashes
yum install php53u-fpm php53u-pecl-apc
#Accept the dependencies

Next install nginx from IUS. Note that this is nginx 0.7.65 so it appears to have the usual backports from 0.8.

Next install nginx 0.8.53 from EPEL.

#Use EPEL nginx. See directly below if you want IUS 0.7
yum install nginx
#Note that IUS's nginx is suffixed with "07"
#yum install nginx07

Now you have the necessary packages installed. They don’t Just Work(TM) together out of the box so you’ll need to do some minor config changes.

For nginx edit /etc/nginx/nginx.conf and uncomment the entire block shown below to enable fastcgi forwarding for .php requests:

#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}

Within that same block also change:

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

To:

fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;

Now make a phpinfo to test:

#mmm phpinfo
echo "<?php phpinfo();" > /usr/share/nginx/html/phpinfo.php
#make sure APC is running. then copy the stats
cp /usr/share/doc/php53-pecl-apc-3.1.3p1/apc.php /usr/share/nginx/html/

Start it up:

chkconfig php-fpm on
chkconfig nginx on
service php-fpm start
service nginx start

Try it all out in a browser.

Note that this was the least number of steps to get this stack going. As usual be sure to check the other config settings for performance and security, like nginx’s number of connections and workers and changing php-fpm’s default user/group from the default of apache and chroot it to a jail.

This entry was posted in CentOS, PHP and tagged , , , , . Bookmark the permalink.

67 Responses to CentOS 5.5 + PHP 5.3.3 + PHP-FPM + APC + nginx (yum RPMs)

  1. bob says:

    Doesnt work.

    Everything seems to go well (except that nginx and php-fpm do not restart on reboot) however the phpinfo returns a blank page.

    • Rob Olmos says:

      OK sorry about that. I probably missed a step so let me re-run through it and check. Thanks for your input!

    • Rob Olmos says:

      I clarified the nginx.conf steps a bit and added the chkconfig lines. I did the steps start to finish on a fresh CentOS 5.5 install and it works fine. Any errors in the logs?

  2. bob says:

    Thank you for responding.

    I have checked the logs and only inconsequential ‘favicon’ errors are listed in nginx log. The access log shows that it used get on ‘test.php’ but nothing else seems out of the ordinary.

    A modification that I did make is that I was using 32 bit centos so I changed each file to its 32 bit counterpart. Today I will change the os on my vps to 64 bit and try again.

    I will reply to let you know if this makes a difference.

  3. bob says:

    Ok, I tried it with 64 bit centos and it worked.

    Thanks

  4. bob says:

    Whether it was a mistake by me or what I do not know, but it now works with the 32 bit version very well.

    This is by far the easiest install of nginx and php available on the internet.

    Please keep up the good work.

  5. arcadebb says:

    Rob Olmos, you rocks! I just created a SAN 2.0 VPS with bare Centos 5.5 + PHP 5.3.3 + PHP-FPM + Nginx 0.7.65.

    • Brian says:

      I’m trying to do the same, but I can’t install APC…. I’ve tried both repo’s (5 & 5.5)

      Error Downloading Packages:
      php53-common-5.3.3-4.ius.el5.x86_64: failure: php53-common-5.3.3-4.ius.el5.x86_64.rpm from ius: [Errno 256] No more mirrors to try.
      php53-cli-5.3.3-4.ius.el5.x86_64: failure: php53-cli-5.3.3-4.ius.el5.x86_64.rpm from ius: [Errno 256] No more mirrors to try.
      php53-5.3.3-4.ius.el5.x86_64: failure: php53-5.3.3-4.ius.el5.x86_64.rpm from ius: [Errno 256] No more mirrors to try.

  6. stephan says:

    Installed without issues – however, having issues with installing php-mysql which i need. Any advice? Thanks…

    Transaction Check Error:
    file /etc/php.ini from install of php-common-5.1.6-27.el5.x86_64 conflicts with file from package php53-common-5.3.3-4.ius.el5.x86_64

    • Rob Olmos says:

      You’ll have to uninstall the old PHP 5.1.6 RPMs before installing the new 5.3.3 ones. This install was done with a stock Rackspace Cloud Server install which doesn’t have PHP installed which I forgot to detail and will do so. Thanks for letting me know! There’s actually a “yum replace” script from IUS that will do so automatically but I haven’t used it.

  7. stephan says:

    I started from a bare centos 5.5, there was no php installed prior to following your guide. It’s when i tried installing php-mysql that I got the error. Excuse my ignorance, this is not my area of expertise, any pointers much appreciated :)

    [root@myren ~]# rpm -qa |grep -i php
    php53-cli-5.3.3-4.ius.el5
    php53-common-5.3.3-4.ius.el5
    php53-pear-1.8.1-4.ius.el5
    php53-fpm-5.3.3-4.ius.el5
    php53-5.3.3-4.ius.el5

    [root@myren ~]# yum install php-mysql
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    * addons: mirrors.serveraxis.net
    * base: centos.corenetworks.net
    * epel: mirrors.tummy.com
    * extras: yum.singlehop.com
    * updates: mirror.ubiquityservers.com
    Setting up Install Process
    Resolving Dependencies
    –> Running transaction check
    —> Package php-mysql.x86_64 0:5.1.6-27.el5 set to be updated
    –> Processing Dependency: php-common = 5.1.6-27.el5 for package: php-mysql
    –> Processing Dependency: php-pdo for package: php-mysql
    –> Running transaction check
    —> Package php-common.x86_64 0:5.1.6-27.el5 set to be updated
    —> Package php-pdo.x86_64 0:5.1.6-27.el5 set to be updated
    –> Finished Dependency Resolution

    Install 3 Package(s)
    Upgrade 0 Package(s)

    Total download size: 307 k
    Is this ok [y/N]: y
    Downloading Packages:
    (1/3): php-pdo-5.1.6-27.el5.x86_64.rpm | 66 kB 00:00
    (2/3): php-mysql-5.1.6-27.el5.x86_64.rpm | 89 kB 00:00
    (3/3): php-common-5.1.6-27.el5.x86_64.rpm | 153 kB 00:00
    ———————————————————————————————————————————————————————————————————————————————————————————————————-
    Total 425 kB/s | 307 kB 00:00
    Running rpm_check_debug
    Running Transaction Test
    Finished Transaction Test

    Transaction Check Error:
    file /etc/php.ini from install of php-common-5.1.6-27.el5.x86_64 conflicts with file from package php53-common-5.3.3-4.ius.el5.x86_64

    • Rob Olmos says:

      No worries. I see the problem: The IUS packages are prefixed with “php53″ to avoid clashes. You’ll need to use “php53-mysql” instead of “php-mysql” for installing that package.

  8. stephan says:

    Oh man, that was way too easy, thanks!

  9. Pingback: CentOS 5.5 PHP 5.3.3 PHP-FPM nginx (RPMs) | Rob Olmos - Phat Site Blog

  10. kelvin says:

    When I follow the instructions,I am told that the packages do not exist

  11. kampreto says:

    i got this error when running

    yum install php53-fpm php53-pecl-apc

    ERROR with rpm_check_debug vs depsolve:

    i’m using centos 5.5 32bit

  12. Victor says:

    Thanks a lot for the article, it saved me a lot of frustration with figuring out the new install, everything went very smoothly! :)

  13. UN1X says:

    Your info is very useful for me ..

    i think the compiling method still better (regarding performance issue) for me.. but .. the integration between apc and others modules will be easy with this method

    Thank You

  14. ETB says:

    Same here: centos 5.5 x86_64 – phpinfo page is blank.
    Installed:
    php53-mysql-5.3.3-4.ius.el5
    php53-fpm-5.3.3-4.ius.el5
    php53-cli-5.3.3-4.ius.el5
    php53-pecl-apc-3.1.4-1.ius.el5
    php53-devel-5.3.3-4.ius.el5
    php53-pear-1.8.1-5.ius.el5
    php53-pdo-5.3.3-4.ius.el5
    php53-5.3.3-4.ius.el5
    php53-common-5.3.3-4.ius.el5

    Nothing in log

  15. ETB says:

    give up with posting code, simply: there should be ?php not just ? at script opening

  16. Stefan says:

    Been playing around with nginx on a CentOS 5.5 VirtualBox install for a few days now. It’s been a straight nightmere dealing with spawn-fcgi in conjuction with nginx and CentOS. These instructions worked perfectly for me. Also added php-mysql and php-gd without any issues. Great job and thank you so much for sharing this. Couldn’t be happier right now. Bookmark set. :)

    Thanks again,
    Stefan

  17. gilank says:

    work perfectly great!

  18. Kangbud says:

    Great tutorial,
    how about define virtual host ? can you make a tutorial too about that ?
    I try some times but it not work

  19. daniel says:

    Thanks for the great tutorial.
    When executing “yum install php53-fpm php53-pecl-apc”
    I am getting “No package php53-fpm available.”
    I searched with ‘yum list’ for packages with similar names but found nothing.
    Can you please help?

  20. daniel says:

    Found the answer here:
    http://blog.dengruo.com/201009/978

    Instead of:
    yum install php53-fpm php53-pecl-apc

    I used:
    yum install php53-pecl-apc

    rpm -ivh http://dl.iuscommunity.org/pub/ius/stable/Redhat/5.5/x86_64/php53-fpm-5.3.3-4.ius.el5.x86_64.rpm
    yum install php53-fpm

    • Rob Olmos says:

      The linked blog is currently unavailable so I’ll have to check later to see what the referenced answer is.

      I’m not sure why the package is not available for you. I just tested it again and it found the packages fine. You could try “yum clean all” to force yum to update the list of packages. Glad you figured it out though.

  21. tonyd says:

    I was under the impression php 5.3.3 included php-fpm?

    • Rob Olmos says:

      Yes and no. PHP 5.3.3 now includes PHP-FPM within the PHP source but still requires a configure/compile switch to enable it (–enable-fpm).

      The php53 package from IUS does not build in the PHP-FPM while the php53-fpm package does build it in but into a separate binary (/usr/bin/php vs /usr/sbin/php-fpm since it’s designed to be a server) and also provides the surrounding configs, init.d script, etc.

      The separation of the two binaries feels well designed and cleaner to me.

  22. Completedo says:

    Hey, thanks a lot for the guide. It has been very useful. Is there any reason for installing nginx 0.7.65 instead of the newer 0.8.53?

  23. Completedo says:

    Yum, now I see that when you wrote the article the 0.8 branch was not a stable version :-)

    • Rob Olmos says:

      Yup that’s correct. I noticed this myself about a week ago but forgot to update this post. Thanks for reminding me.

  24. Mikkel koch says:

    A few steps is missing.

    Disable apache before trying to start nginx. I got a listen to port 80 error.

    So service httpd stop and maybe a way to stop it from starting at boot up.

    If you got php installed allready this worked for me.

    yum remove php*.*

    apc could be updated, 3.1.16 has been released.

  25. Mike Sayre says:

    If you are using Zend for WHMCS or any other PHP 5 script, this install won’t work.

    PHP 5.3+ isn’t supported by Zend yet, and will take some manual coding of your Zend PHP scripts to make it work.

    Zend only supports upto PHP 5.214, just an fyi.

    Mike

  26. badtz says:

    hrmm, wanted to play around and found this, everything works up until i go to open the page in a browser, it dwonloads instead of renderin ghte php, what am I missing?

  27. badtz says:

    Doh, dont comment out
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; but remove/delete the line

    silly badtz.

  28. badtz says:

    and thank you, this was simple concise and minus my typos worked with no issues on 5.5 as of today. I did have to change some paths but that was easy enough. Thanks for a great tutorial.

  29. amir says:

    Hey,

    I think as Jan 04-11 the packages are now called php53u-fpm and php53u-pecl-apc

    Attempting to install right now .. just waiting for yum remove php to finish

    • YH says:

      Took me ages… this comment is a lifesaver! it would be really helpful to update the tutorial, it is an excellent clear one.

  30. stephan says:

    Any tips on how to get php53-sqlite installed ? There’s no IUS package…

  31. Ozia says:

    Hi, Everything works fine untill I setup a WordPress, it says ‘Sorry, but I can’t write the wp-config.php file.’ and I have to manually add that file, I think there’s something wrong with file permissions, I downloaded and unzipped WordPress via wget the, I see the default owner group is 33:33, I’m not very good at CentOS. Any suggestion? Thanks.

  32. Ozia says:

    Aha, I found that: in /etc/php-fpm.d/www.conf, change the user and group from apache to nginx (I complied nginx with –user=nginx –group=nginx), and then restart php-fpm, then it seems works. cheers.

  33. Ozia says:

    Just one problem. it seems that php-fpm doesn’t automatically start after VPS rebooting.

  34. Mohammed says:

    When I go to the last step I get the following error:
    # service nginx start
    Starting nginx: [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
    [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
    [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
    [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
    [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
    [emerg]: still could not bind()
    [FAILED]

  35. Tim says:

    Hi, I’ve just gone through this on a clean CentOS 5.6 (freshly yum updated, Jun-1-11) 64bit OpenVZ VM. Install goes smoothly; there was an issue with a bit of whitespace in the fastcgi_param line if direct copy-paste from textbox above; but once that is clear nginx starts up fine; and php-fpm processes are runing as is nginx. However when I visit a phpinfo page – all I get is blank (zero content returned). This sounds similar to the first poster in this forum .. I’m unclear, is there any obvious / known reason why this might be the case ? Thanks! –Tim

    • Tim says:

      A footnote: After a nights sleep; reviewed my config again. Problem was that I copy-pasted the php stanzas (rather than uncommenting existing ones already present in the default nginx.conf file); this resulted in unexpected behaviour (things were broken). The cause for ‘blank page’ was that the path ‘didn’t exist’ for the ‘root’ line stanza. Adjusted this line, restart nginx, bouf, things work fine. This after reading other various online hints, especially one comment from URL, “https://bbs.archlinux.org/viewtopic.php?id=101750″ which stated nicely, “The blank page is a silent error saying “this script doesn’t exist”.

      Anyhoo . I hope this might help someone else doing the walk through, maybe. Great guide, BTW!

      Tim

  36. Danny says:

    Does the file ‘php53u-fpm’ include a full install of PHP5.3? All I’m really after is an RPM based php-fpm. I have everything working with nginx / PHP / php-fpm, the only thing I don’t have is an init.d script for php-fpm so if the server is rebooted I have to start that manually

  37. Pingback: 启程 | blog.im942.com

  38. Pingback: Apache Alternatives for Serving PHP | Wazi

  39. Pingback: Apache Alternatives for Serving PHP | radworx.com

  40. Pingback: CentOS 5.5 + PHP 5.3.3 + PHP-FPM + APC + nginx (yum RPMs) | 佳良_技術筆記本

  41. Pingback: » Linuxaria – Everything about GNU/Linux and Open source Apache Alternatives for Serving PHP

  42. Beginner says:

    Hi just wanted to say thank you!

    I’ve been struggling with PHP to get it to work for hours!

    You probably also want to add MySQL support, which is extremely easy once you find it;
    Just add “php53u-mysql” to the “yum install php53u-fpm php53u-pecl-apc” line.

    Again, thanks!

  43. Keddy says:

    Thank You, it works perfect!

    You can also add php-mysql support;)

  44. Sibil Mohammed says:

    Worked very well for me on RHEL 6.
    Only change:
    baseurl=http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/

    Thanks a lot.

  45. Emran Hasan says:

    Very nice and simple post…I installed Nginx and PHP-FPM in CentOS 5.6 without any issue and now running it happily! The comments also helped as well.

  46. atesin says:

    so many thanks !! .. worked like a charm at first try in a recently installed centos 57 !!

    just a few advices:
    - ius repo official page is mentioned at official php downloads page
    - you may correct your paths and your versions
    - if you run php at same machine as nginx, you may try unix sockets instead tcp

    edit /etc/php-fpm.d/www.conf , line 12:
    listen = /var/lib/php/php-fpm.sock

    edit /etc/nginx/nginx.conf , section “location ~ \.php {”
    #fastcgi_pass 127.0.0.1:9000;
    fastcgi_pass unix:/var/lib/php/php-fpm.sock;

    - additionally you can include ‘service php-fpm start|stop’ in nginx initscript

    see you

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>