0

I wanted to run Apache as a service so it will start on bootup, so I tried:

sudo systemctl start apache2.service

but got the error:

Failed to start apache2.service: Unit apache2.service not found.

So, apparently I need to install apache as a "service unit". The Apache documentation does not appear to have information on how to do this, just the old confusing init.d instructions which do not work anymore.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Tyler Durden
  • 5,631
  • 1
    Did you actually install apache2? – Nasir Riley Nov 15 '18 at 01:36
  • @NasirRiley Yes, works fine. I usually start it with apachectl but I would like to start it as a service now (or some kind of startup script) so that it starts automatically on boot. – Tyler Durden Nov 15 '18 at 01:43
  • What Debian is that? –  Nov 15 '18 at 01:48
  • 1
    What do you get from systemctl list-unit-files | grep apache? Also, did you install it via apt or did you compile it from source? – Nasir Riley Nov 15 '18 at 01:50
  • @NasirRiley I get nothing when I enter that. I built it from source, but added many features. Possibly I missed the one that automatically enables it to run as a service? – Tyler Durden Nov 15 '18 at 02:02

1 Answers1

2

Building apache2 from source does not create a service for it. That is why you are getting the error that the service isn't found.

You will either need to create one manually or install apache2 via apt. The latter is much easier and you can also install all of the features that you added. To get it to run at boot, you will need to use systemctl enable apache2 after creating the service or installing apache2 via apt.

Nasir Riley
  • 11,422
  • Ok, so how do I manually create a service unit for Apache? That is the question? – Tyler Durden Nov 15 '18 at 02:30
  • 2
    You could just copy the apache2.service file from the Debian source, replace the paths to represent your source install, and place it in /etc/systemd/system/. After systemctl daemon-reload it should be available – GracefulRestart Nov 15 '18 at 02:44
  • @NasirRiley That's what systemctl enable apache2.service does (assuming the [Install] section has been correctly configured.) I'd recommend enabling it through systemctl enable rather than creating symbolic links by hand. – filbranden Nov 15 '18 at 03:35
  • 1
    Building it from the Debian source does include the service units, as well as a number of patches that make Apache use Debian paths and whatnot. The problem here is likely to be building from the wrong source. – JdeBP Nov 15 '18 at 09:12
  • 1
    @FilipeBrandenburger You are correct. It even says so when you run systemctl enable service. I'll remove that comment since it's uneccessary redundant. – Nasir Riley Nov 15 '18 at 11:55
  • @JdeBP In this case, it does not create the service units. Even if it were built from Debian source, it would need to done as root to create the units in the correct location and load the daemon as a normal user wouldn't have write access to that location. – Nasir Riley Nov 15 '18 at 17:57
  • You have an incorrect notion of what building from a source package does. Building from the Debian source package will yield executables, service units, data files, doco, and maintainer scripts, all in the appropriate binary packages ready to be installed. It will include all of the Debian patches and additions (such as the very service unit files in question) that building from the wrong source does not. – JdeBP Nov 15 '18 at 18:14
  • @JdeBP I just said that. The issue is that it would need to be done as root. If the user is running as root then it would make more sense to just use apt anyway. Whether or not the source is wrong depends on what the user is trying to do. – Nasir Riley Nov 15 '18 at 19:46
  • No, you said some incorrect things about it not creating the service units, when it does, and the build process needing to be run as the super user, when it does not. And you've just repeated them. You have an incorrect notion of building from a source package. We already have Q&As from people doing this that demonstrate the right ideas, such as https://unix.stackexchange.com/questions/304590/ , https://unix.stackexchange.com/questions/143781/ , and https://unix.stackexchange.com/a/468218/5132 just for starters. I suggest reading and learning. – JdeBP Nov 15 '18 at 23:16
  • @JdeBP I said that compiling apache from source doesn't create the service which it doesn't. What you are referring to is completely different than what's being addressed in the question. He didn't compile what you linked to which is the Debian Apache source code which is acquired via apt for which he would indeed need to be root. He simply compiled the Apache source code which comes from here: https://httpd.apache.org/download.cgi . Nothing that I've stated here is incorrect. What you have stated doesn't apply to what's being addressed here. – Nasir Riley Nov 15 '18 at 23:27