PHP5 and PHP4 Running on SAME apache server

**Update 10/30/04 **
First off, it has been brought to my attention that this DOES NOT work for Linux servers. It has only been successfully tested on FreeBSD servers. In particular a FreeBSD 4.10_STABLE server. I am currently working on a similar solution for Apache on Linux. (Redhat9 in particular)

I think I have finally found a way to do what I need to do with parrallel versions. My situation is my company’s hosting farm doesn’t give us the ability to run two seperate apache servers on one box and I don’t particularly want to run a CGI version due to the loss of $_SERVER variables and other security issues. So I endeavored on a trick to get php5 to use a different mime-type. Well, it’s not a trick, but it’s also probably not obvious to most people how to accomplish this so read on.

The changes I made where to get it to work for Apache 1.3.*. I am not sure if the same changes (different directory) can be applied for it to work with Apache 2.In any case, the file I edited was /sapi/apache/mod_php5.c. Around line 913
change the following values:

  • application/x-httpd-php -> application/x-httpd-php5
  • application/x-httpd-php-source -> application/x-httpd-php5-source

The changes will be your new mimetypes for use with the AddType directive in PHP. (eg: AddType application/x-httpd-php5 .php)

Then towards line 924 change these values:

  • php_value -> php5_value
  • php_flag -> php5_flag
  • php_admin_value -> php5_admin_value
  • php_admin_flag -> php5_admin_flag

The changes here should be used if you want to set php.ini options via your httpd.conf or .htaccess files. (eg: php5_value include_dir "/usr/local/lib/php")

Now, I haven’t found any warning that this won’t work, but I also haven’t found any references to other people doing this. So I would proceed with caution. I am the senior developer for a fairly large web development firm and we currently have versions 4 and 5 co-existing on one of our productions servers. Over the course of almost a month we haven’t had any problems. But that doesn’t mean that problems don’t exist. I am hoping to start a discussion about any possible drawbacks of this method. Let me know your thoughts and whether or not this works for you.

