编译安装apache实现自动启动

编译安装apache之后要想启动我们可以用命令:路径/apachectl start 如果要开机启动我们可以把这条命令放在/etc/rc.d/rc.local下,但如果我们要像启动其它服务一样用service 服务名 start|stop|status一样启动,停止,查看状态;让chkconfig来安排系统在某个级别来启动或者是关闭某个服务,我们就要做一些简单的操作了,
 
首先我们
[root@apple ~]# ls /lamp/apache/bin/apachectl
/lamp/apache/bin/apachectl
查到到编译安装过的apache启动脚本文件的位置,然后我们尝试着去启动他
[root@apple ~]# /lamp/apache/bin/apachectl start         -------这是正确的启动方法
[root@apple ~]# service apachectl start
apachectl: 未被识别的服务
[root@apple ~]# chkconfig --list apachectl
在 apachectl 服务中读取信息时出错:没有那个文件或目录
[root@apple ~]#
如果我们要想实现用chkconfig实现在某个级别自动启动我们要做如下操作。
1:复制编译安装的启动脚本到/etc/init.d目录下
[root@apple ~]# cp /lamp/apache/bin/apachectl /etc/init.d/
[root@apple ~]# ll /etc/init.d/apachectl -i
518664 -rwxr-xr-x 1 root root 3414 11-26 19:18 /etc/init.d/apachectl
[root@apple ~]# ll /etc/rc.d/init.d/apachectl -i
518664 -rwxr-xr-x 1 root root 3414 11-26 19:18 /etc/rc.d/init.d/apachectl
我们把文件复制到/etc/init.d之后我们会发现系统自动会在/etc/rc.d/init.d目录下创建该文件的硬链接文件。
注意:节点号相同
2:修改apache启动脚本文件的内容
要在配置文件中加入让chkconfig在哪个级别启动这个服务以及启动服务和关闭服务的顺序和描述信息。格式如下
[root@apple ~]# vi /etc/init.d/apachectl
#!/bin/sh
#
#chkconfig:35 81 91                     ------ 新添加行
#description:this is a apache server          ------ 新添加行
# Licensed to the Apache Software Foundation (ASF) under one or more
注:其中:35为系统进入的级别,81为系统第几个启动的服务,91为系统第几个关闭的服务。 像81和91 这些数字我们要注意不要跟系统已经存在的启动项冲突!
3:让chkconfig包含apachectl服务
[root@apple ~]# chkconfig --add apachectl
 
然后我们就可以查看了,
[root@apple ~]# chkconfig --list apachectl
apachectl       0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:启用 6:关闭
我们在/etc/rc.d/rc3.d和/etc/rc.d/rc5.d这两个目录下我们可以查看到
[root@apple ~]# ll /etc/rc.d/rc3.d/S81apachectl
lrwxrwxrwx 1 root root 19 11-26 19:28 /etc/rc.d/rc3.d/S81apachectl -> ../init.d/apachectl
[root@apple ~]# ll /etc/rc.d/rc5.d/S81apachectl
lrwxrwxrwx 1 root root 19 11-26 19:28 /etc/rc.d/rc5.d/S81apachectl -> ../init.d/apachectl
[root@apple ~]#
然后我们就可以用chkconfig 的来安排系统启动哪个级别来启动apache服务了
[root@apple ~]# chkconfig --level 345 apachectl on
[root@apple ~]# chkconfig --list apachectl
apachectl       0:关闭 1:关闭 2:关闭 3:启用 4:启用 5:启用 6:关闭
[root@apple ~]# chkconfig --level 345 apachectl off
[root@apple ~]# chkconfig --list apachectl
apachectl       0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
[root@apple ~]#