PHP4 (dso) and PHP5 (fcgi) with apache2 on CentOS-4.5

I've had php4 running with apache2 for a while now and wanted to get php5 also simultaneously running. There are other ways to do this, but I opted with getting php5 to run as fastcgi and keep php4 as apache2 module so they can co-exist together.

This was done on CentOS release 4.5 with ISPConfig Control Panel on a dedicated hosting scenario. If you're looking to share the server with others then you might want to set up with suphp/SuExec for security purposes.

  • Preparation:

    1. There was a fair bit of complications when compiling php. First, suggest installing the below devel rpms so you don't get errors when compiling php.

      # yum install curl-devel gmp-devel libc-client-devel ncurses-devel aspell-devel libxslt-devel libgcrypt-devel

    2. php-5.2.x will also require pcre-6.6.1 so download the source rpm from centos-5 and rebuild it for centos-4.5

      # yum install rpm-build
      # cd /usr/src/redhat/SRPMS/
      # wget ftp://ftp.pbone.net/mirror/ftp.centos.org/5.0/os/SRPMS/pcre-6.6-1.1.src.rpm
      # rpmbuild --rebuild pcre-6.6-1.1.src.rpm
      # rpm -Uvh ../RPM/i386/pcre-*.rpm

  • Installation:

    1. Install fcgi libraries:

      $ wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
      $ tar -xvzf fcgi-2.4.0.tar.gz
      $ cd fcgi-2.4.0
      $ ./configure
      $ make
      # make install

    2. Install mod_fastcgi apache module:

      $ wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.2.tar.gz
      $ tar -xvzf mod_fastcgi-2.4.2.tar.gz
      $ cd mod_fastcgi-2.4.2
      $ cp Makefile.AP2 Makefile
      $ make top_dir=/usr/lib/httpd
      # make top_dir=/usr/lib/httpd install

    3. Now for the php installation, download and extract the source from one of the mirrors at php.net.

      I used the existing phpinfo() on php4 to generate the configure arguments, but had to adjust it a bit as some of php4 options were deprecated or changed.

      To compile with fcgi support, the below is what are essential.

      '--enable-fastcgi' \
      '--enable-discard-path' \
      '--enable-force-cgi-redirect' \

      Here is what I used to configure:

      $ ./configure \
      '--prefix=/usr/local/php5' \
      '--enable-fastcgi' \
      '--enable-discard-path' \
      '--enable-force-cgi-redirect' \
      '--disable-debug' \
      '--with-pic' \
      '--disable-rpath' \
      '--enable-inline-optimization' \
      '--with-bz2' \
      '--with-db4=/usr' \
      '--with-curl' \
      '--with-freetype-dir=/usr' \
      '--with-png-dir=/usr�39; \
      '--with-gd=shared' \
      '--enable-gd-native-ttf' \
      '--without-gdbm' \
      '--with-gettext' \
      '--with-ncurses=shared' \
      '--with-gmp' \
      '--with-iconv' \
      '--with-jpeg-dir=/usr' \
      '--with-openssl' \
      '--with-pspell' \
      '--with-xsl=/usr' \
      '--with-xmlrpc=shared' \
      '--with-pcre-regex=/usr' \
      '--with-zlib' '--with-layout=GNU' \
      '--enable-bcmath' \
      '--enable-exif' \
      '--enable-ftp' \
      '--enable-magic-quotes' \
      '--enable-sockets' \
      '--enable-sysvsem' \
      '--enable-sysvshm' \
      '--enable-wddx' \
      '--with-pear=/usr/share/pear' \
      '--with-imap=shared9; \
      '--with-imap-ssl' \
      '--with-kerberos' \
      '--with-mysql=shared,/usr' \
      '--enable-shmop' \
      '--enable-calendar'; \
      '--enable-mbstring=shared' \
      '--enable-mbregex' \
      '--with-mime-magic=/usr/share/file/magic.mime'

      Once configured.

      # make && make install

    4. Create the php.ini file and make sure to include the dynamic shared objects, specifically gd.so and mysql.so as it's very commonly used.

      $cp php.ini-dist /usr/local/php5/etc/php.ini

    5. Check with:

      /usr/local/php5/bin/php-cgi -v

      Note, the output should produce, the cgi-fcgi build as below.

      PHP 5.2.4 (cgi-fcgi) (built: Sep 12 2007 22:50:06)
      Copyright (c) 1997-2007 The PHP Group
      Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

  • Configuration:

    1. First configure mod_fcgi to load by adding the line below to "httpd.conf" in the Dynamic Shared Object Section.

      LoadModule fastcgi_module modules/mod_fastcgi.so

      While, at it check that "mod_actions.so" is also being loaded.

    2. Add the below after the apache User/Group is set, possibly at the end of the httpd.conf file just before any vhosts include section:

      # FastCGI directives
      <IfModule mod_fastcgi.c>
        FastCgiIpcDir /tmp/fcgi_ipc/
        AddHandler fastcgi-script .fcgi
        FastCgiConfig -autoUpdate -singleThreshold 100 -killInterval 300 -idle-timeout 240 -pass-header HTTP_AUTHORIZATION
      </IfModule>

    3. Prepare the Ipc Directory:

      # mkdir /tmp/fcgi_ipc
      # chown apache:apache /tmp/fcgi_ipc/

    4. Add the below handler and action to the specific virtual host directive as needed, in order to bind .php and .php5 file extension to FastCGI config script php5.fcgi:

      <Directory /path/to/directory/>
      AddHandler application/x-httpd-php5 .php .php5
      Action   &nbsp; application/x-httpd-php5 /cgi-bin/php5.fcgi
      </Directory>

      Note: on ISPConfig, there is a section under the sites 'optional Apache Directives' where it needs to be entered, else you may end up losing the customization if put directly into the vhosts conf file.

    5. Create the "php5.fcgi" file in the sites cgi-bin directory and make it executable:

      $ cat <<EOF > /path/to/site/cgi-bin/php5.fcgi
      #!/bin/sh
      PHP_FCGI_CHILDREN=2
      export PHP_FCGI_CHILDREN
      PHP_FCGI_MAX_REQUESTS=5000
      export PHP_FCGI_MAX_REQUESTS
      exec /usr/local/php5/bin/php-cgi />EOF
      $ chmod 755 path/to/site/cgi-bin/php5.fcgi

    6. Test and restart apache, then create a phpinfo() page to check that php5 is indeed being run.

      # service httpd configtest
      # service httpd restart

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pcre srpm is hard to find

