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.

10
  • 1
    Apache is started when you start Ubuntu because it is listed as a startup application. Commented Jul 28, 2015 at 10:14
  • Kindly explain how this question is different from the one linked and then flag it for reopening. Commented Jul 28, 2015 at 10:14
  • 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? Commented Jul 28, 2015 at 10:49
  • 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. Commented Jul 28, 2015 at 15:36
  • 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. Commented Jul 30, 2015 at 12:33

1 Answer 1

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 <[email protected]>" 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.
2
  • For now this is a simplified version ;-) Commented Jul 31, 2015 at 7:44
  • Really very good explanation. Thanks @Rinzwind . Commented Aug 2, 2015 at 7:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.