PHP Crashes Caused By Extension Ordering: A Workaround
As I posted about nearly a year ago, I was (and still am) seeing Apache crashes caused by PHP extension ordering issues. So far, there has been no official or even unofficial workaround for the problem. I wrote a small shell script (/bin/sh for better portability) that will reorder the extensions in php.ini into the order that seems to cause the least problems for me.
Suggestions and improvements are more than welcome. I submitted this script to the PHP port maintainer for FreeBSD but have not yet heard back, which could be due to the hackishness of my script…
Anyhow, I’m pleased to announce that It Works For Me(TM) and you’re welcome to try it: fixphpextorder.sh
You may have to edit the file to correct pathnames and such, but if you build PHP from FreeBSD’s ports system, it should work. It’s especially nice when used with portupgrade like so:
portupgrade -A /root/bin/fixphpextorder.sh php5-\*
That will cause portupgrade to execute that script after each module is rebuilt. This will help if you have any cron or CLI PHP scripts that would reload modules while the upgrade is happening. I tried this method on several servers and it worked well. The only problem was a server running Cacti that polls every 5 minutes. I had two crashes while the upgrade was going on, but that is far better than the dozens it was getting when doing this by hand.
Update 11/21/07: I updated the script to also put pspell.so at the end of the file. It needs to be loaded after spl.so or PHP may crash Apache when a process terminates — either with a full shutdown or when an extra forked process is killed.
Update 6/25/08: Script updated to ensure spl.so comes before mysqli.so, which caused problems with PHP when used at the command line (CLI). Reported by Octaviao Ionescu.
Update 2/22/09: I updated the script again. I found that now sqlite.so must come after spl.so, or it complains about missing symbols. I also moved xml to the end hoping to fix another issue, but it did not help. It didn’t hurt, either, so I left the change in. Let me know if there are problems.



