PHP4 and PHP5 Side-By-Side

| 30 Comments | 1 TrackBack

As I'm sure a lot of you saw, we got PHP5 working alongside PHP4 over at A Small Orange. It took a little bit of Google and fiddling around, but it's working great so far. In the interest of helping out where we can, I'll post the full instructions on how to set it up here.

The basic architecture runs PHP4 as a module in Apache and PHP5 as a FastCGI. This allows you to have speedy operation with both but without the problems of symbol overlap that you would have if trying to load them both as modules in Apache. PHP5 will exist as a separate install from whereever you have PHP4 installed. For the purpose of this article, let's assume you already have PHP4 installed as a module and it's up and running just fine. There are other articles for that anyways.

First, there are the dependencies. Assuming you've install Apache 1.x, libxml2 may or may not be installed on your system. So, to fix that, let's get it installed. If you've got CentOS or RHEL 3 installed, it's best to get the latest and greatest, rather than the 2.5 version out of your up2date or yum repository. So, let's grab from Dag's RPM repository:

wget http://dag.wieers.com/packages/libxml2/libxml2-2.6.16-1.1.el3.rf.i386.rpm \
http://dag.wieers.com/packages/libxml2/libxml2-python-2.6.16-1.1.el3.rf.i386.rpm \
http://dag.wieers.com/packages/libxml2/libxml2-devel-2.6.16-1.1.el3.rf.i386.rpm
rpm -Uvh libxml*
rm -f libxml2-*

If you have CentOS or RHEL4, yum/up2date is probably easier. So just yum install -y libxml2-devel or up2date -u libxml2-devel to get something recent enough on there.

You should also need FastCGI installed before PHP5 can compile for it, so let's get that installed.

cd /usr/local/src/   
wget http://fastcgi.com/dist/fcgi-2.4.0.tar.gz http://fastcgi.com/dist/mod_fastcgi-2.4.2.tar.gz
tar zxf fcgi-2.4.0.tar.gz
tar zxf mod_fastcgi-2.4.2.tar.gz
cd fcgi-2.4.0
./configure && make && make install
cd ..
cd mod_fastcgi-2.4.2
/usr/local/apache/bin/apxs -o mod_fastcgi.so -c *.c
/usr/local/apache/bin/apxs -i -a -n fastcgi mod_fastcgi.so

Now, let's configure Apache to use FastCGI properly:

mkdir /tmp/fcgi_ipc/
mkdir /tmp/fcgi_ipc/dynamic/
chmod -R 777 /tmp/fcgi_ipc/

cat <<EOT >> /usr/local/apache/conf/httpd.conf

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

EOT

Restart Apache and you should be good to go with FastCGI. This can be used for a variety of other languages and applications besides PHP, so it's good to have installed anyways. Ruby on Rails would be a prime example. Just as a note, if you ever get an hassle starting up Apache about the fcgiipc directory and some -1's in the output, just run chmod -R 777 /tmp/fcgiipc and that should fix it up.

Next, let's get a FastCGI copy of PHP installed. First, grab PHP and extract it:

cd /usr/local/src
wget http://us2.php.net/distributions/php-5.1.2.tar.bz2
tar xjf php-5.1.2.tar.bz2
cd php-5.1.2

Now comes the configuration part. I've included some options for MySQL and GD w/ FreeType, but those are optional. You may want to compile in other modules. I'll leave that to you to figure out, and it should not be much more than installing any dependencies and adding to this command:

./configure --prefix=/usr/local/php5-fcgi --enable-fastcgi --enable-discard-path --enable-force-cgi-redirect \
--enable-ftp --enable-memory-limit --with-mysqli --with-mysql=/usr  --with-zlib --enable-sockets \
--with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr --with-xpm-dir=/usr/X11R6 \
--with-freetype-dir=/usr --enable-gd-native-ttf

The only important part of that command is the first line. It specifies to install it into /usr/local/php5-fcgi, so it remains separate from your existing copy of PHP. The rest is to enable FastCGI support and so it runs properly. Now, let's build and install:

make && make install 
cp php.ini-dist /usr/local/php5-fcgi/lib/php.ini

You've now got a separate copy of PHP5 installed that can be used with FastCGI. Excellent! YOu can double-check by running /usr/local/php5-fcgi/bin/php -v and this should spit out:

root@beta [~]# /usr/local/php5-fcgi/bin/php -v
PHP 5.1.2 (cgi-fcgi) (built: Feb 13 2006 13:42:12)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies

So, that's all the grunt work. Now we just need to glue together our .php5 files with this copy of PHP. Very simple stuff. You'll need to add these two lines to your apache configuration:

Action  application/x-httpd-php5 /php5.fcgi
AddType application/x-httpd-php5 .php5

All that tells Apache to do is to run every request for a .php5 file through a file named php5.fcgi in the root directory of the site you're on. So, what's in that file? Another quickie:

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

