Where should I put systemd file for e.g. Nginx nginx.service
or something like that on Ubuntu 16.04 ?

- 56,709
- 26
- 150
- 232

- 635
2 Answers
The recommended place is /etc/systemd/system/nginx.service
Then issue the command :
systemctl enable nginx
And finally
systemctl start nginx

- 138,973

- 396
- 4
- 3
-
7
-
1Daemon reload is only need when you change a file that is already in use. By enabling the service nginx, there is no need for daemon-reload. – Bruno Mairlot May 31 '17 at 07:09
-
-
3M. Kudriavtsev is wrong.
systemctl enable
enacts a reload, as its manual page does say. An explicit reload is not necessary. – JdeBP Jun 23 '17 at 19:39
Usually, in Ubuntu, the package provided unit files go in /lib/systemd/system/
directory e.g. /lib/systemd/system/nginx.service
whereas the user provided or any modification to the package provided unit file(s) go in /etc/systemd/system/
directory.
Now, to override, you can:
Name the unit file as the package provided one e.g. to override
/lib/systemd/system/nginx.service
, you can create the file/etc/systemd/system/nginx.service
and put the directives in thereOr you can create a drop-in snippet ending in
.conf
in a directory that is named after the original unit file with.d
appended to the directory name e.g. you can create a file named/etc/systemd/system/nginx.service.d/override.conf
and put statements in there
Now, you can leverage the systemctl edit
command to do all the manual stuffs for you i.e. it will create the necessary override file for you (at first it will create a temporary file (with intermediate directory(ies) for drop-in snippets), and upon saving it will commit and rename()
the temp file to desired final file), and you just need to edit the file to put in your overrides.
For example, to create an override snippet for ngnx.service
(/lib/systemd/system/nginx.service
) mentioned above:
systemctl edit nginx.service
By default, it will use the file /etc/systemd/system/nginx.service.d/override.conf
. If --full
is used then the full replacement file /etc/systemd/system/nginx.service
will be used (the original content from /lib/systemd/system/nginx.service
will be copied to this file).
You can also use --runtime
to create the snippet in /run/systemd/system/
(at first drop-in snippet, then whole so --runtime
and --full
are not mutually-exclusive) that will be temporary of course.
You can obviously choose the editor to use, the order of precedence is:
$SYSTEMD_EDITOR > $EDITOR > $VISUAL > editor > nano > vim > vi

- 56,300
-
Is there a reason you reverted my revision? The edit fixes inaccurate information in your answer. I even provided you with documentation supporting the changes. – phemmer May 30 '17 at 21:37
-
@Patrick sorry about that. Ubuntu doesn't comply with the locations, well, not to the extent that would be helpful to end users...also,
/lib/
(and/usr/lib/
) are not symlinks in Ubuntu... – heemayl May 30 '17 at 21:47
nginx-common
seems to have a systemd unit file, too – ilkkachu May 30 '17 at 12:39dpkg --install
that package. Should you do that, the correct place for service descriptions is/lib/systemd/system
. – spectras May 30 '17 at 14:58