Skip to content

Commit c999d98

Browse files
committed
Improve systemd configuration documentation
Closes gh-28453
1 parent f78dcac commit c999d98

File tree

3 files changed

+79
-77
lines changed

3 files changed

+79
-77
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,3 +1020,14 @@ web.servlet.spring-mvc.json=features.json.jackson.custom-serializers-and-deseria
10201020

10211021
# gh-32795
10221022
getting-started.first-application.code.enable-auto-configuration=getting-started.first-application.code.spring-boot-application
1023+
1024+
# gh-28453
1025+
deployment.installing.supported-operating-systems=deployment.installing
1026+
deployment.installing.nix-services=deployment.installing
1027+
deployment.installing.nix-services.init-d=deployment.installing.init-d
1028+
deployment.installing.nix-services.init-d.securing=deployment.installing.init-d.securing
1029+
deployment.installing.nix-services.system-d=deployment.installing.system-d
1030+
deployment.installing.nix-services.script-customization=deployment.installing.init-d.script-customization
1031+
deployment.installing.nix-services.script-customization.when-written=deployment.installing.init-d.script-customization.when-written
1032+
deployment.installing.nix-services.script-customization.when-running=deployment.installing.init-d.script-customization.when-running
1033+
deployment.installing.nix-services.script-customization.when-running.conf-file=deployment.installing.init-d.script-customization.when-running.conf-file

spring-boot-project/spring-boot-docs/src/docs/asciidoc/deployment/installing.adoc

Lines changed: 67 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,58 @@
11
[[deployment.installing]]
22
== Installing Spring Boot Applications
3-
In addition to running Spring Boot applications by using `java -jar`, it is also possible to make fully executable applications for Unix systems.
4-
A fully executable jar can be executed like any other executable binary or it can be <<deployment#deployment.installing.nix-services,registered with `init.d` or `systemd`>>.
5-
This helps when installing and managing Spring Boot applications in common production environments.
3+
In addition to running Spring Boot applications by using `java -jar` directly, it is also possible to run them as `systemd`, `init.d` or Windows services.
4+
5+
6+
7+
[[deployment.installing.system-d]]
8+
=== Installation as a systemd Service
9+
`systemd` is the successor of the System V init system and is now being used by many modern Linux distributions.
10+
Spring Boot applications can be launched by using `systemd` '`service`' scripts.
11+
12+
Assuming that you have a Spring Boot application packaged as an uber jar in `/var/myapp`, to install it as a `systemd` service, create a script named `myapp.service` and place it in `/etc/systemd/system` directory.
13+
The following script offers an example:
14+
15+
[indent=0]
16+
----
17+
[Unit]
18+
Description=myapp
19+
After=syslog.target network.target
20+
21+
[Service]
22+
User=myapp
23+
Group=myapp
24+
25+
Environment="JAVA_HOME=/path/to/java/home"
26+
27+
ExecStart=${JAVA_HOME}/bin/java -jar /var/myapp/myapp.jar
28+
ExecStop=/bin/kill -15 $MAINPID
29+
SuccessExitStatus=143
30+
31+
[Install]
32+
WantedBy=multi-user.target
33+
----
34+
35+
IMPORTANT: Remember to change the `Description`, `User`, `Group`, `Environment` and `ExecStart` fields for your application.
36+
37+
NOTE: The `ExecStart` field does not declare the script action command, which means that the `run` command is used by default.
38+
39+
The user that runs the application, the PID file, and the console log file are managed by `systemd` itself and therefore must be configured by using appropriate fields in the '`service`' script.
40+
Consult the https://www.freedesktop.org/software/systemd/man/systemd.service.html[service unit configuration man page] for more details.
41+
42+
To flag the application to start automatically on system boot, use the following command:
43+
44+
[source,shell,indent=0,subs="verbatim"]
45+
----
46+
$ systemctl enable myapp.service
47+
----
48+
49+
Run `man systemctl` for more details.
50+
51+
52+
53+
[[deployment.installing.init-d]]
54+
=== Installation as an init.d Service (System V)
55+
To use your application as `init.d` service, configure its build to produce a <<deployment#deployment.installing, fully executable jar>>.
656

757
CAUTION: Fully executable jars work by embedding an extra script at the front of the file.
858
Currently, some tools do not accept this format, so you may not always be able to use this technique.
@@ -35,30 +85,11 @@ The following example shows the equivalent Gradle configuration:
3585
}
3686
----
3787

38-
You can then run your application by typing `./my-application.jar` (where `my-application` is the name of your artifact).
39-
The directory containing the jar is used as your application's working directory.
40-
41-
88+
It can then be symlinked to `init.d` to support the standard `start`, `stop`, `restart`, and `status` commands.
4289

43-
[[deployment.installing.supported-operating-systems]]
44-
=== Supported Operating Systems
45-
The default script supports most Linux distributions and is tested on CentOS and Ubuntu.
46-
Other platforms, such as OS X and FreeBSD, require the use of a custom `embeddedLaunchScript`.
47-
48-
49-
50-
[[deployment.installing.nix-services]]
51-
=== Unix/Linux Services
52-
Spring Boot application can be easily started as Unix/Linux services by using either `init.d` or `systemd`.
53-
54-
55-
56-
[[deployment.installing.nix-services.init-d]]
57-
==== Installation as an init.d Service (System V)
58-
If you configured Spring Boot's Maven or Gradle plugin to generate a <<deployment#deployment.installing, fully executable jar>>, and you do not use a custom `embeddedLaunchScript`, your application can be used as an `init.d` service.
59-
To do so, symlink the jar to `init.d` to support the standard `start`, `stop`, `restart`, and `status` commands.
60-
61-
The script supports the following features:
90+
The default launch script that is added to a fully executable jar supports most Linux distributions and is tested on CentOS and Ubuntu.
91+
Other platforms, such as OS X and FreeBSD, require the use of a custom script.
92+
The default scripts supports the following features:
6293

6394
* Starts the services as the user that owns the jar file
6495
* Tracks the application's PID by using `/var/run/<appname>/<appname>.pid`
@@ -91,8 +122,8 @@ For example, on Debian, you could use the following command:
91122

92123

93124

94-
[[deployment.installing.nix-services.init-d.securing]]
95-
===== Securing an init.d Service
125+
[[deployment.installing.init-d.securing]]
126+
==== Securing an init.d Service
96127
NOTE: The following is a set of guidelines on how to secure a Spring Boot application that runs as an init.d service.
97128
It is not intended to be an exhaustive list of everything that should be done to harden an application and the environment in which it runs.
98129

@@ -130,7 +161,7 @@ One way to protect against this is to make it immutable by using `chattr`, as sh
130161

131162
This will prevent any user, including root, from modifying the jar.
132163

133-
If root is used to control the application's service and you <<deployment#deployment.installing.nix-services.script-customization.when-running.conf-file, use a `.conf` file>> to customize its startup, the `.conf` file is read and evaluated by the root user.
164+
If root is used to control the application's service and you <<deployment#deployment.installing.init-d.script-customization.when-running.conf-file, use a `.conf` file>> to customize its startup, the `.conf` file is read and evaluated by the root user.
134165
It should be secured accordingly.
135166
Use `chmod` so that the file can only be read by the owner and use `chown` to make root the owner, as shown in the following example:
136167

@@ -142,56 +173,15 @@ Use `chmod` so that the file can only be read by the owner and use `chown` to ma
142173

143174

144175

145-
[[deployment.installing.nix-services.system-d]]
146-
==== Installation as a systemd Service
147-
`systemd` is the successor of the System V init system and is now being used by many modern Linux distributions.
148-
Although you can continue to use `init.d` scripts with `systemd`, it is also possible to launch Spring Boot applications by using `systemd` '`service`' scripts.
149-
150-
Assuming that you have a Spring Boot application installed in `/var/myapp`, to install a Spring Boot application as a `systemd` service, create a script named `myapp.service` and place it in `/etc/systemd/system` directory.
151-
The following script offers an example:
152-
153-
[indent=0]
154-
----
155-
[Unit]
156-
Description=myapp
157-
After=syslog.target
158-
159-
[Service]
160-
User=myapp
161-
ExecStart=/var/myapp/myapp.jar
162-
SuccessExitStatus=143
163-
164-
[Install]
165-
WantedBy=multi-user.target
166-
----
167-
168-
IMPORTANT: Remember to change the `Description`, `User`, and `ExecStart` fields for your application.
169-
170-
NOTE: The `ExecStart` field does not declare the script action command, which means that the `run` command is used by default.
171-
172-
Note that, unlike when running as an `init.d` service, the user that runs the application, the PID file, and the console log file are managed by `systemd` itself and therefore must be configured by using appropriate fields in the '`service`' script.
173-
Consult the https://www.freedesktop.org/software/systemd/man/systemd.service.html[service unit configuration man page] for more details.
174-
175-
To flag the application to start automatically on system boot, use the following command:
176-
177-
[source,shell,indent=0,subs="verbatim"]
178-
----
179-
$ systemctl enable myapp.service
180-
----
181-
182-
Run `man systemctl` for more details.
183-
184-
185-
186-
[[deployment.installing.nix-services.script-customization]]
176+
[[deployment.installing.init-d.script-customization]]
187177
==== Customizing the Startup Script
188178
The default embedded startup script written by the Maven or Gradle plugin can be customized in a number of ways.
189179
For most people, using the default script along with a few customizations is usually enough.
190180
If you find you cannot customize something that you need to, use the `embeddedLaunchScript` option to write your own file entirely.
191181

