使用 Windows Apache 转发系统服务搭建 Service Server

我们常常会写基于 C-S 的架构的程序, 一个服务程序监听系统的某个端口, 为客户端提供某些服务; 也有很多系统程序也以这种方式存在, 比如 RedmineGitosis. 我们有没有办法把这些程序托管到 apache 上, 让用户直接通过 Http 的方式访问呢? 其实很简单, 只需两步即可实现: 创建系统服务 + 代理转发

创建系统服务

Windows 一个很强大的服务管理程序, 命令很简单, sc. 常用的功能包括

sc start/stop/create/delete

两个 trick 需要注意

1. create的时候, binPath= 是一个整体, = 前不能有空格, 但是 = 后必须有空格

2. delete的时候, 如果 services.msc 打开着, 那么你需要把它关了才能够把服务真正删除掉

但是这么简单就可以把一个程序转成服务了么? 当然不是.

比如 Redmine, 在安装完之后, 可以调用 ruby script/server webrick -e production 来启动一个 StandAlone 的服务器进行测试. 这个程序一直在终端里运行, 直到用户 Ctrl+C 把它杀掉. 那么我们直接用这条命令作为 service的binPath, 这个服务可以运行么? 我们让一个服务启动时, 这个服务对应的命令应该是立即返回的, 只是它会触发另外一个过程来启动一个驻守进程(具体原理没有研究). 我们让服务调用这么一个命令, 因为它没有返回, 因此服务就会在那卡着等它结束, 因此结果就是服务因为长时间未响应而启动失败.

解决这个问题问题的方法来自于微软的一套系统管理程序,Windows Server 2003 Resource Kit Tools. 我们要用到的只有两个程序, instsrv.exe srvany.exe. 前者创建一个以后者为壳的服务.

然后打开注册表, 找到新创建的服务, 给它添加运行参数, 就是我们具体要运行的服务器程序.

具体可参考: http://www.redmine.org/boards/1/topics/4123

First, download the Windows NT Resource Kit: http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en and install it.

It should be noted that this is not added to your PATH so you will always need to specify absolute paths when running these commands. To add the registry entry for redmine we do:

path\INSTSRV.EXE My Service path\SRVANY.EXE

In my case it looked like this:

"C:\Program Files\Windows NT Resource Kit\INSTSRV.EXE" Redmine "C:\Program Files\Windows NT Resource Kit\SRVANY.EXE"

Then add the following registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Redmine

Now go to Start -> Run -> regedit. Right click on the registry key and select New -> Key. Name it "Parameters" without the quotes.

Now we need to add two values to the Parameters key. Right click on the parameters key, New -> String Value. Name it "Application". Now create another one named "AppParameters". Give them the following values:

For Application:

C:\ruby\bin\Ruby.exe

For AppParameters:

C:\RUBYAPP\script\server -e production

Where RUBYAPP is the directory that contains the redmine website.

Now you can go to Administrative Tools -> Services. There you can start the Redmine service and test whether or not it is working properly. It should be noted that the service will be marked as started prior to WEBrick finishing its boot procedure. You should give it 1min or so before trying to hit the service to verify that it is working correctly.

I hope this helps someone. Good luck.

 这样的话, 我们就确确实实安装了一个系统服务, 比如这个例子, 就是在3000端口建立的 Redmine 的服务.

代理转发

这个其实就非常简单了.只需要加上一个 ProxyPass即可.

ProxyPass /redmine http://127.0.0.1:3000/
ProxyPassReverse /redmine http://127.0.0.1:3000/

原文地址:https://www.cnblogs.com/dabaopku/p/2639397.html