1
0

run-as-service.mdx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. ---
  2. title: "Run as service"
  3. description: "Configure Gogs to start automatically as a system service on Linux, macOS, and Windows"
  4. icon: "server"
  5. ---
  6. Running Gogs as a system service ensures it starts automatically on boot and restarts if it crashes. Choose the init system or service manager that matches your operating system.
  7. ## Linux
  8. <Tabs>
  9. <Tab title="Systemd">
  10. Systemd is the default init system on most modern Linux distributions (Ubuntu 16.04+, Debian 8+, CentOS 7+, Fedora, Arch Linux, etc.).
  11. Gogs ships with a [systemd service template](https://github.com/gogs/gogs/blob/main/scripts/systemd/gogs.service) at `scripts/systemd/gogs.service` in the installation directory.
  12. ### 1. Customize the service file
  13. Copy the template and edit it to match your installation:
  14. ```bash
  15. sudo cp /home/git/gogs/scripts/systemd/gogs.service /etc/systemd/system/gogs.service
  16. sudo vim /etc/systemd/system/gogs.service
  17. ```
  18. Here is the default service file for reference:
  19. ```ini
  20. [Unit]
  21. Description=Gogs
  22. After=network.target
  23. After=mariadb.service mysql.service mysqld.service postgresql.service memcached.service redis.service
  24. [Service]
  25. # Uncomment the following if you have repos with lots of files
  26. # and get an HTTP 500 error because of that
  27. #LimitMEMLOCK=infinity
  28. #LimitNOFILE=65535
  29. Type=simple
  30. User=git
  31. Group=git
  32. WorkingDirectory=/home/git/gogs
  33. ExecStart=/home/git/gogs/gogs web
  34. Restart=always
  35. RestartSec=2s
  36. Environment=USER=git HOME=/home/git
  37. # Hardening directives (comment out if not supported by your systemd version)
  38. ProtectSystem=full
  39. PrivateDevices=yes
  40. PrivateTmp=yes
  41. NoNewPrivileges=true
  42. [Install]
  43. WantedBy=multi-user.target
  44. ```
  45. Update the following values to match your environment:
  46. - `User` and `Group` -- the system user running Gogs
  47. - `WorkingDirectory` -- the Gogs installation directory
  48. - `ExecStart` -- the full path to the Gogs binary
  49. - `Environment` -- ensure `USER` and `HOME` match the Gogs user
  50. <Tip>
  51. If you use MySQL/MariaDB, PostgreSQL, Redis, or Memcached, the `After=` lines ensure Gogs starts after those services. Uncomment or add lines as needed for your database.
  52. </Tip>
  53. ### 2. Enable and start the service
  54. ```bash
  55. sudo systemctl enable gogs
  56. sudo systemctl start gogs
  57. ```
  58. ### 3. Verify the service
  59. ```bash
  60. # Check status
  61. sudo systemctl status gogs -l
  62. # View logs
  63. sudo journalctl -b -u gogs
  64. ```
  65. <Note>
  66. If you have repositories with a large number of files and encounter HTTP 500 errors, uncomment the `LimitMEMLOCK=infinity` and `LimitNOFILE=65535` directives in the service file.
  67. </Note>
  68. </Tab>
  69. <Tab title="init.d (Debian/Ubuntu)">
  70. For Debian-based systems that do not use systemd, Gogs provides an init.d script at `scripts/init/debian/gogs`.
  71. ### 1. Copy and customize the script
  72. ```bash
  73. sudo cp /home/git/gogs/scripts/init/debian/gogs /etc/init.d/gogs
  74. sudo chmod +x /etc/init.d/gogs
  75. sudo vim /etc/init.d/gogs
  76. ```
  77. Verify or update the following variables in the script:
  78. ```bash
  79. WORKINGDIR=/home/git/gogs
  80. DAEMON=$WORKINGDIR/$NAME
  81. DAEMON_ARGS="web"
  82. USER=git
  83. ```
  84. If you run Gogs alongside Nginx and PostgreSQL, update the init dependencies at the top of the script:
  85. ```bash
  86. # Required-Start: $syslog $network $local_fs nginx postgresql
  87. # Required-Stop: $syslog $local_fs
  88. ```
  89. ### 2. Register and start the service
  90. ```bash
  91. sudo update-rc.d gogs defaults 30 70
  92. sudo service gogs start
  93. ```
  94. ### 3. Verify
  95. Visit your Gogs URL in a browser to confirm it is running. You can also check:
  96. ```bash
  97. sudo service gogs status
  98. ```
  99. </Tab>
  100. <Tab title="init.d (CentOS/RHEL)">
  101. For CentOS/RHEL systems, Gogs provides an init.d script at `scripts/init/centos/gogs`.
  102. ### 1. Copy and customize the script
  103. ```bash
  104. sudo cp /home/git/gogs/scripts/init/centos/gogs /etc/rc.d/init.d/gogs
  105. sudo chmod +x /etc/rc.d/init.d/gogs
  106. sudo vim /etc/rc.d/init.d/gogs
  107. ```
  108. Verify or update the following variables:
  109. ```bash
  110. NAME=gogs
  111. GOGS_HOME=/home/git/gogs
  112. GOGS_PATH=${GOGS_HOME}/$NAME
  113. GOGS_USER=git
  114. LOGPATH=${GOGS_HOME}/log
  115. ```
  116. <Note>
  117. You can override these defaults by creating `/etc/sysconfig/gogs` with your custom values.
  118. </Note>
  119. ### 2. Register and start the service
  120. ```bash
  121. sudo chkconfig gogs on
  122. sudo service gogs start
  123. ```
  124. ### 3. Verify
  125. ```bash
  126. sudo service gogs status
  127. ```
  128. </Tab>
  129. <Tab title="OpenRC (Gentoo)">
  130. For Gentoo and other distributions using OpenRC, create a service script at `/etc/init.d/gogs`:
  131. ```bash
  132. #!/sbin/openrc-run
  133. description="Gogs - A painless self-hosted Git service"
  134. command="/home/git/gogs/gogs"
  135. command_args="web"
  136. command_user="git"
  137. command_background=true
  138. pidfile="/var/run/gogs.pid"
  139. directory="/home/git/gogs"
  140. depend() {
  141. need net
  142. after mysql postgresql
  143. }
  144. ```
  145. Make it executable and enable it:
  146. ```bash
  147. sudo chmod +x /etc/init.d/gogs
  148. sudo rc-update add gogs default
  149. sudo rc-service gogs start
  150. ```
  151. </Tab>
  152. </Tabs>
  153. ## Windows
  154. On Windows, Gogs can run as a native Windows service using either the builtin `minwinsvc` support or the third-party NSSM tool.
  155. <Tabs>
  156. <Tab title="Builtin (sc.exe)">
  157. Gogs binaries built with the `minwinsvc` tag (release archive name that contains `mws`) support running as a native Windows service.
  158. ### 1. Configure Gogs for Windows
  159. Edit `C:\gogs\custom\conf\app.ini`:
  160. ```ini
  161. RUN_USER = COMPUTERNAME$
  162. ```
  163. Replace `COMPUTERNAME` with the output of `echo %COMPUTERNAME%` in a command prompt. For example, if the computer name is `USER-PC`, set `RUN_USER = USER-PC$`.
  164. Configure the server section:
  165. ```ini
  166. [server]
  167. DOMAIN = gogs
  168. PROTOCOL = http
  169. HTTP_ADDR = 127.0.1.1
  170. HTTP_PORT = 80
  171. OFFLINE_MODE = true
  172. EXTERNAL_URL = http://gogs/
  173. ```
  174. <Tip>
  175. Using an address in the `127.x.x.x` range (other than `127.0.0.1`) prevents port conflicts with other local services while keeping traffic local. Any address from `127.0.0.2` to `127.254.254.254` works.
  176. </Tip>
  177. ### 2. Add a hosts entry
  178. Open Notepad as Administrator and edit `C:\Windows\System32\drivers\etc\hosts`:
  179. ```text
  180. # Gogs local HTTPd
  181. 127.0.1.1 gogs
  182. ```
  183. ### 3. Create and start the service
  184. Open a command prompt as Administrator:
  185. ```cmd
  186. sc create gogs start= auto binPath= "\"C:\gogs\gogs.exe\" web --config \"C:\gogs\custom\conf\app.ini\""
  187. ```
  188. <Warning>
  189. There must be a space after each `=` in the `sc create` command. This is a Windows `sc.exe` syntax requirement.
  190. </Warning>
  191. Start the service:
  192. ```cmd
  193. net start gogs
  194. ```
  195. You should see:
  196. ```text
  197. The gogs service is starting.
  198. The gogs service was started successfully.
  199. ```
  200. <Warning>
  201. Windows services run from `%WINDIR%\System32` by default. Always use **absolute paths** in `app.ini` for data directories and database files:
  202. ```ini
  203. [server]
  204. APP_DATA_PATH = c:/gogs/data
  205. [database]
  206. PATH = c:/gogs/data/gogs.db
  207. ```
  208. Use forward slashes (`/`) in paths to avoid escape character issues.
  209. </Warning>
  210. </Tab>
  211. <Tab title="NSSM">
  212. [NSSM (Non-Sucking Service Manager)](https://nssm.cc/) provides more control over service configuration and automatic restart behavior.
  213. ### 1. Install NSSM
  214. Download [nssm.exe](https://nssm.cc/download) for your architecture (32-bit or 64-bit) and place it in a directory on your `PATH`.
  215. ### 2. Install the Gogs service
  216. Open a command prompt as Administrator:
  217. ```cmd
  218. nssm install gogs
  219. ```
  220. The NSSM service installer GUI will appear. Configure the following tabs:
  221. **Application tab:**
  222. - **Path:** `C:\gogs\gogs.exe`
  223. - **Startup directory:** `C:\gogs`
  224. - **Arguments:** `web`
  225. **Details tab:**
  226. - **Display name:** `Gogs`
  227. - **Description:** `Gogs is a painless self-hosted Git service.`
  228. - **Startup type:** `Automatic (Delayed Start)`
  229. <Note>
  230. Delayed start means the service will begin approximately two minutes after non-delayed services, reducing impact on boot time.
  231. </Note>
  232. **I/O tab:**
  233. - **Output (stdout):** `C:\gogs\log\gogs-nssm.txt`
  234. - **Error (stderr):** `C:\gogs\log\gogs-nssm.txt`
  235. **File rotation tab:**
  236. - Check **Rotate files**
  237. - **Restrict rotation to files bigger than:** `1000000` bytes
  238. **Environment tab:**
  239. - **Environment variables:** `PATH=%PATH%;C:\gogs;C:\Program Files (x86)\Git\bin`
  240. <Tip>
  241. The environment variable ensures both `gogs.exe` and `git.exe` are available on the service's PATH at runtime.
  242. </Tip>
  243. Click **Install service** to complete the setup.
  244. ### 3. Start and manage the service
  245. ```cmd
  246. nssm start gogs
  247. ```
  248. You should see:
  249. ```text
  250. gogs: START: The operation completed successfully.
  251. ```
  252. Verify by checking `C:\gogs\log\gogs-nssm.txt` for Gogs startup output, ending with lines like:
  253. ```text
  254. [I] Run Mode: Production
  255. [I] Listen: http://127.0.1.1:80
  256. ```
  257. To restart after configuration changes:
  258. ```cmd
  259. nssm restart gogs
  260. ```
  261. NSSM will automatically attempt to restart Gogs if it crashes.
  262. </Tab>
  263. </Tabs>