`

Ubuntu下关闭apache和mysql的开机启动相关知识

 
阅读更多
操作系统:Ubuntu12.04
硬件环境:HP CQ45
当用户使用sudo apt-get install安装完apache和mysql之后,这些服务默认是开机启动的,但是有的时候需要关闭这些服务的开机启动。
首先普及一个概念,以下转自维基百科:

运行级别Runlevel)指的是Unix或者Linux等类Unix操作系统下不同的运行模式。运行级别通常分为7等,分别是从0到6,但如果必要的话也可以更多。

例如在大多数linux操作系统下一共有如下6个典型的运行级别:

  • 0 停机
  • 1 单用户,Does not configure network interfaces, start daemons, or allow non-root logins
  • 2 多用户,无网络连接 Does not configure network interfaces or start daemons
  • 3 多用户,启动网络连接 Starts the system normally.
  • 4 用户自定义
  • 5 多用户带图形界面
  • 6 重启

Debian Linux中2-5这四个运行级别都集中在级别2上。这个级别也是系统预设的正常运行级别。

(注:对于这些运行级别我们不必太过纠结与到底什么意思,只需要知道在Debian/Ubuntu(Ubuntu是Debian的分支)Linux中默认都是在级别(Runlevel)2启动的)

在Debian Linux中,下列路径对应不同的运行级别。当系统启动时,通过其中的脚本文件来启动相应的服务。
  • /etc/rc0.d Run level 0
  • /etc/rc1.d Run level 1
  • /etc/rc2.d Run level 2
  • /etc/rc3.d Run level 3
  • /etc/rc4.d Run level 4
  • /etc/rc5.d Run level 5
  • /etc/rc6.d Run level 6

下面我们去/etc/目录下寻找这些目录,在终端中输入:

hp@hp-Compaq-Presario-CQ45-Notebook-PC:~$ ls /etc/rc* /etc/rc.local /etc/rc0.d: K09apache2 K20winbind S30urandom S40umountfs K10unattended-upgrades README S31umountnfs.sh S60umountroot K20speech-dispatcher S20sendsigs S35networking S90halt /etc/rc1.d: K09apache2 K20kerneloops K20winbind S70dns-clean K15pulseaudio K20saned README S70pppd-dns K20acpi-support K20speech-dispatcher S30killprocs S90single /etc/rc2.d: README S50pulseaudio S70pppd-dns S99grub-common S20kerneloops S50rsync S75sudo S99ondemand S20speech-dispatcher S50saned S91apache2 S99rc.local S20winbind S70dns-clean S99acpi-support /etc/rc3.d: README S50pulseaudio S70pppd-dns S99grub-common S20kerneloops S50rsync S75sudo S99ondemand S20speech-dispatcher S50saned S91apache2 S99rc.local S20winbind S70dns-clean S99acpi-support /etc/rc4.d: README S50pulseaudio S70pppd-dns S99grub-common S20kerneloops S50rsync S75sudo S99ondemand S20speech-dispatcher S50saned S91apache2 S99rc.local S20winbind S70dns-clean S99acpi-support /etc/rc5.d: README S50pulseaudio S70pppd-dns S99grub-common S20kerneloops S50rsync S75sudo S99ondemand S20speech-dispatcher S50saned S91apache2 S99rc.local S20winbind S70dns-clean S99acpi-support /etc/rc6.d: K09apache2 K20winbind S30urandom S40umountfs K10unattended-upgrades README S31umountnfs.sh S60umountroot K20speech-dispatcher S20sendsigs S35networking S90reboot /etc/rcS.d: README S25brltty S37apparmor S55urandom S70x11-common

代码中带色彩并用下划线标注的内容是与apache服务有关的文件,可以看到在rc1.d rc6.d中为K09apache2,在rc2.d rc3.d rc4.d rc5.d中为S91apache2,下面简单介绍一下K09apache2与S91apache2的意思:

对于以K开头的文件,系统将终止对应的服务;

对于以S开头的文件,系统将启动对应的服务;

现在对于Debian/Ubuntu Linux的启动过程大家应该有所了解,总结一下:

在/etc/rc0.d~rc6.d目录下存放的分别是运行在0~6个运行级时要启动的服务有关的文件,由于Ubuntu默认是在runlevel 2启动的,那么我们之需要修改rc2.d中的文件,从而禁止apache服务启动。回顾刚才/etc/rc2.d目录下的文件可以发现与apache2有关的文件是S91apache2,而之前简单介绍过以S开头的文件,系统将启动对应的服务。所以我们要做的是把S91apache2修改成以K******,但是我们并不知道应该修改成多少,这里我们需要借助一个管理工具进行管理,即sysv-rc-conf。

在Ubuntu下用apt-get安装,非常简单

hp@hp-Compaq-Presario-CQ45-Notebook-PC:/etc$ sudo apt-get install sysv-rc-conf

接下来打开sysv-rc-conf

hp@hp-Compaq-Presario-CQ45-Notebook-PC:/etc$ sudo sysv-rc-conf

(注意:由于这里需要修改系统有关的内容,所以需要使用sudo,即root权限打开)

配置界面简洁明了:

第一行service后面是运行级别,有1,2,3,4,5,0,6,S,与之前介绍的运行级别基本符合,最后一项这里不做介绍

图中打X的即表示运行在对应Runlevel时开机启动的服务,这里我们可以看到apache2在Runlevel2,3,4,5均为开机启动,对应到之前/etc/rc2.d~/etc/rc5.d目录下均为S91apache2文件,再次验证了之前的内容,将光标移动到apache2那行的第2列,按空格进行将启动项反选

按q退出之后配置即完成。现在我们再来看一下/etc/rc2.d/目录下的内容:

hp@hp-Compaq-Presario-CQ45-Notebook-PC:/etc$ ls /etc/rc2.d/ K09apache2 S20winbind S70dns-clean S99grub-common README S50pulseaudio S70pppd-dns S99ondemand S20kerneloops S50rsync S75sudo S99rc.local S20speech-dispatcher S50saned S99acpi-support

现在已经出现了K09apache2文件,而之前的S91apache2文件不在了,到这里配置成功。

重启之后查看apache服务运行状态:

下面关于mysql的启动配置有所区别:

10.04以后把MySql的启动连接到了/lib/init/upstart-job,而后者是调用了/etc/init/mysql.conf文件里面关于开机的定义,这是新的框架,所以以前的方法不管用,我们需要修改mysql.conf文件,首先查看该文件:

hp@hp-Compaq-Presario-CQ45-Notebook-PC:/etc$ sudo vi /etc/init/mysql.conf

# MySQL Service description "MySQL Server" author "Mario Limonciello <superm1@ubuntu.com>" start on runlevel [2345] stop on starting rc RUNLEVEL=[016] respawn respawn limit 2 5

这里用红色粗体字标注出了运行级别,我们只需把该行修改成start on runlevel [345](注意这里需要sudo权限), 也就是删除了runlevel 2。

重启后查看mysql服务运行状态:


参考链接

http://space.itpub.net/370491/viewspace-216954

http://wowubuntu.com/ubuntu1004-close-mysql.html

http://zh.wikipedia.org/wiki/%E8%BF%90%E8%A1%8C%E7%BA%A7%E5%88%AB

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics