Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 68 votes, 5.00 average. Display Modes
Old 10-12-2010, 03:41 AM   #1
OSXdude
Triple-A Player
 
Join Date: Jul 2006
Posts: 96
Exclamation How To Watch And Auto-Restart Crashing PHP

Hello Everyone,

I am running a WebSTAR server on an old Macintosh G4 500 MHz DP mini-tower running Tiger 10.4.11. One of the websites that I operate requires using PHP and a mySQL database.

While WebSTAR has its own PHP binary in its fcgi-bin, recently, I stopped using it, because it seems to crash a lot, and it is at least five years old, I believe.

While Tiger also has PHP 4.4.9 installed in its default location, just recently I installed a self-contained version of PHP 5.2.0 in:

/Applications/PHP5/bin/

This is a self-contained PHP package that I downloaded from the Zend website.

To my frustration, even this newer version of PHP -- which is really almost four years old now -- crashes several times throughout the day. I don't know if it is a result of so many search bots hitting the site, or people who are maliciously attacking the site, or what, but the constant PHP crashing is annoying, and makes the site unreliable.

Currently, the way I have dealt with the PHP crashing problem is by keeping "Activity Monitor" open on my desktop, as well as the Terminal. Whenever I see PHP disappear from "Activity Monitor", I type the following in the Terminal to restart PHP:

set env PHP_FCGI_CHILDREN 8
./fcgirunner.pl xxxx ./php

The x's are the port number.

"fcgirunner.pl" is an old PERL script released by Zeus Technology to startup remote FastCGI processes with their Zeus Web Server.

As you can imagine, it is a hassle to have to keep an eye on PHP, and reboot it manually every time that it crashes. With that in mind, I am interested in finding a way to automatically watch the PHP binary, and to automatically relaunch the PHP binary in fcgi mode, without having to keep "Activity Monitor" and the Terminal open all the time.

Whatever solution anyone here can offer, must be able to do two things:

1. Keep an eye on the self-contained PHP installation.

2. Automatically reboot the PHP binary in fcgi mode every time that it crashes. Again, the path to the PHP binary is:

/Applications/PHP5/bin/

I appreciate any solutions that anyone here may be able to offer.

Please bear in mind that I am not very proficient at using the Terminal, and have very little experience at writing Applescripts, or any other scripts. So, if you offer that kind of a solution, you will need to actually provide me with the script.

Thanks so much in advance.
__________________
27" 2.8 GHz Quad Core i7 iMac with 8 GB of RAM and a 2 TB Hitachi Hard Drive
Zippin' along, baby, zippin' along! Catch us if you can! -- Dave Clark Five 1965
OSXdude is offline   Reply With Quote
Old 10-12-2010, 07:49 AM   #2
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,329
Trash all the above and visit http://www.entropy.ch/software/macosx/php/ . Marc's package has a Mac-specific installer and works just fine.

Make sure you get the back version for Tiger and not the latest-greatest edition.

The MySQL installer on the mysql.org website also works out of the box.
acme.mail.order is offline   Reply With Quote
Old 10-12-2010, 08:21 AM   #3
OSXdude
Triple-A Player
 
Join Date: Jul 2006
Posts: 96
Hello. Thanks for the tip. Actually, I beat you to the punch. Yesterday, while looking for a solution to this problem, I actually went to his site and downloaded the "Entropy PHP 5.3.0-3.pkg" package.

It sounds, however, like you are saying that is the wrong one for my old Tiger machine. So, I just now downloaded "entropy-php-5.2.4-1.tar.gz".

I do have some concerns, which is why I didn't install the 5.3.0-3 package yesterday. My web server -- and PHP script -- is in full operation right now, and I didn't want to take the chance of seriously messing up something.

Also, there is this on Mark's page, right above the Tiger download link:

"Here’s an older, unsupported and probably insecure version for 10.4:"

Another question or two:

Have you personally ever used Mark's PHP installations?

How reliable are they insofar as PHP crashing is concerned?

