Skip to content

Instantly share code, notes, and snippets.

@vensko
Last active August 29, 2015 14:17
Show Gist options
  • Save vensko/a0c911d1a09f1357786b to your computer and use it in GitHub Desktop.
Save vensko/a0c911d1a09f1357786b to your computer and use it in GitHub Desktop.
start_server.bat
rem http://blog.vensko.net/webdev/web-server-batch-script
@echo off
@setlocal
@setlocal enabledelayedexpansion
@cls
SET PARAMS=%*
if "!PARAMS!"=="" SET PARAMS=php
if exist "%cd%\www" SET DOCUMENT_ROOT=%cd%\www
if exist "%cd%\public" SET DOCUMENT_ROOT=%cd%\public
if exist "%cd%\web" SET DOCUMENT_ROOT=%cd%\web
if "!DOCUMENT_ROOT!"=="" (
echo Public directory not found!
pause && exit /B 1
)
for %%a in (.) do set PROJECT_NAME=%%~na
SET PROJECT_ROOT=%cd%
SET PROJECT_TMP=%cd%\storage
SET PROJECT_LOGS=!PROJECT_TMP!\logs
SET PROJECT_SESSIONS=!PROJECT_TMP!\framework\sessions
SET PROJECT_MAIL_LOG=!PROJECT_LOGS!\mail.txt
SET PHP_EXTENSIONS=PHP\PHP-5.6
SET PHP_ROUTER=router.php
rem MariaDB
SET PROJECT_DB_TMP=!PROJECT_TMP!\mariadb_tmp
SET PROJECT_DB_PID=!PROJECT_DB_TMP!\mariadb.pid
SET PROJECT_SLOW_QUERIES_LOG=!PROJECT_LOGS!\slow-queries.log
SET MARIADB_DATA=!PROJECT_ROOT!\database\mariadb_data
rem MongoDB
SET MONGODB_DATA=!PROJECT_ROOT!\database\mongodb_data
call :phpserver
if not "%ERRORLEVEL%"=="0" goto :failed
call :mariadb
if not "%ERRORLEVEL%"=="0" goto :failed
call :memcached
if not "%ERRORLEVEL%"=="0" goto :failed
call :mongodb
if not "%ERRORLEVEL%"=="0" goto :failed
echo Server started with parameters !PARAMS!
timeout /T 2
if not "!PARAMS!"=="!PARAMS:php=!" (
if exist W: subst W: /D
subst W: "!PROJECT_ROOT!"
start http://localhost/
)
goto :eof
:failed
echo ===== Server start failed, stopping...
call stop_server.bat
pause
goto :eof
REM FUNCTIONS
:phpserver
nircmd killprocess php.exe
nircmd waitprocess php.exe
if "!PARAMS!"=="!PARAMS:php=!" exit /B 0
echo Starting PHP server...
if not exist "!PROJECT_TMP!" mkdir "!PROJECT_TMP!"
if not exist "!PROJECT_LOGS!" mkdir "!PROJECT_LOGS!"
call :tmpgitignore "!PROJECT_LOGS!"
if not exist "!PROJECT_SESSIONS!" mkdir "!PROJECT_SESSIONS!"
call :tmpgitignore "!PROJECT_SESSIONS!"
if not "!PARAMS!"=="!PARAMS:memcached=!" SET PHP_MEMCACHED=-d extension="!PHP_EXTENSIONS!\php_memcache.dll"
if not "!PARAMS!"=="!PARAMS:mongodb=!" SET PHP_MONGODB=-d extension="!PHP_EXTENSIONS!\php_mongo.dll"
if not "!PARAMS!"=="!PARAMS:xdebug=!" SET PHP_XDEBUG=-d zend_extension="!PHP_EXTENSIONS!\php_xdebug.dll"
hideconsole php -S 127.0.0.1:80 -t "!DOCUMENT_ROOT!" !PHP_XDEBUG! !PHP_MONGODB! !PHP_MEMCACHED!^
-d sendmail_path="cat - >> "!PROJECT_MAIL_LOG!""^
-d session.save_path="!PROJECT_SESSIONS:\=/!"^
-d opcache.enable=1^
-d extension="!PHP_EXTENSIONS!\php_xcache.dll"^
-d xcache.coverager=0^
-d xcache.var_ttl=3600^
-d xcache.var_size=2M^
-d xdebug.file_link_format=pstorm://%%f:%%l^
-d xdebug.collect_params=4^
-d xdebug.collect_return=1^
-d xdebug.remote_enable=1^
-d xdebug.show_exception_trace=0^
-d xdebug.force_display_errors=1^
-d xdebug.show_local_vars=1^
-d xdebug.profiler_output_dir="!PROJECT_LOGS:\=/!"^
-d xdebug.profiler_output_name="cachegrind.out.xt"^
-d xdebug.profiler_enable_trigger=1^
-d xdebug.trace_output_dir="!PROJECT_LOGS:\=/!"^
-d xdebug.trace_output_name="xdebug.trace.%%H.dbg"^
"!PHP_ROUTER!"
tasklist /FI "IMAGENAME eq php.exe" 2>NUL | find /I /N "php.exe" >nul
exit /B %ERRORLEVEL%
:mariadb
tasklist /FI "IMAGENAME eq mysqld.exe" 2>NUL | find /I /N "mysqld.exe" >nul
if "%ERRORLEVEL%"=="0" (
mysqladmin shutdown -u root 2>&1 >nul
nircmd waitprocess mysqld.exe
)
if "!PARAMS!"=="!PARAMS:mariadb=!" exit /B 0
echo Starting MariaDB...
if not exist "!MARIADB_DATA!" (
SET NEW_DB_MODE=1
mkdir "!MARIADB_DATA!"
)
if not exist "!PROJECT_DB_TMP!" mkdir "!PROJECT_DB_TMP!"
call :tmpgitignore "!PROJECT_DB_TMP!"
hideconsole mysqld --no-defaults -u root^
--port=3306^
--character-set-server=utf8mb4^
--collation-server=utf8mb4_general_ci^
--datadir="!MARIADB_DATA!"^
--innodb-data-home-dir="!MARIADB_DATA!"^
--innodb-data-file-path=ibdata1:10M:autoextend^
--innodb-file-per-table^
--init-connect="SET NAMES UTF-8"^
--pid-file="!PROJECT_DB_PID!"^
--innodb-log-file-size=10M^
--innodb-log-group-home-dir="!PROJECT_DB_TMP!"^
--aria-log-dir-path="!PROJECT_DB_TMP!"^
--slow-query-log^
--slow-query-log-file="!PROJECT_SLOW_QUERIES_LOG!"^
--shared-memory^
--skip-grant-tables^
--skip-name-resolve^
--skip-slave-start
tasklist /FI "IMAGENAME eq mysqld.exe" 2>NUL | find /I /N "mysqld.exe" >nul
if "!NEW_DB_MODE!"=="1" mysqladmin -b -s -w --port=3306 create !PROJECT_NAME!_db
exit /B %ERRORLEVEL%
:memcached
nircmd killprocess memcached.exe
nircmd waitprocess memcached.exe
if "!PARAMS!"=="!PARAMS:memcached=!" exit /B 0
echo Starting Memcached...
hideconsole memcached
tasklist /FI "IMAGENAME eq memcached.exe" 2>NUL | find /I /N "memcached.exe" >nul
exit /B %ERRORLEVEL%
:mongodb
tasklist /FI "IMAGENAME eq mongod.exe" 2>NUL | find /I /N "mongod.exe" >nul
if "%ERRORLEVEL%"=="0" (
mongo --eval "db.getSiblingDB('admin').shutdownServer()" >nul
nircmd waitprocess mongod.exe
)
if "!PARAMS!"=="!PARAMS:mongodb=!" exit /B 0
echo Starting MongoDB...
if not exist "!MONGODB_DATA!" mkdir "!MONGODB_DATA!"
hideconsole mongod --quiet --httpinterface --noauth --rest --profile 0 --nojournal --smallfiles --dbpath "!MONGODB_DATA!"
tasklist /FI "IMAGENAME eq mongod.exe" 2>NUL | find /I /N "mongod.exe" >nul
exit /B %ERRORLEVEL%
:tmpgitignore
if not exist "%~1\.gitignore" (
echo *>"%~1\.gitignore"
echo ^^!.gitignore>>"%~1\.gitignore"
)
exit /B 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment