Fixing random PHP crashes on IIS 6 recorded as PHP memory leaks
September 30th, 2007
I recently came accross a serious problem that seems to be relativly un-documented bar other people raising bug tickets on bugs.php.net.
If you are having problems with PHP on IIS where you get a couple page views from your application then you just get a blank screen? If so then read on….
There was nothing in the Event Log for applicatons or system, so I tried re-starting IIS; with no luck. This particular problem seemed to be happening tottaly silently with absolutly no record anywhere (php_error.log et al). I them re-started the whole box, and upon boot up I checked the Event viewer and there was an entry in the application log detailing a memory leak in “php.exe”. So, a memory leak I thought, great…
Trawling the web I then found a document detailing how windows server kills off processes it deems to be at all ‘dangerous’. It does this silently (which is unbeliviably handy) and fitted with the problems I was encountering. Going on the presumption that it was indeed killing the PHP process (or more specifically the worker threads) I turned to the application pool configuration… And broke out alll my PHP apps into a seperate application pool. When they stoped working, I could just recycle the pool and they worked again for two requests. NB: Ive seen other peoples configurations take a couple more than 2 requests so bear that in mind if your having this problem.
So, by now I knew that it was windows killing of the process causing the problems, however I wasnst sure how to fix it.
I looked at the loaded modules in my PHP.ini, and found that removing all non-essential modules solved the problem. I then brought in one by one each PHP module, and found that it was not actually the modules them selves that were leaking, but just the order in which they were loaded causing the problem.
Yes, thats very crap. I couldnt find a more elegant solution to this, and I really hope this helps someone as I must have wasted a day of my life trying to solve this bloody problem! lol
Installing Apache 2.2 on OSX with mod_python
September 23rd, 2007
Following on from setting up Django on your local system for dev purposes, I thought id just do an article about how to set up apache to use mod_python for when you want to roll your Django app out to the big bad world.
Before you start, you will NEED to have completed the other post about installing Django, as you need to have the mac python installer in and configured properly for this to work.
Step 1
First of all we’ve got to get Apache 2.2 working, as Apple only ship 1.3 with OSX (I wonder if this will change in Lepord? Lets hope so!). You can download the source here.
Extract the archive and cd into the new directory. We need to configure it with some options so that we have proxying (which you might want for rails etc) and just some other candy you might find usefull at a later date.
Set the CFLAGS:
export CFLAGS=”-arch i386″
Run this configure command:
./configure \
--prefix=/Library/Apache-2.2.6 \
-–enable-so \
--enable-mods-shared=all \
--with-mpm=prefork \
--enable-dav \
--enable-cache \
--enable-proxy \
--enable-shared \
--disable-static \
--disable-unique-id \
--disableipv6 \
--enable-logio \
--enable-deflate \
--with-ldap \
--with-ldap-include=/usr/include \
--with-ldap-lib=/usr/lib
--with-included-apr
--enable-ldap \
--enable-auth-ldap \
--enable-cgi
--enable-cgid \
--enable-suexec
Let it do its business then run the usual:
make
then
make install (NB: we dont use sudo here as that way apache is compiled with permisions that let you edit the conf files and so on without needing to be an admin which is just ‘nicer’ for local dev)
Step 2
Configure your httpd.conf file as you see fit. I only really felt the need to change the following:
Change user and group to your user name (or any other one you want) Change ServerAdmin email to an appropriate one Uncomment “Include conf/extra/httpd-mpm.conf” Uncomment “Include conf/extra/httpd-default.conf” Add “NameVirtualHost *” for virtual hosting
My personal preference is then to have an “applications” folder in the apache conf dir, for which I then add a directive to httpd.conf to load in per-application virtual hosting configurations. It just keeps it nice and clean that way.
mkdir /Library/Apache-2.2.6/conf/applications
Include conf/applications in httpd.conf (in your httpd.conf)
Step 3
If you havent already, grab mod_python from here. Extract, it and cd into the directory.
You’ll then need to configure it with the following command:
./configure -–with-apxs=/Library/Apache-2.2.6/bin/apxs \
-–with-python=/Library/Frameworks/Python.framework/Versions/Current/bin/python
Then do make and sudo make install
Provided you got no errors upon compilation, all should be well. You just need to add another line to your httpd conf:
LoadModule python_module modules/mod_python.so
Step 4
Poor yourself a nice cuppa!
Running Django on OSX
September 22nd, 2007
No doubt you’ve heard about the new python web framework thats making some waves at the moment - Django.
As with most *nix developers, im running OSX (10.4), and wanted to get a working Django dev environment function to have a play around with it and found that a needed to mess around with a few things to get it working, so I thought id post them up here in case anyone else found them usefull :)
Step 1
Download the python-mac installer from here and run it. That will give you a working version of the latest python bin.
Step 2
Checkout the latest version of Django source like so
cd /usr/local
svn co http://code.djangoproject.com/svn/django/trunk/ django
Step 3
If you used the python installer for mac, then the below will work, otherwise, you’ll need to just tweak the path’s a bit:
ln -s /usr/local/django/django /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django
Step 4
Add the django source to your path:
PATH=/usr/local/django/django/bin:$PATH
Then that should be it :) However, if your a touch on the forgetfull side when using the terminal, it can be annoying having to keep putting the .py on the end of the django commands, so we’ll just quickly symlink them so if we forget to add .py, it wont slap us with a kipper
cd /usr/local/django/django
ln -s django-admin.py django-admin
Then your all done! Enjoy Django :)
Very random video...
September 22nd, 2007
Running Pound reverse proxy on windows server 2003
September 22nd, 2007
Pound is the unix reverse proxy software popularized by Rails deployment over the past 8-12 months. Pound is a unix tool, and was never meant to run under windows - it is however possible to make it run under windows if you package it up with some key DLL files from cygwin :)
I must stress here that this is not necessarily the best thing to be doing, this post is more about how to do it if you really want too rather than a "do this, I really recommend it" type post. There are also some issues with pound and its behavior with X-FORWARDED_FOR headers, but i wont bang on about that here; refer to google for more info. Anyway... back to the post.....
Take the latest pound distro from here then compile it from within Cygwin like so
./configure --without-ssl --disable-log --disable-dynscale
then
make all
Then you should be left with a working binary of pound within your build directory. In order to then make this work in a standalone fashion (I use the term standalone loosely as its still using the cygwin environment) you need to package several DLL files from cygwin:
cygssl-0.9.8.dll (required)
cygcrypto-0.9.8.dll (required)
cygwin1.dll (required)
pound.exe (required, should be fairly obvious!)
cygrunsrv.exe (optional, include only if you want to install pound as a service)
with these files you can then run pound, and even install it as a service on the host machine. If you do want to install the service, run the following command:
cygrunsrv --install Pound --path X:\applications\pound-2.3.2\bin\pound.exe --args "-f X:\applications\pound-2.3.2\config\pound.cfg" --stdout X:\applications\pound-2.3.2\log\pound.log --stderr X:\applications\pound-2.3.2\log\pound.log
Where "X:\applications\pound-2.3.2" is the real path to where ever it is on the file system you chose to install.
If you don't want to compile your own, then you can download my version of pound 2.3.2 for windows here. It goes without saying I take no responsibility for any problems you might run into by using my compiled version; it worked for me thats all I can say!
Making IIS play nice with Apache on windows server 2003
September 21st, 2007
Now, being a unix man through and through, I do see the irony that my first blog post is about winows; such is life!
Anyway…
Typically IIS and Apache do not work well together at all, as IIS hogs all IPs (both physical and ARP) on any given win 2003 machine which is why when trying to run anything else on port 80 on any IP can be a total nightmare.
I recently had to do an install on a win 2003 box where the client wanted to support some of their legacy ASP applications, PHP, but then allowing scope in the future to run Rails, Seam and Django applications. This means running:
- Apache with mod_python (for Django)
- Apache proxying to mongrel (for Rails)
- JBoss for Seam (EJB configuration)
- IIS for ASP/ASP.NET
IIS has no real ability to proxy to other backends (which in this instance we need due to the rails requirment) in the way Apache 2.2 does, so I chose to run Apache out front binding to one of my avalible 5 on this machine. Apache then uses proxy pass (or proxy balencer for rails) to pass PHP and ASP requests to IIS. No doubt there are people out there who would have chosen to run PHP on Apache, but due to the way the legacy applications were written, PHP and IIS were already installed so it made sense to leave it “as is” - no need to make more work for myself, especially as i only had one night to do the install!
Step 1
IIS however requires a bit of wrangling before we can get on with the install of Apache…Make sure you have your win 2003 install discs with you; as you’ll need to install the support tools (found in support > tools on the cd). It is however possible to do this without the support tools, but it involves hacking around in the registry. Once you have the support tools installed, were going to be making some changes to let IIS know what exact IP address it can bind too. If you have the support tools installed you can the use the httpcfg tool like so (note: if using RDP you might need to logout then login to get support tools on your PATH)
httpcfg set iplisten -i 555.555.555.555 (where this is your IP address)
If you do not have the support tools (or your just too lazy to dig out that disc from the depths of the server room) you can edit the following registry key:
HKEYLOCALMACHINE/SYSTEM/CurrentControlSet/Services/HTTP/Paramaters in there is a paramater called ListenOnlyList from which you can edit with the list of IP’s IIS should listen to.
Once youve done that, restart IIS as usual from the MMC.
Then you can do a netstat -ano to see that IIS only binds to the IP you set. Simple.
Step 2
Next up you need to download apache. I went with apache 2.2.4 without SSL, which i grabbed from here
Run the installer, and make sure you choose on port 80 as a server for all users when installing.
Once apache is installed, you’ll need to set up port listening and IP address binding. This is a well discussed topic in the apache manuals so I wont labour the point here. See the apache docs.
I then set up some virtual hosting to hand my requests off to IIS. Granted, I could have done this with rewrite rules for .asp extensions, but I chose to proxy it on domains, due to my particular circumstances. Heres what my virtual host looks like:
<VirtualHost *:80>
ServerName www.domain.co.uk
ServerAlias domain.co.uk
ProxyPreserveHost On
ProxyPass / http://172.16.1.11:3000/
</VirtualHost>
(Where my site definition within IIS was running on port 3000)
Step 3
I then downloaded Python and installed it. And then I followed the very good instructions on setting up mod_python on google groups
Step 4
Do the usual rails install bits and bobs - this is so well document I wont pollute the blogsphere any more :)
Step 5
Java time…. Download and run the JEMS installer. At this point we only want to install JBoss on the box with an EJB configuration; it will take a couple more posts to go into the configuration required to get JBoss Seam up and running so we shant discuss that here.
Summary
Thats pretty much it. Its not too difficult, you can proxy off to any service you want to (within reason) and you can have all the latest and greatest web serving technologys happily co-exsisting!