Will his installation launch PHP in fcgi mode, or just regular cgi mode? I need fcgi.

I am currently using MySQL 5.0.45 Tiger PPC, which I probably got from the mySQL website. I can't remember now.

Anyway, I am going to go ahead and install it, and see how it goes.

Oh, another important question:

Will PHP automatically boot and run as a process each time that I start up the G4, or will I have to do something, as I do now in the Terminal with fcgirunner.pl?

Thanks!
__________________
27" 2.8 GHz Quad Core i7 iMac with 8 GB of RAM and a 2 TB Hitachi Hard Drive
Zippin' along, baby, zippin' along! Catch us if you can! -- Dave Clark Five 1965
OSXdude is offline   Reply With Quote
Old 10-12-2010, 08:40 AM   #4
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,329
Quote:
Originally Posted by OSXdude
Have you personally ever used Mark's PHP installations?

Yes. I have his install running on my own server.

Quote:
How reliable are they insofar as PHP crashing is concerned?

Never had a problem, and PHP doesn't 'crash' in the usual sense as it doesn't run as a background process.

Quote:
Will his installation launch PHP in fcgi mode, or just regular cgi mode? I need fcgi.

No idea.

Quote:
Will PHP automatically boot and run as a process each time that I start up the G4, or will I have to do something, as I do now in the Terminal with fcgirunner.pl?

It runs when Apache needs it to run, then it disappears. Apache / httpd is a daemon process, php is a regular application
acme.mail.order is offline   Reply With Quote
Old 10-12-2010, 09:51 AM   #5
OSXdude
Triple-A Player
 
Join Date: Jul 2006
Posts: 96
Well ...ahem...that was certainly a bad experience.

I installed Mark's Tiger PHP installation.

After rebooting my Mac, I opened Activity Monitor. PHP wasn't running...which answered one of my questions.

So, I opened the Terminal and manually navigated to the location of Mark's install: cd /usr/local/PHP5/bin

I manually started PHP with: ./php

Great! It started, and appeared in Activity Monitor's window.

Next, I launched WebSTAR, and proceeded to go to my PHP/mySQL-based website in Firefox.

Oh gosh...darn...just a blank white page...meaning that Webstar couldn't locate/identify/recognize, or whatever, the new PHP installation.

I even tried using the same startup string -- and the fcgirunner.pl PERL script -- that I use with the other PHP package that I have in my Applications folder. Still no dice. I was still getting a white, blank page.

Finally, I thought maybe I could just replace my previous PHP binary in the Applications folder, with Mark's binary, since it was newer.

Still no dice. I could get it to start up, but within seconds it disappeared from Activity Monitor's window...crash!

In short, I am back to using my previous setup for now.

I think one important point that you probably missed in my original post, is that I am NOT using Apache. I am using WebSTAR, which is a self-contained http/ftp/mail server combo suite...which is no longer developed or supported. 4D sold it to Kerio, who promptly killed it in order to promote their own Kerio Mail server.

Mark's install is made to work with Apache, and obviously won't work with WebSTAR.

Oh well...nice try.

Have you ever heard of supervisord? I installed it yesterday, hoping to use it to watch and reboot PHP when it crashes. I got as far as installing it, but am stuck on the part of moving and configuring the config file. I wrote to the author yesterday, but he hasn't responded yet.
__________________
27" 2.8 GHz Quad Core i7 iMac with 8 GB of RAM and a 2 TB Hitachi Hard Drive
Zippin' along, baby, zippin' along! Catch us if you can! -- Dave Clark Five 1965
OSXdude is offline   Reply With Quote
Old 10-12-2010, 01:55 PM   #6
J_T
Triple-A Player
 
Join Date: Jul 2007
Posts: 96
This is a bit of a hack but it has gotten me through with unstable processes. make a shell script and load it into cron for a minute by minute check.

pidcheck.sh

Code:
ofPid=`/bin/ps -ax | grep php | grep -v pidcheck.sh | grep -v grep`

if [ -n "$ofPid" ] 
then
      exit 0;	
     
else
	
	/path/to/fcgirunning.pl
fi
exit 0;
its not pretty but can buy you some time. Load it into your crontab and have it check every minute. You will have to change the "php" to whatever process you are looking for.
J_T is offline   Reply With Quote
Old 10-12-2010, 04:01 PM   #7
Hal Itosis
Hall of Famer
 
Join Date: Apr 2002
Posts: 3,315

tl;dr

To answer just the title literally (How To Watch And Auto-Restart Crashing PHP), that sounds like a job for launchd (with the KeepAlive key enabled). However, if the crashes are too close together (every 11 seconds or something?), then launchd may get irritated... and just let it stay crashed. [the real solution is to fix the cause of the crash.]
Hal Itosis is offline   Reply With Quote
Old 10-12-2010, 06:35 PM   #8
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,329
You are all missing a major point - PHP does not run as a background process.

Starting php from the command line with ./php will make it run in command-line mode, patiently waiting for keyboard input.

The Entropy package installs php for use with the Apple-provided Apache installation - if you want to use WebSTAR you will have to edit the config files in that package to use the new php installation.

And stop looking for PHP in Activity Monitor - if you see it there for more than half a second, something is wrong.

Quote:
Originally Posted by OSXdude
I think one important point that you probably missed in my original post, is that I am NOT using Apache. I am using WebSTAR

I saw that, and suggested in #2 that you trash it. Old & unsupported.

Last edited by acme.mail.order; 10-12-2010 at 06:37 PM.
acme.mail.order is offline   Reply With Quote
Old 10-12-2010, 08:05 PM   #9
J_T
Triple-A Player
 
Join Date: Jul 2007
Posts: 96
acme that is true, although but runs a separate process called WSCGIServer, I believe all php is run through that as a fast CGI. Thats probably what is most likely crashing for this guy.

On another note, WebStar is so old it was never supported for 10.4. It just happens to run by luck. Its definately time to get away from it and run apache or lighthttpd or something... any thing.
J_T is offline   Reply With Quote
Old 10-12-2010, 08:08 PM   #10
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,329
I'd really like to know why everyone keeps insisting on reinventing the wheel. Apache/PHP* has come fully tweaked and integrated since at least 10.3, all you need to do is remove one, maybe two, characters from a single config file. If you can't handle this then PHP itself will be a complete mystery.

* ok, so not the latest/greatest version and Apple didn't compile a few modules.
acme.mail.order is offline   Reply With Quote
Old 10-13-2010, 03:09 PM   #11
OSXdude
Triple-A Player
 
Join Date: Jul 2006
Posts: 96
Quote:
Originally Posted by acme.mail.order
You are all missing a major point - PHP does not run as a background process.

Starting php from the command line with ./php will make it run in command-line mode, patiently waiting for keyboard input.

The Entropy package installs php for use with the Apple-provided Apache installation - if you want to use WebSTAR you will have to edit the config files in that package to use the new php installation.

And stop looking for PHP in Activity Monitor - if you see it there for more than half a second, something is wrong.

I saw that, and suggested in #2 that you trash it. Old & unsupported.

Acme.mail.order, I beg to differ with you. Here is what 4D has to say about the setup I am using. They in fact recommended this method when WebSTAR V came out:


How to configure FastCGIs for WebSTAR V?
(c) 2002-2005 4D, Inc.

***********************
Introduction to FastCGI
***********************

FastCGI is an open extension to CGI that provides high performance for Internet applications without the penalties of CGI. FastCGIs are a bit more difficult to set up than CGIs but they are usually dramatically faster. For instance, PHP is ten to twenty times faster when running as a FastCGI.

Why are FastCGI so 'Fast'? With CGIs, a new process needs to be created each time the CGI is executed, which is an expensive operation. With FastCGIs, a pool of processes is created only once, and it will stay alive to process many different requests. This is why we can speak of 'FastCGI servers'.