The key thing is to make sure it's chmod'ed 777 and is owned by the user that site is running under. The nicest thing about this setup is that it runs suexec'ed, so you know exactly which user a script is running under. You also don't have to worry about files being owned by nobody, since PHP isn't running under that account anymore.

The other cool thing you can do is set up your own personal php.ini file under this system. Just add -c /home/username/php.ini to the end of the last line and pop the default php.ini into your home directory. You can tweak it to your heart's content to tune PHP however you'd like.

So, it's nothing amazingly complex, but it's incredibly powerful. When we go clustered at ASO, we'll be running all PHP with FastCGI and SuExec. We'll finally be able to track down exactly who is using what for system resources with Linux process accounting and a lot of the headaches of the current PHP setup will go away. PHP as a module can be great, but the features and flexibility afforded by FastCGI are even better.

1 TrackBack

TrackBack URL: http://www.timdorr.com/cgi-bin/MT/mt-tb.cgi/323

The idea of using FastCGI first struck me on Monday. I had previously transitioned my server to use SuEXEC for PHP. Pages were loading slower than I would have liked. PHP was running as a CGI application. Each time there was a request for a page the se... Read More

30 Comments

I finally got all of this working on my new home dev server. For some one who is not yet experienced with Linux, it was quite a challenge. I did learn a lot though, and it's sweet to have 4 and 5 running side by side. I can write scripts to take advantage of the latest PHP offerings and still have the legacy support that PHP 5 breaks.

Very nice. Although your implementation of FCGI and it's module seems a touch muddy and didn't work on my server. I went to the Rails site for more info on setting up FCGI, once that was done this PHP5 installation was a breeze to get up and running

If you have problems with :
/usr/local/apache/bin/apxs -o mod_fastcgi.so -c *.c

Just do:
cp Makefile.AP2 Makefile

#to change top_dir (for me : /usr/lib/httpd)
vi Makefile

make
make install

This is what I'm getting when I check the php -v:

root@ns1 [/usr/local/php-5.1.4]# /usr/local/php5-fcgi/bin/php -v
PHP 5.1.4 (cli) (built: Jun 12 2006 16:36:23)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies


Then when I try to load a simple phpinfo() php5 page, this is what I get:

(none) devcdg.com 71.216.201.211 Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4 /info.php5 403


Got any hints? It does not look like Apache is passing the request to php5-fcgi correctly.

You built the CLI version (or installed it, at least). You need to build the CGI version.

That is exactly what I was thinking. Ok I found that I was giving the php compile a mixed message when I had with-apxs. Once I took with-apxs out, compiled, and installed, php now says it is running as cgi-fcgi. Good but not quite there yet. I am still getting the same message in my browser when I try to call my phpinfo.php5 file. The troubling part is the 403 at the end of the message. I think this Apache telling me that it doesn't have permissions to do something. As far as I know, it is calling /php5.fcgi which in turn calls /usr/local/php5-fcgi/bin/php. For testing sake, I set those two files perms to 777. I also set the phpinfo.php5 file to 777 permissions.

Do you have any idea what might be my problem now?

One more question I had was if the cat appended the 4 lines to the end of the httpd.conf? I tried running that from the command line and it output the contents of httpd.conf to the screen and did not write to the file. I manually added those 4 lines at the end of the httpd.conf. I assume the EOF (end of file) applies to the cat command and does not need to be manually appended to the httpd.conf file.

Tell me if I am wrong about this.

Sorry for blowing up the blog with my questions, but I had one thing I found in my apache error_log that seemed to be the problem.

FastCGI: invalid (dynamic) server "/home/devcdg/public_html/php5.fcgi": script not found or unable to stat()

The line in httpd.conf:
Action application/x-httpd-php5 /php5.fcgi
should tell apache to run php5.fcgi from the root of the server, but it looks like it is trying to run the file from the site root rather than the server root.

Any ideas?

Yeah, it looks like my Markdown formatted ate some symbols. I've fixed up that entry.

Also, make sure you're running under a virtual host or with SuExec setup for that user. I don't think it will run without it. And the permissions should be 755, 777 makes no difference. Also make sure you've saved the php5.fcgi file with Unix line endings, because it won't work with DOS ones.

Well I got it to work, but with one kinda big compromise. I made a copy of the php5.fcgi file in the virtual host documentroot. This let the php5 handle as fastcgi fine, but it is an ugly compromise. It must be something funky I am doing in my Apache config.

Thank you so much for the help and this great article.

Well, that's required. That's why I said "All that tells Apache to do is to run every request for a .php5 file through a file named php5.fcgi in the root directory of the site you're on." :)

I followed the instructions exactly, but when I go to a .php5 url i get the error below.

I know that php5 is installed, the output is
PHP 5.1.4 (cgi-fcgi) (built: Jun 19 2006 12:56:40) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies

any ideas?

Bad Request
Your browser sent a request that this server could not understand.

Invalid URI in request GET /phpinfo.php5 HTTP/1.1

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

it turns out I had the php5.fcgi file in the public_html dir, and I needed to move it up one dir... now im getting this error

