windows10使用bat脚本安装前后端环境之nginx注册服务

首先需要搞清楚nginx本地是怎么安装配置的、然后在根据如下步骤编写bat脚本

思路

1.下载nginx-1.26 zip压缩包安装包
2.调整conf配置
3.借助winsw将nginx应用注册为服务,winsw下载地址 然后重命名nginx_service.exe
4.配置nginx-service.xml
5.注册windows server服务
6.启动nginx

在这里插入图片描述
nginx.conf (最简化配置、可自定义调整)

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
	   
	server {
        listen 9000;
        server_name localhost; 
		
		# 前端域名映射
        location / {
            root   html;
            index  index.html index.htm;
			try_files $uri $uri/ /index.html;
        }
		
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

nginx-service.xml

<service> 
  <id>NGINX_LINSN</id> 
  <name>NGINX_LINSN</name> 
  <description>NGINX_LINSN service</description> 
  <executable>C:\nginx-1.26.0\nginx.exe</executable> 
  <logpath>C:\nginx-1.26.0\logs</logpath> 
</service> 

bat脚本

@echo off
echo windows10 x64 server Nginx init
REM 请求管理员权限并隐藏窗口
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 :: & echo 执行中,请稍候... & timeout 2 > nul","","runas",0)(window.close)&&exit
setlocal enabledelayedexpansion


rem 设置 Nginx 目录变量,方便后续引用和修改
set ServiceName=NGINX_LINSN

%~d0
cd %~dp0
set CURRENT_DIR=%~dp0
set CURRENT_DIR=%CURRENT_DIR:~0,-1%
set NGINX_DIR=%CURRENT_DIR%\nginx-1.26.0
set NGINX_SERVER_DIR=%CURRENT_DIR%\nginx-1.26.0\nginx-service.exe
set NGINX_LOGS=%CURRENT_DIR%\nginx-1.26.0\logs

echo query %ServiceName% state info ...
sc query "%ServiceName%" >nul 2>&1
if %errorLevel% equ 0 (
    rem 服务存在,检查服务状态
    for /f "tokens=2 delims=: " %%a in ('sc query "%ServiceName%" ^| find "STATE"') do (
        if /i "%%a" equ "RUNNING" (
            echo Nginx server state is RUNNING...
        ) else if /i "%%a" equ "STOPPED" (
            echo Nginx server state is STOPPED...
            net start %ServiceName% 
        ) else (
			echo Nginx server state is %%a...
        )
    )
) else (
	rem 服务不存在,初始化服务
	echo Nginx-server is not exist, init server %ServiceName%
	cd %NGINX_DIR%
	
	rem 创建 XML 配置文件内容
	echo ^<service^> > nginx-service.xml
	echo   ^<id^>%serviceName%^</id^> >> nginx-service.xml
	echo   ^<name^>%serviceName%^</name^> >> nginx-service.xml
	echo   ^<description^>%serviceName% service^</description^> >> nginx-service.xml
	echo   ^<executable^>%NGINX_DIR%\nginx.exe^</executable^> >> nginx-service.xml
	echo   ^<logpath^>%NGINX_LOGS%^</logpath^> >> nginx-service.xml
	echo ^</service^> >> nginx-service.xml

	rem 安装服务
	echo Nginx-server start server %ServiceName%
	%NGINX_SERVER_DIR% install

	rem 启动服务
	net start %ServiceName%	
	
)

echo windows-server-nginx success
endlocal

REM 执行完成后自动关闭
timeout /t 2 >nul
exit

亲测有效。

以下是一个简单的bat脚本,用于管理nginx的系统环境配置。 ```bat @echo off setlocal EnableDelayedExpansion set NGINX_CONF_PATH=C:\nginx\conf\nginx.conf :MENU cls echo ===========NGINX配置管理器============ echo. echo 1. 查看当前配置 echo 2. 修改配置 echo 3. 重启服务 echo 4. 退出 echo ======================================= set /p choice=请选择操作(数字): if "%choice%"=="1" ( echo 查看当前配置... type %NGINX_CONF_PATH% pause goto MENU ) if "%choice%"=="2" ( echo 修改配置... notepad %NGINX_CONF_PATH% pause goto MENU ) if "%choice%"=="3" ( echo 重启服务... taskkill /F /IM nginx.exe start /B C:\nginx\nginx.exe echo 服务已重启! pause goto MENU ) if "%choice%"=="4" ( echo 退出... exit ) echo 无效的选项,请重新输入! pause goto MENU ``` 该脚本定义了nginx配置文件的路径,然后给出了一个菜单供用户选择。菜单包含了四个选项:查看当前配置、修改配置、重启服务和退出。 当用户选择查看当前配置时,脚本使用`type`命令输出nginx配置文件内容,并暂停脚本执行等待用户按下任意键返回菜单。 当用户选择修改配置时,脚本使用`notepad`命令打开nginx配置文件供用户编辑,并暂停脚本执行等待用户关闭编辑器。 当用户选择重启服务时,脚本使用`taskkill`命令杀掉nginx进程,并使用`start`命令重新启动nginx服务,并输出提示信息“服务已重启!”,最后暂停脚本执行等待用户按下任意键返回菜单。 当用户选择退出时,脚本使用`exit`命令退出脚本。 请注意,该脚本只是一个简单的示例,可能无法完全满足您的需求,您需要根据自己的实际情况进行适当修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值