4D WebSTAR V, starting with v5.1, is now able to work with FastCGIs (in addition to Unix and AppleEvents CGIs). Only Remote FastCGIs in their Responder role are supported. This means that the administrator is responsible for launching and maintaining his FastCGI servers and that they will use the TCP/IP protocol, even locally (as opposed to using the not-so-reliable Unix Domain Sockets).

Setting up and running a FastCGI is typically a three-step process:
1) Download and build your FastCGI application.
2) Configure WebSTAR V (that's the easy part) to tell it where the FastCGI is. You just have to specify a couple of things (IP address + Port Number). This will be '127.0.0.1' for local servers.
3) Launch your FastCGI server that will listen on a given port.

FastCGIs can be written in any programming language. We provide examples for Java, Perl and C/C++ (with PHP). For further information, especially on how to write your own FastCGIs, check out www.fastcgi.com. You will need to install and download the FastCGI toolkit.


*****************************
Using PHP as a FastCGI server
*****************************

When installing WebSTAR V (v5.1 or later), two binaries of PHP will be installed. The default configuration uses CGI, and the corresponding binary is in /WebServer/cgi-bin. The other one, compiled as a FastCGI, is located in /WebServer/fcgi-bin. This is of course the one we will use in this section.

1) Configure WebSTAR

- Use the Admin Client to connect to your server, and select the appropriate Web Host in the Settings Group window.
- Select the CGI Settings panel from the Web Host panels group.
- Make sure that the box "Allow UNIX CGI and FastCGI execution" is checked.
- Add this entry to the CGI actions table (or modify any existing entries related to PHP):
[PHP] [ends with] [.php][FastCGI][127.0.0.1:8081]
- Click Save.

2) Run the PHP binary.

You just need to launch one instance of the php application, because the binary will take care of opening the listening socket, creating instances of itself and restarting a php process if it dies. By default it will not create more than one process. You can change this by using the PHP_FCGI_CHILDREN environment variable.

- Use the Terminal application navigate to the fcgi-bin directory:
cd /Applications/4DWebSTAR/WebServer/fcgi-bin
- Set the PHP_FCGI_CHILDREN environment variable:
set env PHP_FCGI_CHILDREN 8
- Launch PHP using the script called fcgirunner:
./fcgirunner.pl 8081 ./php
- Check that the processes have been started:
top
(you should see many processes called 'php' - hit Ctrl-C to quit top)
- You can also use 'top' to monitor the php processes to see if all are busy and you need to set the PHP_FCGI_CHILDREN environment variable higher.