192182

193183

194-
[[deployment.installing.nix-services.script-customization.when-written]]
184+
[[deployment.installing.init-d.script-customization.when-written]]
195185
===== Customizing the Start Script When It Is Written
196186
It often makes sense to customize elements of the start script as it is written into the jar file.
197187
For example, init.d scripts can provide a "`description`".
@@ -299,9 +289,9 @@ The following property substitutions are supported with the default script:
299289

300290

301291

302-
[[deployment.installing.nix-services.script-customization.when-running]]
292+
[[deployment.installing.init-d.script-customization.when-running]]
303293
===== Customizing a Script When It Runs
304-
For items of the script that need to be customized _after_ the jar has been written, you can use environment variables or a <<deployment#deployment.installing.nix-services.script-customization.when-running.conf-file, config file>>.
294+
For items of the script that need to be customized _after_ the jar has been written, you can use environment variables or a <<deployment#deployment.installing.init-d.script-customization.when-running.conf-file, config file>>.
305295

306296
The following environment properties are supported with the default script:
307297

@@ -364,7 +354,8 @@ See the https://www.freedesktop.org/software/systemd/man/systemd.service.html[se
364354

365355

366356

367-
[[deployment.installing.nix-services.script-customization.when-running.conf-file]]
357+
[[deployment.installing.init-d.script-customization.when-running.conf-file]]
358+
====== Using a Conf Gile
368359
With the exception of `JARFILE` and `APP_NAME`, the settings listed in the preceding section can be configured by using a `.conf` file.
369360
The file is expected to be next to the jar file and have the same name but suffixed with `.conf` rather than `.jar`.
370361
For example, a jar named `/var/myapp/myapp.jar` uses the configuration file named `/var/myapp/myapp.conf`, as shown in the following example:
@@ -378,7 +369,7 @@ For example, a jar named `/var/myapp/myapp.jar` uses the configuration file name
378369

379370
TIP: If you do not like having the config file next to the jar file, you can set a `CONF_FOLDER` environment variable to customize the location of the config file.
380371

381-
To learn about securing this file appropriately, see <<deployment#deployment.installing.nix-services.init-d.securing,the guidelines for securing an init.d service>>.
372+
To learn about securing this file appropriately, see <<deployment#deployment.installing.init-d.securing,the guidelines for securing an init.d service>>.
382373

383374

384375

spring-boot-project/spring-boot-docs/src/docs/asciidoc/documentation/advanced.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
== Advanced Topics
33
Finally, we have a few topics for more advanced users:
44

5-
* *Spring Boot Applications Deployment:* <<deployment#deployment.cloud, Cloud Deployment>> | <<deployment#deployment.installing.nix-services, OS Service>>
5+
* *Spring Boot Applications Deployment:* <<deployment#deployment.cloud, Cloud Deployment>> | <<deployment#deployment.installing, OS Service>>
66
* *Build tool plugins:* <<build-tool-plugins#build-tool-plugins.maven, Maven>> | <<build-tool-plugins#build-tool-plugins.gradle, Gradle>>
77
* *Appendix:* <<application-properties#appendix.application-properties,Application Properties>> | <<configuration-metadata#appendix.configuration-metadata,Configuration Metadata>> | <<auto-configuration-classes#appendix.auto-configuration-classes,Auto-configuration Classes>> | <<test-auto-configuration#appendix.test-auto-configuration,Test Auto-configuration Annotations>> | <<executable-jar#appendix.executable-jar,Executable Jars>> | <<dependency-versions#appendix.dependency-versions,Dependency Versions>>

0 commit comments

Comments
 (0)