windows下php-cgi进程守护
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(icount<1) icount=1; if(icount>32) 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<icount;i++) CloseHandle(CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,(LPVOID)s,0,0)); WaitForSingleObject(hJob, INFINITE); closesocket(s); WSACleanup(); ServiceStatus.dwWin32ExitCode = 0; ServiceStatus.dwCurrentState = SERVICE_STOPPED; SetServiceStatus(hStatus, &ServiceStatus); } //--------------------------------------------------------------------------- void ThreadProc(LPVOID pParam) { STARTUPINFO si; PROCESS_INFORMATION pi; while(ServiceStatus.dwCurrentState == SERVICE_RUNNING) { ZeroMemory(&si,sizeof(STARTUPINFO)); ZeroMemory(&pi,sizeof(PROCESS_INFORMATION)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = (HANDLE)pParam; si.hStdOutput = INVALID_HANDLE_VALUE; si.hStdError = INVALID_HANDLE_VALUE; if(0==CreateProcess(NULL,cmdbuf,NULL,NULL,TRUE, CREATE_NO_WINDOW | CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB ,NULL,NULL,&si,&pi)) break; AssignProcessToJobObject(hJob, pi.hProcess); ResumeThread(pi.hThread); WaitForSingleObject(pi.hProcess, INFINITE); CloseHandle( pi.hProcess); CloseHandle( pi.hThread ); } } //--------------------------------------------------------------------------- void ControlHandler(DWORD request) { switch(request) { case SERVICE_CONTROL_STOP: ServiceStatus.dwWin32ExitCode = 0; ServiceStatus.dwCurrentState = SERVICE_STOPPED; SetServiceStatus(hStatus, &ServiceStatus); CloseHandle(hJob); return; case SERVICE_CONTROL_SHUTDOWN: ServiceStatus.dwWin32ExitCode = 0; ServiceStatus.dwCurrentState = SERVICE_STOPPED; SetServiceStatus(hStatus, &ServiceStatus); CloseHandle(hJob); return; } ::SetServiceStatus (hStatus, &ServiceStatus); } //--------------------------------------------------------------------------- void Install(void) { char szFilePath[256]; SC_HANDLE hSCM,hService; ZeroMemory(szFilePath,256); GetModuleFileName(NULL,szFilePath,256); hSCM=::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); SC_LOCK sclLock = LockServiceDatabase(hSCM); hService = ::CreateService( hSCM, cServiceName, cServiceName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, szFilePath, NULL, NULL,NULL, NULL, NULL); SERVICE_DESCRIPTION sdBuf; sdBuf.lpDescription = "php fastcgi process helper for windows"; ChangeServiceConfig2(hService,SERVICE_CONFIG_DESCRIPTION,&sdBuf); ::CloseServiceHandle(hService); ::UnlockServiceDatabase(sclLock); ::CloseServiceHandle(hSCM); printf("Service Install OK !!!"); } //--------------------------------------------------------------------------- void Uninstall(void) { SERVICE_STATUS sStatus ; SC_HANDLE hSCM=::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); SC_HANDLE hService=::OpenService(hSCM, cServiceName, SERVICE_STOP | DELETE); if (hService==NULL) { printf("Service Open Error !!!"); } else { ::ControlService(hService, SERVICE_CONTROL_STOP, &sStatus); ::DeleteService(hService); printf("Service Uninstall OK !!!"); } ::CloseServiceHandle(hService); ::CloseServiceHandle(hSCM); } //---------------------------------------------------------------------------

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.