I found it here:
http://code.google.com/p/google-search-appliance-mirror/downloads/detail?name=pcre-6.6-1.1.src.rpm&can=2&q=

by chance totally.

Direct link:
http://google-search-appliance-mirror.googlecode.com/files/pcre-6.6-1.1.src.rpm

strace and error logs...

Anything in the error logs?

Make sure that FastCgiIpcDir has the right permissions.

What do you get with strace for the fastcgi process?

Help please, problems with FastCGI and PHP5

Thanks for your HOWTO. I've found preciously little out "there" on this specific topic.

I have followed your instructions but am having some problems in the last stages, any help would be appreciated.

The specs:

Apache 2.0.59
PHP 5.2.5
FastCGI 2.4.0
mod_fastcgi 2.4.6

The problem:

I want to have a web server directory (in this case a whole virtual host) that executes PHP scripts via PHP5 FastCGI.

Apache2 loads mod_fastcgi happily. My virtual host contains a Directory directive:

AddHandler application/x-httpd-php5 .php .php5
Action application/x-httpd-php5 /cgi-bin/php5.fcgi

The php5.fcgi script contains the following:

#!/bin/sh
PHP_FCGI_CHILDREN=2
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS
exec /usr/local/php5/bin/php-cgi

The executable (php-cgi) it refers to is in place and tested ok.

But, when I access / execute a PHP script in that directory Apache appears to hand to control over to FastCGI and then hang until Apache returns a 500 Internal Server Error.

To me it seems that FastCGI is not responding or handing the script over to the PHP5 executable as it should.

Any ideas what I should look at next?

Thanks!

Paul S.
paul (*dont_spam_me*) procursa (d0t) com

Comment