3) You can now serve PHP files. (try for instance the file installed by default at http://localhost/Examples/PHP/example.php)

TIP: To kill the processes you created when you are done with serving the FastCGI, type this in the Terminal:
killall php
__________________
27" 2.8 GHz Quad Core i7 iMac with 8 GB of RAM and a 2 TB Hitachi Hard Drive
Zippin' along, baby, zippin' along! Catch us if you can! -- Dave Clark Five 1965
OSXdude is offline   Reply With Quote
Old 10-13-2010, 06:13 PM   #12
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,329
Lets approach the issue from another direction then.

Do you need it?

One might say that a 500MHz box needs all the help it can get, one might also say that a webserver today needs up-to-date software. I've spent many hours this week clearing out hacked files on a commercial webserver account that had all the updates and someone still got in.

You can get a high-powered web host for $20/month with all this done for you.
acme.mail.order is offline   Reply With Quote
Old 10-15-2010, 04:25 PM   #13
OSXdude
Triple-A Player
 
Join Date: Jul 2006
Posts: 96
Quote:
Originally Posted by J_T
This is a bit of a hack but it has gotten me through with unstable processes. make a shell script and load it into cron for a minute by minute check.

pidcheck.sh

Code:
ofPid=`/bin/ps -ax | grep php | grep -v pidcheck.sh | grep -v grep`

if [ -n "$ofPid" ] 
then
      exit 0;	
     
else
	
	/path/to/fcgirunning.pl
fi
exit 0;
its not pretty but can buy you some time. Load it into your crontab and have it check every minute. You will have to change the "php" to whatever process you are looking for.

Hello J_T. I apologize for the delay in getting back with you, but I've been really busy here.

Anyway, I am interested in trying out your solution, but I don't quite understand how to do it, as my experience with shell scripts is about zilch.

Anyway, I copied your script into a new BBEdit document, changed the path to fcgirunner.pl, and then placed the file in my home directory.

Next, I opened the Terminal and typed:

sh pidcheck.sh

The Terminal returned:

pidcheck.sh: line 1: [: missing `]'

So apparently, something is wrong with the script the way you wrote it.

From what I read, after we get the script written correctly, I need to just use the Terminal to make it executable.

But after that, the rest of your response is all Greek to me. I have no experience with crontab, and I am not certain how to set it up with this shell script.

I am also unsure of what you mean by this:

You will have to change the "php" to whatever process you are looking for.

Whenever I start up PHP in the Terminal, I only see one occurrence of it in Activity Monitor. Am I somehow supposed to add the process ID or something else to the script? That doesn't seem to make sense, because whenever I have to restart PHP, it has a difference process ID.

Can you please walk me through this a step at a time?

Thanks so much!
__________________
27" 2.8 GHz Quad Core i7 iMac with 8 GB of RAM and a 2 TB Hitachi Hard Drive
Zippin' along, baby, zippin' along! Catch us if you can! -- Dave Clark Five 1965
OSXdude is offline   Reply With Quote
Old 10-15-2010, 04:30 PM   #14
OSXdude
Triple-A Player
 
Join Date: Jul 2006
Posts: 96
Quote:
Originally Posted by Hal Itosis

tl;dr

To answer just the title literally (How To Watch And Auto-Restart Crashing PHP), that sounds like a job for launchd (with the KeepAlive key enabled). However, if the crashes are too close together (every 11 seconds or something?), then launchd may get irritated... and just let it stay crashed. [the real solution is to fix the cause of the crash.]

Hello Hal Itosis . . . Yeah, you REALLY smell!

Anyway, thanks for your response, but I am very new to all of this stuff. The crashed only occur about four times a day, and even that is just an estimate.

Can you please explain in more detail, step-by-step, what you mean by "launchd with the KeepAlive key enabled"?

Thanks so much!
__________________
27" 2.8 GHz Quad Core i7 iMac with 8 GB of RAM and a 2 TB Hitachi Hard Drive
Zippin' along, baby, zippin' along! Catch us if you can! -- Dave Clark Five 1965
OSXdude is offline   Reply With Quote
Old 10-15-2010, 04:35 PM   #15
renaultssoftware
MVP
 
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
For the brackets you need two: [[ -oops error ]]
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Old 10-15-2010, 04:35 PM   #16
OSXdude
Triple-A Player
 
Join Date: Jul 2006
Posts: 96
Quote:
Originally Posted by acme.mail.order
Lets approach the issue from another direction then.

Do you need it?

One might say that a 500MHz box needs all the help it can get, one might also say that a webserver today needs up-to-date software. I've spent many hours this week clearing out hacked files on a commercial webserver account that had all the updates and someone still got in.

You can get a high-powered web host for $20/month with all this done for you.

Sorry, but I don't quite follow you. Do I need what?

$20 a month may not seem like a lot of money to you, but that is almost $250 more a year, on top of annual domain name registrations, paying for domain discreet, a monthly cable net bill of almost $100, and all of the other expenses that go into running and maintaining our websites and server. I live on a fixed budget, so I must do with what I've got.

It may not be the best situation, but these are the tools that I have to work with, so I am reaching out to folks here to help me find a way to get a handle on the PHP/fastcgi crashes. I have no idea what is causing them, just theories.

Thanks.
__________________
27" 2.8 GHz Quad Core i7 iMac with 8 GB of RAM and a 2 TB Hitachi Hard Drive
Zippin' along, baby, zippin' along! Catch us if you can! -- Dave Clark Five 1965
OSXdude is offline   Reply With Quote
Old 10-15-2010, 04:49 PM   #17
OSXdude
Triple-A Player
 
Join Date: Jul 2006
Posts: 96
Quote:
Originally Posted by renaultssoftware
For the brackets you need two: [[ -oops error ]]

Hello, I changed that line in the script so that it now says:

if [[ -n "$ofPid" ]]

However, now, when I type "sh pidcheck.sh" in the Terminal, it says:

pidcheck.sh: line 1: [[: command not found
: command not found:
__________________
27" 2.8 GHz Quad Core i7 iMac with 8 GB of RAM and a 2 TB Hitachi Hard Drive
Zippin' along, baby, zippin' along! Catch us if you can! -- Dave Clark Five 1965
OSXdude is offline   Reply With Quote
Old 10-15-2010, 04:59 PM   #18
OSXdude
Triple-A Player
 
Join Date: Jul 2006
Posts: 96
Am I also supposed to change the following string so that it points to the actual PHP binary that I am using:

ofPid=`/bin/ps -ax | grep php | grep -v pidcheck.sh | grep -v grep`

I am NOT using the PHP that is installed with OSX. As I said in an earlier post, I am using a self-contained installation of PHP in my Applications folder.

Once we get this working, will this script run continuously, keeping an eye on PHP?

Will the script start up automatically if I add it to my startup items in my user account?

Thanks!
__________________
27" 2.8 GHz Quad Core i7 iMac with 8 GB of RAM and a 2 TB Hitachi Hard Drive
Zippin' along, baby, zippin' along! Catch us if you can! -- Dave Clark Five 1965
OSXdude is offline   Reply With Quote
Old 10-15-2010, 06:27 PM   #19
schwartze
MVP
 
Join Date: Dec 2002
Location: NYC
Posts: 1,005
from the info you posted in post 11 looking for php should be enough - you shouldn't have to change that.

I just tested the script with [[ ]] and it works. You may want to copy and paste it again then just make that change.

It will not just run and check. That's why J_T suggested making it a cron job.

Once you have the script working while in the terminal look at man crontab
__________________
Many things are easier to break than to fix. - Craig R. Arko
schwartze is offline   Reply With Quote
Old 10-16-2010, 02:27 PM   #20
Hal Itosis
Hall of Famer
 
Join Date: Apr 2002
Posts: 3,315
Quote:
Originally Posted by OSXdude
Hello Hal Itosis . . . Yeah, you REALLY smell!

Anyway, thanks for your response, but I am very new to all of this stuff. The crashed only occur about four times a day, and even that is just an estimate.

Can you please explain in more detail, step-by-step, what you mean by "launchd with the KeepAlive key enabled"?

Thanks so much!

Illustration: inside your /Users/*you*/Library/ folder, create a folder called LaunchAgents

Then, copy this text...
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>my.textedit.job</string>
	<key>KeepAlive</key>
	<true/>
	<key>ProgramArguments</key>
	<array>
		<string>/Applications/TextEdit.app/Contents/MacOS/TextEdit</string>
	</array>
</dict>
</plist>
...and save the file (with a .plist extension) into that LaunchAgents folder.

E.g. we now have something like this on your disk:
/Users/osxdude/Library/LaunchAgents/kom.testkeepalive.plist

The next time you log in, TextEdit will launch.
When/if you try to quit TextEdit, it will relaunch.

I'll let Apple fill in the blanks:

man launchd.plist
Daemons and Agents

You owe me a Mentos.

Last edited by Hal Itosis; 10-16-2010 at 02:43 PM. Reason: fixed "pliist" typo
Hal Itosis is offline   Reply With Quote
Reply

Tags
crash, php, process, restart, webstar

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 06:34 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.