Nginx

windows在同一個bat的後台啟動nginx和php-fpm

  • July 13, 2020

我正在嘗試在 Windows 10 上創建一個 bat 文件以在後台啟動 nginx 和 php-fpm。目前我可以這樣做,但命令視窗(黑色視窗)仍然打開。如果我關閉視窗,php-fpm 也會終止。

這是我在 start.bat 中添加的內容

@ECHO OFF
cd D:\servers\nginx-1.10.1\
echo Starting Nginx...
start /B cmd /K "D:\servers\nginx-1.10.1\nginx.exe"
echo Start Php-cgi...
start /B cmd /K "D:\servers\php-5.6.26\php-cgi.exe -b 127.0.0.1:9000 -c D:\servers\php-5.6.26\php.ini"
EXIT

這是列印在黑色視窗上的內容:

Starting Nginx...
Start Php-cgi...

我希望視窗自動關閉,但保持程序(nginx 和 php-fpm)執行。

php-cgi 鎖定視窗。嘗試 nginx 傢伙在他們的頁面上給出的解決方案。 Windows 上的 PHP-FastCGI

您可以隱藏控制台視窗作為解決此問題的方法之一。使用hidcon等 CLI 實用程序很容易。對於我的項目,我使用這個腳本:start-server.cmd

@Echo Off
:: Portable nginx+php5+mysql for Windows
:: Inquisitor, 2016-2018
CD /D "%~dp0"

:: Settings
Set LogFile=.\%~n0.log
Set TimestampFormat=[%%Date%% %%Time:~,8%%]
Set WatcherTimeout=10

If "%~1"=="" (
   Start "" "%~dp0tools\hidcon" %~nx0 restart_hidden
   Call Echo.>>"%LogFile%"
   Call :Log Loader started
   Exit
)


Set Path=%~dp0tools;%~dp0nginx;%~dp0php-fpm;%~dp0mysql\bin;%Path%
::  ImageMagick 6.9.3-3
rem Set Path=%Path%;%~dp0modules\imagick6
::  ImageMagick 7.0.5.10
rem Set Path=%Path%;%~dp0modules\imagick7
::  FFMpeg 3.3.1
Set Path=%Path%;%~dp0modules\ffmpeg

Set Temp=%~dp0\tmp



:: MySQL
Call :Log Starting MySQL...
:start-mysql
PushD .\mysql\bin
Start mysqld --defaults-file=my.ini --standalone
PopD
If "%1"=="restart" (
   Call :Log MySQL crashed, restarting
   Exit /B
)

:: PHP
Call :Log Starting PHP-FPM...
:start-php
PushD .\php-fpm
Start hidcon php-cgi -b 127.0.0.1:9123 -c ".\php.ini"
PopD
If "%1"=="restart" (
   Call :Log PHP-FPM crashed, restarting
   Exit /B
)

:: nginx
Call :Log Starting NginX...
:start-nginx
PushD .\nginx
Start nginx
PopD
If "%1"=="restart" (
   Call :Log NginX crashed, restarting
   Exit /B
)



TaskKill /F /IM "hidcon.exe"

For %%A In ("mysqld.exe", "php-cgi.exe", "nginx.exe") Do (
   wmic process where name="%%~A" CALL setpriority "high priority"
)



Set LockFile=.\.%~n0.lock
Echo.>"%LockFile%"

:Watcher
Ping -n %WatcherTimeout% 127.0.0.1>nul 2>nul
TaskList /FI "imagename EQ php-cgi.exe" /FO:CSV|FindStr /I /C:"php-cgi.exe">nul||Call :start-php restart
If Not Exist "%LockFile%" (
   Call :Log Lockfile not found, starting shutdown process

   PushD .\nginx
   nginx -s stop
   PopD

   mysqladmin -u root -pPCIIctvYUijcfNLR shutdown

   TaskKill /F /T /IM php-cgi.exe

   Call :Log Loader stopped
   Exit
)
GoTo :Watcher

:Log
   Call Echo %TimestampFormat% %*>>"%LogFile%"
Exit /B

和簡單的 stop-server.cmd

@Echo Off
Del "%~dp0.loader.lock" 2>nul

引用自:https://serverfault.com/questions/805563