16

This question already has an answer here:

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

marked as duplicate by RalfFriedl, Jeff Schaller, Thomas, schily, sebasth Sep 8 '18 at 13:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Is there something wrong with the nginx packages in Ubuntu? nginx-common seems to have a systemd unit file, too – ilkkachu May 30 '17 at 12:39
  • 1
    No there is nothing wrong. I just tried to compile nginx from source and want to be able to control it via sysctl. – user3448600 May 30 '17 at 12:41
  • @user3448600> even when compiling from source, you should normally build a package and dpkg --install that package. Should you do that, the correct place for service descriptions is /lib/systemd/system. – spectras May 30 '17 at 14:58
21

The recommended place is /etc/systemd/system/nginx.service

Then issue the command :

systemctl enable nginx

And finally

systemctl start nginx
  • 5
    systemctl daemon-reload – Dmitry Kudriavtsev May 30 '17 at 21:00
  • Daemon 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 7:09
  • It would just say service not found until you daemon reload – Dmitry Kudriavtsev May 31 '17 at 17:12
  • 2
    M. 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
14

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 there

  • Or 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
  • 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. – Patrick 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.