windows下php-cgi进程守护
程序员文章站
2022-04-12 17:22:10
...
php在linux,unix下有php-fpm,spawn-fcgi等进程守护程序,但是在window下没有。 一般会以下命令启动C:/php5/php-cgi.exe-b127.0.0.1:9000-cC:/php5/php.ini 但是这样受限于php-cgi的PHP_FCGI_MAX_REQUESTS环境变量,默认处理500次后自动退出php-cgi.加之一些非
php在linux,unix下有php-fpm,spawn-fcgi等进程守护程序,但是在window下没有。
一般会以下命令启动 C:/php5/php-cgi.exe -b 127.0.0.1:9000 -c C:/php5/php.ini
但是这样受限于php-cgi的PHP_FCGI_MAX_REQUESTS环境变量,默认处理500次后自动退出php-cgi.加之一些非正常原因的异常退出,都会导致php-cgi.exe的退出。所有在window需要进程守护程序,当php-cgi退出后自动打开新的php-cgi进程。
注意事项:windows建议关闭Dr. Watson,否则 异常的php-cgi无法正常退出。
http://support.microsoft.com/kb/188296/zh-cn
// FastCGIHelper.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #pragma comment(lib, "ws2_32.lib") const PCHAR cServiceName="WinFastCGIHelper"; HANDLE hJob; INT icount; INT iport; CHAR ipbuf[32]; CHAR cmdbuf[256]; //--------------------------------------------------------------------------- SERVICE_STATUS ServiceStatus; SERVICE_STATUS_HANDLE hStatus; void Install(void); void Uninstall(void); void ServiceMain(int argc, char** argv); void ThreadProc(LPVOID pParam); void ControlHandler(DWORD request); //--------------------------------------------------------------------------- int _tmain(int argc, _TCHAR* argv[]) { if (argc>1 && _stricmp(argv[1],"/i")==0) { Install(); } else if (argc>1 && _stricmp(argv[1],"/u")==0) { Uninstall(); } else { SERVICE_TABLE_ENTRY ServiceTable[2]; ServiceTable[0].lpServiceName = cServiceName; ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain; ServiceTable[1].lpServiceName = NULL; ServiceTable[1].lpServiceProc = NULL; ::StartServiceCtrlDispatcher(ServiceTable); } return 0; } void ServiceMain(int argc, char** argv) { ServiceStatus.dwServiceType =SERVICE_WIN32; ServiceStatus.dwCurrentState =SERVICE_START_PENDING; ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; ServiceStatus.dwWin32ExitCode = 0; ServiceStatus.dwServiceSpecificExitCode = 0; ServiceStatus.dwCheckPoint = 0; ServiceStatus.dwWaitHint = 0; hStatus = ::RegisterServiceCtrlHandler(cServiceName, (LPHANDLER_FUNCTION)ControlHandler); if (hStatus == NULL) return; if (GetLastError()!=NO_ERROR) { ServiceStatus.dwCurrentState = SERVICE_STOPPED; ::SetServiceStatus(hStatus, &ServiceStatus); return; } CHAR cConfigName[256]; ZeroMemory(cConfigName,256); DWORD dwSize=GetModuleFileName(NULL,cConfigName,256); while(dwSize>0 && cConfigName[dwSize]!='\\') cConfigName[dwSize--]=0; strcat_s(cConfigName,"config.ini"); icount=GetPrivateProfileInt("setup","CmdCount",1,cConfigName); GetPrivateProfileString("setup","CmdPath","",cmdbuf,256,cConfigName); if(icount32) icount=32; iport =GetPrivateProfileInt("setup","Port",9000,cConfigName); GetPrivateProfileString("setup","IP","127.0.0.1",ipbuf,32,cConfigName); WSADATA wsa_data; WSAStartup(0x0202, &wsa_data); SOCKADDR_IN listen_addr; listen_addr.sin_family =AF_INET; listen_addr.sin_addr.S_un.S_addr= inet_addr(ipbuf); listen_addr.sin_port = htons(iport); DWORD s=socket(AF_INET,SOCK_STREAM,IPPROTO_IP); if(bind(s, (struct sockaddr*)&listen_addr, sizeof(struct sockaddr_in)) || listen(s,16) ){ ServiceStatus.dwCurrentState = SERVICE_STOPPED; ::SetServiceStatus(hStatus, &ServiceStatus); return; } CHAR strtmp[128]; ZeroMemory(strtmp,128); GetPrivateProfileString("env","PHPRC",".",strtmp,127,cConfigName); SetEnvironmentVariable("PHPRC",strtmp); GetPrivateProfileString("env","PHP_FCGI_MAX_REQUESTS","5000",strtmp,127,cConfigName); SetEnvironmentVariable("PHP_FCGI_MAX_REQUESTS",strtmp); hJob=CreateJobObject(NULL,NULL); JOBOBJECT_EXTENDED_LIMIT_INFORMATION limit; QueryInformationJobObject(hJob, JobObjectExtendedLimitInformation, &limit, sizeof(limit), NULL); limit.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &limit, sizeof(limit)); ServiceStatus.dwCurrentState = SERVICE_RUNNING; SetServiceStatus(hStatus, &ServiceStatus); for(int i=0;i
上一篇: 关于WebSocket部署服务器外网无法连接的解决方案
下一篇: 微信小程序之自定义抽屉菜单实例详解