前提要求
想要使 Gogs 通过 Windows 服务的方式运行,必须满足以下两个条件:
miniwinsvcminiwinsvc
[repository] ROOT
C:\Gogs\custom\conf\app.ini
RUN_USER = COMPUTERNAME$
COMPUTERNAMEecho %COMPUTERNAME%USER-PCRUN_USER = USER-PC$
[server]DOMAIN = gogsPROTOCOL = httpHTTP_ADDR = 127.0.1.1HTTP_PORT = 80OFFLINE_MODE = trueROOT_URL = http://gogs/
127.0.0.2 - 127.254.254.254
C:\Windows\System32\drivers\etc\hosts
# Gogs local HTTPd127.0.1.1 gogs
gogs/
Use Builtin Functionality
cmd.exe
C:\> sc create gogs start= auto binPath= "\"C:\gogs\gogs.exe\" web --config \"C:\gogs\custom\conf\app.ini\""
=
To start the service run following command:
C:\> net start gogs
You should see following output:
The gogs service is starting.The gogs service was started successfully.
Use NSSM
%PATH%
Open a command line as administrator and do following command to configure Gogs as a service:
C:\>nssm install gogs
“NSSM service installer” will appear. Configure it as follows:
Application tab:
C:\gogs\gogs.exeC:\gogsweb
Details tab:
GogsA painless self-hosted Git service.Automatic (Delayed Start)
Note that we’ve chosen delayed start, so that the service will not impact the early boot time. Gogs will start two minutes after the non-delayed services.
I/O tab:
C:\gogs\log\gogs-nssm.txtC:\gogs\log\gogs-nssm.txt
That will capture all text output that you would normally receive from Gogs on the command line console, and log it to that file instead.
File rotation tab:
Rotate files1000000 bytes
Environment tab:
PATH=%PATH%;C:\gogs;C:\Program Files (x86)\Git\bin
gogs.exegit.exe
Click “Install service”, and you should be confirmed that it succeeded.
If it failed, refer back to the command line console you started, for the error message. When it succeeds, go to command line and do:
nssm start gogs
You should see:
gogs: START: The operation completed successfully.
C:\gogs\log\gogs-nssm.txt
timestamp [I] Run Mode: Productiontimestamp [I] Listen: http://127.0.1.1:80
http://gogs/
Gogs is running as a service, and will not need to be run manually, unless something goes wrong. NSSM will attempt to restart the service for you, if the Gogs server crashes.
app.ini
nssm restart gogs
custom\conf\app.ini
An example of the logging and error handling in action:
[log]ROOT_PATH = C:\gogs\log
C:\gogs\log\gogs-nssm.txt
timestamp [T] Custom path: C:/gogs/customtimestamp [T] Log path: C:\gogs\logtimestamp [I] Gogs x.y.ztimestamp [log.go:294 Error()] [E] Fail to set logger(file): invalid character 'g' in string escape code
C:\custom\conf\app.ini
ROOT_PATH = C:/gogs/log
nssm
C:\>nssm restart gogsgogs: STOP: The operation completed successfully.gogs: Unexpected status SERVICE_PAUSED in response to START control.C:\>nssm start gogsgogs: START: An instance of the service is already running.C:\>nssm stop gogsgogs: STOP: The operation completed successfully.C:\>nssm start gogsgogs: Unexpected status SERVICE_PAUSED in response to START control.C:\>nssm restart gogsgogs: STOP: The operation completed successfully.gogs: START: The operation completed successfully.