start_server_with_nginx_on_windows.bat 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. @echo off
  2. chcp 65001 >nul
  3. setlocal EnableDelayedExpansion
  4. rem Set basic variables
  5. set "SCRIPT_DIR=%~dp0"
  6. set "LOG_DIR=%SCRIPT_DIR%logs"
  7. set "BACKEND_PID_FILE=%SCRIPT_DIR%backend.pid"
  8. set "NGINX_PID_FILE=%SCRIPT_DIR%nginx.pid"
  9. if not exist "%LOG_DIR%" mkdir "%LOG_DIR%"
  10. set "LOG_FILE=%LOG_DIR%\server_%date:~0,4%%date:~5,2%%date:~8,2%.log"
  11. rem Clean up old processes and files
  12. taskkill /F /IM nginx.exe >nul 2>&1
  13. taskkill /F /IM python.exe /FI "WINDOWTITLE eq OpenWebUI*" >nul 2>&1
  14. timeout /t 2 >nul
  15. if exist "%BACKEND_PID_FILE%" del "%BACKEND_PID_FILE%"
  16. if exist "%NGINX_PID_FILE%" del "%NGINX_PID_FILE%"
  17. rem Create necessary directories for Nginx
  18. cd /d "%SCRIPT_DIR%nginx-1.26.3"
  19. if not exist "logs" mkdir logs
  20. if not exist "temp" mkdir temp
  21. if not exist "temp\client_body_temp" mkdir "temp\client_body_temp"
  22. if not exist "temp\proxy_temp" mkdir "temp\proxy_temp"
  23. if not exist "temp\fastcgi_temp" mkdir "temp\fastcgi_temp"
  24. if not exist "temp\uwsgi_temp" mkdir "temp\uwsgi_temp"
  25. if not exist "temp\scgi_temp" mkdir "temp\scgi_temp"
  26. goto :main
  27. :write_log
  28. echo [%date% %time%] %~1
  29. echo [%date% %time%] %~1 >> "%LOG_FILE%"
  30. exit /b
  31. :start_backend
  32. cd /d "%SCRIPT_DIR%backend"
  33. call :write_log "Starting backend service..."
  34. start "OpenWebUI Backend" /B cmd /c start_windows.bat
  35. if errorlevel 1 (
  36. call :write_log "Failed to start backend"
  37. exit /b 1
  38. )
  39. timeout /t 5 >nul
  40. for /f "tokens=2" %%a in ('wmic process where "commandline like '%%python%%' and name='python.exe'" get processid ^| findstr /r "[0-9]"') do (
  41. echo %%a > "%BACKEND_PID_FILE%"
  42. call :write_log "Backend started with PID: %%a"
  43. exit /b 0
  44. )
  45. call :write_log "Failed to get backend PID"
  46. exit /b 1
  47. :start_nginx
  48. cd /d "%SCRIPT_DIR%nginx-1.26.3"
  49. call :write_log "Starting Nginx..."
  50. start "" /B nginx.exe
  51. if errorlevel 1 (
  52. call :write_log "Failed to start Nginx"
  53. exit /b 1
  54. )
  55. timeout /t 2 >nul
  56. for /f "tokens=2" %%a in ('tasklist /fi "imagename eq nginx.exe" /nh ^| findstr "nginx"') do (
  57. echo %%a > "%NGINX_PID_FILE%"
  58. call :write_log "Nginx started with PID: %%a"
  59. exit /b 0
  60. )
  61. call :write_log "Failed to get Nginx PID"
  62. exit /b 1
  63. :start
  64. call :write_log "Starting services..."
  65. call conda activate open-webui
  66. if errorlevel 1 (
  67. call :write_log "Failed to activate conda environment"
  68. goto :cleanup
  69. )
  70. call :start_backend
  71. if errorlevel 1 goto :cleanup
  72. call :start_nginx
  73. if errorlevel 1 goto :cleanup
  74. cd /d "%SCRIPT_DIR%"
  75. call :write_log "All services started successfully"
  76. exit /b 0
  77. :cleanup
  78. call :write_log "Start failed, cleaning up..."
  79. call :stop
  80. exit /b 1
  81. :stop
  82. call :write_log "Stopping services..."
  83. if exist "%BACKEND_PID_FILE%" (
  84. for /f %%a in (%BACKEND_PID_FILE%) do (
  85. taskkill /F /T /PID %%a >nul 2>&1
  86. if not errorlevel 1 call :write_log "Stopped backend process (PID: %%a)"
  87. )
  88. del "%BACKEND_PID_FILE%"
  89. )
  90. for /f "tokens=2" %%a in ('wmic process where "commandline like '%%python%%' and name='python.exe'" get processid ^| findstr /r "[0-9]"') do (
  91. taskkill /F /T /PID %%a >nul 2>&1
  92. call :write_log "Stopped additional Python process (PID: %%a)"
  93. )
  94. if exist "%NGINX_PID_FILE%" (
  95. for /f %%a in (%NGINX_PID_FILE%) do (
  96. taskkill /F /PID %%a >nul 2>&1
  97. if not errorlevel 1 call :write_log "Stopped Nginx (PID: %%a)"
  98. )
  99. del "%NGINX_PID_FILE%"
  100. )
  101. taskkill /F /IM nginx.exe >nul 2>&1
  102. if not errorlevel 1 call :write_log "Stopped all remaining Nginx processes"
  103. call :write_log "All services stopped"
  104. exit /b 0
  105. :status
  106. set "running=0"
  107. if exist "%BACKEND_PID_FILE%" (
  108. for /f %%a in (%BACKEND_PID_FILE%) do (
  109. tasklist /FI "PID eq %%a" 2>nul | find "%%a" >nul
  110. if not errorlevel 1 (
  111. call :write_log "Backend is running (PID: %%a)"
  112. set /a running+=1
  113. )
  114. )
  115. )
  116. if exist "%NGINX_PID_FILE%" (
  117. for /f %%a in (%NGINX_PID_FILE%) do (
  118. tasklist /FI "PID eq %%a" 2>nul | find "%%a" >nul
  119. if not errorlevel 1 (
  120. call :write_log "Nginx is running (PID: %%a)"
  121. set /a running+=1
  122. )
  123. )
  124. )
  125. if %running% equ 2 (
  126. call :write_log "All services are running"
  127. exit /b 0
  128. ) else (
  129. call :write_log "Some services are not running"
  130. exit /b 1
  131. )
  132. :main
  133. if "%1"=="" (
  134. echo Usage: %~nx0 {start^|stop^|status}
  135. exit /b 1
  136. )
  137. if "%1"=="start" (
  138. call :stop
  139. timeout /t 2 >nul
  140. call :start
  141. ) else if "%1"=="stop" (
  142. call :stop
  143. ) else if "%1"=="status" (
  144. call :status
  145. ) else (
  146. echo Invalid command. Use: %~nx0 {start^|stop^|status}
  147. exit /b 1
  148. )
  149. exit /b %errorlevel%