更详细的说明,请参考文章末尾的参考链接,我这里只讲直接的操作步骤。
环境
- windows 7 / win 8.1 / win 10 pro
- nginx v1.12.0
- winsw v2.1.0
- 如果在 windows 7 下配置 nginx 为系统服务,启动报错(System error 1067 has occurred)的童鞋,请按我的配置文件配置。
步骤
- 下载最新版的 winsw,github 开源项目地址:传送门->猛戳进入,这是一个开源项目,求快的,直接进入下载页面:传送门->猛戳进入,笔者写该文时最新版为winsw-v2.1.0。
- 下载合适的 nginx 版本, 传送门->猛戳进入,笔者下载的是 nginx-1.12.0 stable version,解压到 C:\nginx-1.12.0
- 将第一步下载到的 WinSW.NET4.exe copy 到 nginx 目录:C:\nginx-1.12.0并改名为 winsw.exe
添加 winsw.xml,内容如下:
1
2
3
4
5
6
7
8
9
10<service>
<id>nginx</id>
<name>nginx</name>
<description>nginx</description>
<logpath>C:\nginx-1.12.0\logs</logpath>
<logmode>roll</logmode>
<depend></depend>
<executable>c:\nginx-1.12.0\nginx.exe</executable>
<stopexecutable>C:\nginx-1.12.0\nginx.exe -s stop</stopexecutable>
</service>以管理员身份运行控制台(cmd),进入 nginx 主目录,运行 winsw install,即大功告成。
1
2
3
4
5
6
7C:\nginx-1.12.0>winsw install
2017-05-09 19:17:49,419 INFO - Starting ServiceWrapper in the CLI mode
2017-05-09 19:17:49,753 INFO - Completed. Exit code is 0
C:\nginx-1.12.0>net start nginx
The nginx service is starting.
The nginx service was started successfully.
在win 8.1 / win10 上安装
按上面的方式,直接在运行窗口输入cmd 进入dos窗口执行命令,报错如下:1
2
3
4
5
6
7
8
9
10
11
12
13C:\nginx-1.12.0>winsw install
2017-06-17 11:40:10,216 INFO - Installing the service with id 'nginx'
2017-06-17 11:40:10,244 FATAL - WMI Operation failure: AccessDenied
WMI.WmiException: AccessDenied
at WMI.WmiRoot.BaseHandler.CheckError(ManagementBaseObject result)
at WMI.WmiRoot.ClassHandler.Invoke(Object proxy, MethodInfo method, Object[] args)
at winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)
at winsw.WrapperService.Main(String[] args)
WMI.WmiException: AccessDenied
at WMI.WmiRoot.BaseHandler.CheckError(ManagementBaseObject result)
at WMI.WmiRoot.ClassHandler.Invoke(Object proxy, MethodInfo method, Object[] args)
at winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)
at winsw.WrapperService.Main(String[] args)
很明显是权限问题,到这个目录:C:\Windows\System32,找到cmd.exe,右键->以管理员身份运行,再次执行,成功。1
2C:\nginx-1.12.0>winsw.exe install
2017-06-17 11:43:38,476 INFO - Installing the service with id 'nginx'
需要注意的是,在 windows8.1 /win10 环境下,执行 net stop nginx 也会有权限问题。1
2
3
4C:\Users\andy>net stop nginx
System error 5 has occurred.
Access is denied.
也需要以管理员身份运行 cmd.exe,推荐将 cmd.exe 添加一个快捷键到桌面或开始页(Start)。1
2
3
4
5
6
7C:\Windows\system32>net stop nginx
The nginx service is stopping.
The nginx service was stopped successfully.
C:\Windows\system32>net start nginx
The nginx service is starting.
The nginx service was started successfully.
- 注意,nginx默认配置端口为80,若本机80端口被其它程序占用,可能导致服务启动失败。
- 查看 windows 服务,将看到名为 nginx 的服务,其它操作如启动、重启、停止跟普通 windows 服务一样,在此不再赘述。
删除 nginx 服务
执行 winsw uninstall 或者 sc delete nginx,然后关闭所有 windows service 控制面板。
备注
- 按照参考链接里的配置文件配置,启动时报错,原因是他用的 winsw 版本跟我的不一样。
1
2
3
4
5
6
7
8
9
10
11<service>
<id>nginx</id>
<name>nginx</name>
<description>nginx</description>
<executable>c:/nginx-1.12.0/nginx.exe</executable>
<logpath>C:/nginx-1.12.0/logs</logpath>
<logmode>roll</logmode>
<depend></depend>
<startargument>-p C:/nginx-1.12.0</startargument>
<stopargument>-p C:/nginx-1.12.0 -s stop</stopargument>
</service>
如上配置,启动时报错如下:1
2
3
4
5
6
7
8
9C:\nginx-1.12.0>net start nginx
The nginx service is starting.
The nginx service could not be started.
A system error has occurred.
System error 1067 has occurred.
The process terminated unexpectedly.
端口被占用报错信息
1 | C:\nginx-1.12.0>nginx -t |
安装成服务后,nginx -s reload 不可用
暂时还没有解决。替代方案是,net stop nginx,然后 net start nginx
如果将服务停掉 net stop nginx,然后 nginx start
则可以正常使用 nginx -s reload
另外,发现一个现象,就是 nginx 作为服务启动之后,有时候修改了配置,net stop nginx 再 net start nginx 并没有使配置生效,到任务管理器查看,发现后台有很多个 nginx 进程,逐一结束掉,然后再 net start nginx,或者进入 nginx 目录, start nginx 之后,配置才会生效。
解决办法,呵呵,还未尝试,等空了再试,然后更新到这里。 todo ***1
2C:\nginx-1.12.0>nginx -s reload
nginx: [error] OpenEvent("Global\ngx_reload_2076") failed (5: Access is denied)
could not build server_names_hash
当我在本地配置了第 13 个网站的时候,以管理员身份先停止 nginx,再启动 nginx,启动时报了如下错误:1
2
3
4
5
6
7
8
9
10
11
12
13
14C:\nginx>net stop nginx
nginx 服务正在停止.
nginx 服务已成功停止。
C:\nginx>net start nginx
nginx 服务正在启动 .
nginx 服务无法启动。
系统出错。
发生系统错误 1067。
进程意外终止。
于是检查配置:1
2
3C:\nginx>nginx -t
nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
nginx: configuration file C:\nginx/conf/nginx.conf test failed
解决
nginx 根目录下,conf/nginx.conf,在 server {
与 }
中间添加一行:server_names_hash_bucket_size 32;
,如果再报这个错,则将 32 再增加到 32 的 N 倍,N 大于等于 2。1
2
3C:\nginx>nginx -t
nginx: the configuration file C:\nginx/conf/nginx.conf syntax is ok
nginx: configuration file C:\nginx/conf/nginx.conf test is successful