This script worked great! I found your original post about this dumb problem some time ago, and I had to reference it again today, and happened to see the script and tried it out. Worked like a charm!
cheers
Comment by Charlie — 11/26/2007 @ 5:54 pm
This script worked great!
Thank you.
Comment by Alexey Vdovin — 3/21/2008 @ 10:33 am
i got the same issue on my freebsd 7.0 , i got your fix script and now it`s running without problems, thank you!!
Comment by Esteban Galvan — 6/5/2008 @ 11:45 am
hi!
i have had a problem on freebsd 7.0 with php 5.2.5 and 5.2.6 regarding mysqli extension. for apache module everything worked fine but for cli version it crashed with error regarding mysqli.so extension.
after a little dig on the net i have found that mysqli depends on spl.so extension wich shall be loaded before the mysqli extension :
extension=spl.so
extension=mysqli.so
after i have made it everything worked fine. if it helps for this script i’m glad if i was of any help
Comment by Octaviao Ionescu — 6/25/2008 @ 8:12 am
You saved my life! (and my sanity)
thanks.
Comment by patpro — 9/5/2008 @ 3:09 am
I’ve had the same problem that mysqli, but with sqlite.
spl seems to have to come before.
Comment by Geoffroy Desvernay — 9/10/2008 @ 11:03 pm
I’ve added line:
MV=/bin/mv
and changed last line (MV instead CP):
$MV $EXTENSIONS_INI.tmp $EXTENSIONS_INI
The .tmp file disappeared.
Comment by Valeri Matyukhin — 9/18/2008 @ 6:01 pm
Thank you so much for figuring this out!
Comment by Fred — 9/24/2008 @ 5:43 pm
Jim, I just tried your script on a couple of machines and it is FANTASTIC, fixed both.
This should definitely be integrated to ports for all php extensions.
Thanks for your work, I was never able to figure out why the order was so important.
ed
Comment by Edwin Culp — 11/15/2008 @ 12:05 pm
Nice script, i have sqlite on my server and sqlite.so must be placed after spl.so.
Thanks.
Comment by Tamas Horvath — 11/29/2008 @ 3:44 am
I have tried this script on my test server … but it didnt help :(. I still got apache crash on apachectl restart. But not on every restart :))
freebsd 7.0-stable (jul’08 world rebuilded); php 5.2.8; apache 2.0.63
Comment by georgi iovchev — 12/10/2008 @ 5:30 am
i have the same os and software
problem of crash apache after apachectl restart gone after i write extension.ini in this order
extension=gettext.so
extension=iconv.so
extension=ldap.so
extension=mysql.so
extension=pcre.so
extension=session.so
extension=xml.so
extension=dom.so
extension=pgsql.so
extension=gd.so
extension=zlib.so
extension=curl.so
maybe its help, sorry for bad english
Comment by Dmitriy Redchyts — 1/16/2009 @ 8:42 am
Thanks for your helpful post! Although the script didn’t eliminate the error messages, it gave me some ideas. I solved it by removing some modules.
Comment by KP — 1/20/2009 @ 4:46 pm
my original extensions.ini:
extension=pgsql.so
extension=pdf.so
extension=ctype.so
extension=simplexml.so
extension=spl.so
extension=dom.so
extension=filter.so
extension=hash.so
extension=iconv.so
extension=json.so
extension=pdo.so
extension=pdo_sqlite.so
extension=posix.so
extension=session.so
extension=sqlite.so
extension=tokenizer.so
extension=xml.so
extension=xmlreader.so
extension=xmlwriter.so
extension=zip.so
extension=zlib.so
after running fixphpextorder.sh:
extension=pgsql.so
extension=pdf.so
extension=ctype.so
extension=simplexml.so
extension=dom.so
extension=filter.so
extension=hash.so
extension=iconv.so
extension=json.so
extension=pdo.so
extension=pdo_sqlite.so
extension=posix.so
extension=session.so
extension=tokenizer.so
extension=xmlreader.so
extension=xmlwriter.so
extension=zip.so
extension=zlib.so
extension=spl.so
extension=sqlite.so
extension=xml.so
issuing “/usr/local/etc/rc.d/apache2 graceful” gives “Performing a graceful restart”
but “/usr/local/etc/rc.d/apache2 status” says “apache2 is not running.”
httpd-error.log shows:
[notice] Graceful restart requested, doing restart
[notice] seg fault or similar nasty error detected in the parent process
[warn] pid file /var/run/httpd.pid overwritten — Unclean shutdown of previous Apache run?
[notice] Apache/2.0.63 (FreeBSD) PHP/5.2.8 with Suhosin-Patch configured — resuming normal operations
the output of the commands above is the same when i was using my original extensions.ini..
FreeBSD 7.1-STABLE
please help. TIA!
Comment by yayix — 3/30/2009 @ 11:25 pm
If you just recently updated PHP on FreeBSD with the ports system, be sure to read /usr/ports/UPDATING, specifically the 20081211 entry. It is possible you have some extensions that are out of date with respect to the version of PHP you are running.
I saw some errors until I followed the instructions in that entry, forcing an update of pcre (I actually had to rm /usr/local/lib/php/20060613/pcre.so before reinstalling).
There are, unfortuantely, more ways to crash PHP than by extension ordering alone… :)
Comment by jim — 3/30/2009 @ 11:30 pm
I am still trying to solve this problem on one of my servers.
I have tried using the extension reordering script and httpd still core dumps. The work around that I found, although quite unsatisfactory operationally, nevertheless works and it would be useful to find out why.
I found that if I restart the running httpd using apachectl graceful httpd coredumps. However if I comment out the LoadModule php5_module and the AddModule mod_php5.c lines, httpd starts and I can then copy back the uncommented version of httpd.conf and do a graceful restart successfully.
Another graceful restart then crashes httpd.
I am running apache 1.3.41 and php5.2.9 on freebsd 7.0
Any ideas?
Comment by Barry Friedman — 5/14/2009 @ 2:35 pm
Thanks a zillion.
The re-ordering is my problem as well!
Comment by Mathos — 5/17/2009 @ 7:37 pm