26 thoughts on “PHP5 and PHP4 Running on SAME apache server”

  1. I’ve done this a little differently, but not so much that I would expect it to not work if it works for you

    I already have php5 installed so I get the source to php4.3.9, modified the sapi/apache/mod_php4.c file, exactly as you have above, substituting ‘4’ for ‘5’ (obviously), and compiled. I have the prefix and ini location set to different dirs for each php install, so i don’t have to worry about overlap. Everything went perfectly fine.

    I have the following lines in apache (1.3.x) httpd.conf (in the appropriate places):

    LoadModule php5 libexec/
    LoadModule php4 libexec/

    AddModule mod_php5.c
    AddModule mod_php4.c

    # php5
    AddType application/x-httpd-php .php
    # php4
    AddType application/x-httpd-php4 .php4

    I then stopped/started apache, but apache never actually starts. If the php4 lines, php5 works…if i comment the php5 lines, php4 works, but not both.

    Wondering if you have any comments or insight, since I don’t really see this as being any different than what you have done, and your modification works.


  2. Are you able to pull any useful info from console or your apache error log when you start it up?

    The only odd thing I came accross resulted from forgetting to change the php_value and php_flag strings in mod_php5.c. Apache would start and php5 would load, however all of my .htaccess files on the server no longer allow me to set php options on my php4 sites. Changing the aforementioned strings pretty much fixed that.

    As long as you made sure to set all of the strings I mentioned you are right, it’s not different from what I did. I do recall however the way that they hook into apache in php4 is pretty different from what they do now in php5. So maybe that has an effect?

    In any case, post those errors if you have them and maybe that’ll tell us something that sheds some light on this.

  3. no, i changed php_value, etc as well, even tho I’m not using them.

    i even changed mod_php4.c back, re configured/compiled/installed and then did YOUR modifications to mod_php5.c, reconfigured/compiled/installed, modified httpd.conf:

    # php5
    AddType application/x-httpd-php5 .php
    # php4
    AddType application/x-httpd-php .php4

    so that it was just like yours, but exact same problem. apache SEEMS to start by hangs…nothing in the error_log whatsoever. any attempts to connect result in a refused connection just as if apache was not running. commenting out either allows the other to work.


  4. I have just spent several hours trying to get this to work but am getting exactly the same problem as Chris — either one or the other works, but not both. No errors in the error_log — apache just doesn’t start.

    I tried commenting out the AddModule lines, but that keeps the modules from loading alltogether.

    I am running RH9 (2.4.26 kernel).

  5. maybe I am just the luckiest guy on the face of the earth :/

    I am actually doing this on FreeBSD. So there may be a slight difference in how FreeBSD loads modules in apache.

    Are you using an rpm version of apache? or did you compile it yourself? I am running a compiled version and the only flags I added were a –prefix flag and –enable-module=so.

    The more and more I read about this the more I think there has to be something goofy going on with Linux that doesn’t happen on FreeBSD, or vice-versa. One other thing you can try is the –enable-versioning flag in PHP when you configure it. It’s a stab in the dark, but maybe you’ll get lucky.

    I am going to see if I can score myself some redhat 9 ISOs and come up with something that works there.

    Until then, you can either subscribe to this entry or just check back for an answer.

  6. There’re more files that must be changed. Do a scan in your php5 folder with “grep -R x-httpd *” and change all application/x-httpd-php to application/x-httpd-php5.

  7. I’m read top instruct, and replay all step. System FreeBSD 5.1 Apache 1.3.33

    1. modify mod_php5.c

    2.Write to httpd.conf

    LoadModule php4_module libexec/apache/
    LoadModule php5_module libexec/apache/
    AddModule mod_php4.c
    AddModule mod_php5.c

    AddType application/x-httpd-php5 .php5
    AddType application/x-httpd-php .php

    Apache not run php4 + php5 mod

    Air top and write log …

    messages: kernel: pid 78877 (httpd), uid 0: exited on signal 6 (core dumped)
    httpd-error: httpd in free(): error: chunk is already free

    Apache + one module php4 or php5 is worked.

    PS: Hmmm… Apache core dump where addModule … LoadModule all ok.

    Sorry bad English =)

  8. Hmmmm … variant not worked.
    I’m build php5 as cgi.

    ./configure –enable-force-cgi-redirect

    add httpd.conf

    Action php-script /cgi-bin/php
    AddHandler php-script .php5

    All Worked =)

  9. Folks,

    Anymore news on this running on Linux, we are looking for something to work with Apache 1.3.x and Redhat 9.

    It does seem there is a different between Free BSD and Linux. I see the same problem, no real errors logged, but it does not work.


  10. So, da ein Freund von mir mich gefragt hat, wie man sowohl PHP4 als auch PHP5 benützen kann und durch eine Config jeweils sagen was gebraucht werden soll.

    Darum nachfolgend eine Linkliste 😉

    Die Grundidee besteht darin, PHP4 oder PHP5 als CGI und…

  11. i’have the same problem !!
    apache don’t want to work with the both modules :(

    I’ve an idea of the problem, so can u send the directive of the apache compilation !

    Thanks for all

  12. I think, I have some dirty solution to the problem with apache and two modules!!
    I’ve made it on x86 FreeBSD, Apache 1.3 (I think there will be no difference on Linux).
    The problem is that and share many common symbols. And when second is loaded, linker sets his symbols to the addresses of first module, I think. That is beacause apache call dlopen() with RTLD_GLOBAL flag and so symbols of modules are exported and visible by other modules. So I changed this flag in call call to dlopen() in “src/os/unix/os.c” to RTLD_LOCAL. After that php modules can be loaded together.
    But since their symbols not exported – they can not load their share modules. So You should compile all needed php modules statically. I have one more problem – when php modules was compiled with openssl support then apache core dumped on mod_ssl module. I turned openssl support off in php and now it works!
    PS. May be I made mistakes in some reasoning, because I’m not so expirenced with dynamic linking. If someone have fixes or more acceptable solutions – they will be highly appreciated.

  13. I came up with a similar conclusion. I have found however that you can use shared modules in which php_mod you load into apache 1st.

    I was able to get openSSL compiled. It was a pain in the butt to do it though. It is keenly important that you use the same openssl library when compiling the two php_mods.

    I’ll see if I can track down the configure string and the ssl library I am using.

  14. Did You tried to compile mod_ssl statically into Apache? I think this may work, but I have not tried it yet, because of no need in it. May be later I’ll try, but now I’m too bored arter a day f###ing with it. :)

  15. I did the same for Apache2 on Linux. The file to alter is srclib/apr/dso/unix/dso.c, line 126.
    It works for now and I’m going to test it heavily.

    To make it look “nicer” I extracted the STANDARD20_MODULE_STUFF-definition from php-4.3.X/sapi/apache2handler/sapi_apache2.c to a separate file mod_php4.c. This way the Apache module is internally called mod_php4.c instead of sapi_apache2.c .

    By the way, I’m using OpenSSL 0.9.7e and it seems to work for all three of them.

    And that’s what I got:
    Apache/2.0.54 (Unix) mod_ssl/2.0.54 OpenSSL/0.9.7e PHP/5.0.4 PHP/4.3.11 Server at localhost

  16. Have you checked to see if you can run shared modules on both versions of php? or are you also stuck with static, compiled-in modules too?

    I haven’t really worked with getting both up in apache 2 soley because we don’t run apache 2 in any production environments. I do know that their modules system was changed pretty extensively though.

  17. Yep, static-only.

    I tested and compiled in modules work, extensions complain because of undefined symbols.
    Both PHPs are missing _zend_list_delete for and _emalloc for .

    nm -o -D libphp?.so |grep _zend_list_delete T _zend_list_delete T _zend_list_delete

    The same for _emalloc, so everything seems to be in place. At the moment I don’t know how to proceed. Ideas anyone?

  18. My Apache 2.2.0 runs fine after patching…

    Apache/2.2.0 (Unix) SMHost/1.23-(2005-12-17-09:39:58) PHP/5.1.2 PHP/4.4.2 mod_ssl/2.2.0 OpenSSL/0.9.7e mod_WebObjects/0.9 mod_perl/2.0.2 Perl/v5.8.6

    although now I have the problem of needing to link in eAccelerator (and a couple of other 3rd party php extensions) statically into each of the apache php modules, any ideas how to patch it into the php build/configure maybe?

  19. man, could you please explain how you did it, i have days trying to compile php4 and php5 as sapi modules for apache2 with no luck, i get always segmentation fault errors.

    I really want this to be done.

    the server where im doing my tests, is a CentOS 4.4 based, but where it will run is Red Hat Enterprise Linux ES 3 (Taroon Update 8) runing Plesk control panel (8.0.1).

    I want this done so i dont have to remove php4 support, to migrate to php5, and for now too many clients uses php4 for their sites, and we dont want to take the risk of breaking their apps because the migration, but also, if we manage to keep this going, we could comfortably offer support for legacy php4 code, but also to php5 coding standards and get the benefit from both versions.

    Thanks a lot,


  20. Take a look at gentoo’s ebuilds for php. There is “concurrentmodphp” flag for this and patches to use concurrent versions of php modules.

  21. You can look at getoo’s build scripts for php here:
    And then try to compile php like there. Patches, referenced there can be downloaded from gentoo distfiles mirror.

  22. fbsd# /usr/local/demo/apache-2.2.3/bin/apachectl -v
    Server version: Apache/2.2.3
    Server built: Dec 5 2006 09:48:30
    fbsd# uname -sr
    FreeBSD 6.1-RELEASE-p5
    httpd.conf has AddType application/x-httpd-php5 .php
    Have the loadmodule statement for the module.
    However, php won’t work. When I modify like the mod_php5.c file with your corrections, it won’t render the php. The module shows that it’s loaded, but it won’t execute the php. Anyone know why this might be?

    From error_log on the .htaccess file. I shouldn’t really even need it because I’ve disabled php4 which works fine. The only thing I can think of is it didn’t rename the functions. It’s basically a base install but with modified function application type names.
    [Tue Dec 05 12:44:12 2006] [alert] [client] /home/mark/public_html/.htaccess: Invalid command ‘php5’, perhaps misspelled or defined by a module not included in the server configuration

Leave a Reply

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