5

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.

  • 1
    Apache is started when you start Ubuntu because it is listed as a startup application. – Parto Jul 28 '15 at 10:14
  • Kindly explain how this question is different from the one linked and then flag it for reopening. – Parto Jul 28 '15 at 10:14
  • Do as in http://askubuntu.com/questions/389078/the-best-lamp-stack-with-a-gui-for-web-dev – Jaysheel Utekar Jul 28 '15 at 10:38
  • @Parto in the "Manually Start LAMP Server" question here described the way how to start LAMP manually and there are some commands provided for this . But still don't tell about how it works. – Rahul Satal Jul 28 '15 at 10:40
  • 1
    Ok @jaysheelutekar i agree there is answer that how can we start/stop the Apache and MySQL . But i never have to use the commands in my PC, Apache and MySQL starts automatically. And i just want to know Why i need not to start them on ubantu In-fact when i use WAMP in windows i always have to activate them? – Rahul Satal Jul 28 '15 at 10:49
  • @RahulSatal. If you disagree that this question is a duplicate, then (a) [edit] it to clarify the distance (which it looks like you've already done), and (b) flag it to request a reopening. However, it looks to me like you yourself marked the question as an dupe, so I'm confused about why you're now contesting that. – TRiG Jul 28 '15 at 13:54
  • 1
    You do not need to start apache cuz there is a startup script inside /etc/init.d/ that is set to start apache on boot. – Rinzwind Jul 28 '15 at 15:36
  • Thanks @Rinzwind for your answer, It means that apache is always active in background even when we don't want to use it. – Rahul Satal Jul 30 '15 at 11:51
  • 1
    Yes but that is the whole idea behind a website: you need a server to check every second for someone trying to access that website. – Rinzwind Jul 30 '15 at 12:33
  • @RahulSatal You can make Windows start daemons or "services", as they are called in Windows, on boot too. I am not sure if they can be Windows API programs though (if they start before CSRSS), but a "native" app would do. – user877329 Jul 31 '15 at 07:35

1 Answers1

8

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.
Fabby
  • 34,259
Rinzwind
  • 299,756