Ubuntu – Why don’t we need to start Apache server on Ubuntu as in Windows

Apache2lamp

I have LAMP server installed on my system and it is working perfectly. However, I am very curious to know why we need not start Apache server in LAMP. When we have WAMP on Windows, by contrast, we have to start it and activate Apache and MySQL. Does Apache start as we start Ubuntu (i.e., it always runs in the background) or does it start when we open localhost?

The answers to the question "Manually Start LAMP Server" describe how to start LAMP manually, but not how it works internally to start LAMP automatically.

Best Answer

Linux in general, so Ubuntu too, has directories where you can put scripts that start/stop/restart/reload a service (or whatever action this service can provide): /etc/init.d/ (=old but still used very often).

/etc/init.d is where all the traditional sysvinit scripts and the backward compatible scripts for upstart live. The backward compatible scripts basically run service myservice start instead of doing anything themselves. Some just show a notice to use the service command.

/etc/init.d$ ls
apache2             glibc.sh               mysql          screen-cleanup
apparmor            halt                   mysql-ndb      sendsigs
...

As an example, the start of the apache2 script (all the others will be similar in style):

$ more apache2
#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          apache2
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop apache2 web server
### END INIT INFO
#
# apache2               This init.d script is used to start apache2.
#                       It basically just calls apache2ctl.

ENV="env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin"

...


There is also /etc/init (=upstart):

/etc/init is where the upstart init configs live. While they are not scripts themselves, they essentially execute whatever is required to replace sysvinit scripts.

cups - CUPS Printing spooler and server

description     "CUPS printing spooler/server"
author          "Michael Sweet <msweet@apple.com>"

start on (filesystem
          and (started dbus or runlevel [2345]))
stop on runlevel [016]

respawn
respawn limit 3 12

So basically inside these scripts/configuration it is stated what other service needs to be started before this service can be started and what service needs to have stopped before this service can be stopped.

Does Apache start as we start Ubuntu (i.e., it always runs in the background) or does it start when we open localhost?

When you install a service like apache (or mysql (databaseserver) or cups (print server)) it generally also includes a startup script AND this is also activated (since the assumption is: if installed you want it running).

So the answer is: it is always running and not started when you access an URL (ie. http://localhost).

It is by the way also possible to stop a service, remove the auto-start from /etc/init.d/ and manually start that service.

There are 2 session managers that take care of this: old Ubuntu (ie. <15.10) uses upstart. New Ubuntu (>15.10) uses systemd.

  • Upstart would be service start apache2 or service stop apache2.
  • Systemd would be systemctl start apache2 or systemctl start apache2 but also supports the method Upstart uses on Debian/Ubuntu systems.
Related Question