我的系统上安装了 LAMP 服务器,并且运行良好。但是,我很好奇为什么我们不需要在 LAMP 中启动 Apache 服务器。相比之下,当我们在 Windows 上安装 WAMP 时,我们必须启动它并激活 Apache 和 MySQL。Apache 是在我们启动 Ubuntu 时启动(即它总是在后台运行)还是在我们打开 localhost 时启动?
问题“手动启动 LAMP 服务器”的答案描述了如何手动启动 LAMP,但没有描述如何在内部自动启动 LAMP。
总的来说,Linux 和 Ubuntu 都有目录,您可以在其中放置启动/停止/重新启动/重新加载服务的脚本(或该服务可以提供的任何操作):/etc/init.d/(=旧但仍然经常使用)。
/etc/init.d 是所有传统的 sysvinit 脚本和 upstart 的向后兼容脚本的所在。向后兼容的脚本基本上运行
service myservice start而不是自己做任何事情。有些只是显示使用service命令的通知。
/etc/init.d$ ls
apache2 glibc.sh mysql screen-cleanup
apparmor halt mysql-ndb sendsigs
...
Run Code Online (Sandbox Code Playgroud)
例如,apache2脚本的开头(所有其他脚本的风格都相似):
$ 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"
Run Code Online (Sandbox Code Playgroud)
...
还有/etc/init(=新贵):
/etc/init 是 upstart init 配置所在的地方。虽然它们本身不是脚本,但它们本质上执行替换 sysvinit 脚本所需的任何内容。
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
Run Code Online (Sandbox Code Playgroud)
因此,基本上在这些脚本/配置中,说明了在启动此服务之前需要启动哪些其他服务以及在停止此服务之前需要停止哪些服务。
Apache 是在我们启动 Ubuntu 时启动(即它总是在后台运行)还是在我们打开 localhost 时启动?
当您安装像 apache(或 mysql(数据库服务器)或cups(打印服务器))这样的服务时,它通常还包括一个启动脚本,并且这也被激活(因为假设是:如果安装了,您希望它运行)。
所以答案是:当你访问一个 URL(即http://localhost)时,它总是在运行而不是启动。
顺便说一句,也可以停止服务,删除自动启动/etc/init.d/并手动启动该服务。
有 2 个会话管理器可以解决这个问题:旧的 Ubuntu(即 <15.10)使用upstart. 新的 Ubuntu (>15.10) 使用systemd.
service start apache2或service stop apache2。systemctl start apache2或systemctl start apache2但也支持 Upstart 在 Debian/Ubuntu 系统上使用的方法。