Forbidden
You don't have permission to access /php5.fcgi/phpinfo.php5 on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

No, the php5.fgi file *should* be in the public_html directory. Make sure you've CHMODed it 755 and that it has UNIX line endings.

I could have sworn that I tried that already but i did it and it worked, thanks alot for the tutorial and for the quick comment response.

is it possible to run php4 and php5 both as fastcgi or does one need to be a module?

You can run them all as FastCGIs. Hell, run PHP3, PHP4, PHP5 and PHP6 (from CVS) all side-by-side if you want :D This is just for those whom already have PHP running as a module and want to add PHP5 alongside it without having to commit to it.

I have one more question for you. How can I get apache to give a 404 header when using php5 fastcgi as described. as of right now if I navigate to a .php5 file that doesnt exist i get:

No input file specified.

Thanks again for all the help!
john

Hmm, that's an interesting one I hadn't encountered before. I'm not really sure how you'd fix that, to be honest.

Does it happen for you too, or is it just my setup?

Thanks for the guide. I tried to use it to get php4 running with FastCGI but run into the following error.

[Wed Jun 28 21:10:00 2006] [warn] FastCGI: (dynamic) server "/var/www/html/metalink.ru/php4.fcgi" has failed to remain running for 30 seconds given 3 attempts, its restart interval has been backed off to 600 seconds

The cgi version of php was built fine:

[root@tux2 logs]# /usr/local/php4-fcgi/bin/php -v
PHP 4.4.2 (cgi-fcgi) (built: Jun 26 2006 19:37:01)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

When I feed the same php script to php4.fcgi through shell it works fine..

Anybody seen this behavior? I already past my 8th hour fighting this and nothing works. Can you help?


Alex

Follow up on the previous issue. Earlier in the log I found

[warn] FastCGI: server "/var/www/html/metalink.ru/php4.fcgi" (pid 29981)
terminated by calling exit with status '107'

then I figured out it's suexec error and was able to fix it by commenting out a line in httpd.conf

#FastCgiWrapper On

Also I had to disable selinux to make everything work.

Alex. Thanks for the comment on commenting out the

FastCgiWrapper On

Commenting this out fixed all of my problems.

Hi, I'm a newbie on linux, and after many hours of work I have completed all the step that you describe here.
But at the end, when I try to open a php5 file I get always this:

Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

I have set LogLevel debug but I can't receive more info.

I really don't know what to try more... do you have any ideas what I can do?

Thanks.

PS. I'm using CentOS with Apache 2.0.52 and PHP 5.2.0

I have resolved... first of all I was not compiling mod_fastcgi-2.4.2 for Apache 2 but for 1.x (see INSTALL.AP2) and for this reason the httpd.conf was not upadate with LoadModule.

I have done another change, but I'm not sure if it is necessary: http://www.fastcgi.com/archives/fastcgi-developers/2002-August/002221.html#cooliris

In the end, using .htaccess I can enable fastcgi only when I want on .php files and not on .php5 so I don't have to rename my scripts.

I hope that this infos can help someone else...

Thank you Tim Dorr for this guide. It was a good start.
Something similar is at this link: http://www.deanspot.org/~alex/php5fcgi/index.html

By, Maurizio

Hi, i am new for linux. can any one solve my issues
i insatll apache 2 by rpm. and now want to config php4 and php5. how i compile php4 for apache that was insatll by rpm .???????

Commenting out the
FastCgiWrapper On
didn't fix my problems, but moving php5.fcgi to cgi-bin directory did!
When it was in site's docroot it complained about not being able to execute it there (of cause!)

Question: If we comment out the above directive (FastCgiWrapper On), are we no longer running this as suexec? So what's the advantage of running it this way over straight forward php executable (compiled as fast cgi), like this:

ScriptAlias /php5-cgi /usr/local/php516-fcgi/bin/php
Action php5-cgi /php5-cgi
AddHandler php5-cgi .php5
<Directory /var/www/portal/php5>
AddHandler php5-cgi .php
</Directory>

thnx.

[Mon Jan 08 18:46:34 2007] [warn] FastCGI: (dynamic) server "/usr/local/php5/php5.fcgi" has failed to remain running for 30 seconds given 3 attempts, its restart interval has been backed off to 600 seconds

anyone help me i face i problem this error log of apache.....

php5.fcgi script is

#!/bin/sh
PHPRC="/usr/local/php5/bin/php.ini"
export PHPRC
PHP_FCGI_CHILDREN=2
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS
exec /usr/local/php5/bin/php

Hi
I followed all the steps in the above tutorial.. but i am getting this in my browser..

"Your browser sent a request that this server could not understand."

Any hint??

Check the path of you php5-cgi-wrapper defined in your http.conf.

Leave a comment

About this Entry

This page contains a single entry by Tim Dorr published on February 17, 2006 11:39 PM.

4 vs 5 was the previous entry in this blog.

Fuddy is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Use OpenDNS
Powered by Movable Type 4.21-en
Creative Commons License
This blog is licensed under a Creative Commons License.