前言
系统环境:CentOS7.9
服务:Apache
其中Apache安装目录为/usr/local/apache/
因为Apache是源码编译安装的,不是yum安装的,所以使用不了systemctl start httpd
这样的方式启动,平时都是 /usr/local/apache/bin/apachectl -k start
这样启动的 ,现在想把Apache添加到系统服务中,以后想使用service httpd start
这样启动。
Linux中把程序添加到系统服务中
复制Apache的启动文件到/etc/init.d/
下
cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
(复制到 /etc/rc.d/init.d/
下也可以,因为 /etc/init.d
是/etc/rc.d/init.d
的软链接)
cd /etc/init.d/
ll httpd #查看是否有执行权限,没有就chmod +x /etc/init.d/apache
chkconfig -add httpd #使用chkconfig 命令将httpd做成系统服务
报错了,如下:
vim /etc/init.d/httpd 在文件前面加入下面两句:
# chkconfig: 345 85 15
# description: Apache is a World Wide Web server
注意两句开头是有#号的,(chkconfig 是什么命令,为什么这样写,看我另一篇文章)
chkconfig --add httpd #不报错了
chkconfig --list httpd #查看是否添加成功
httpd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
表明httpd 服务已经生效,在3、4、5运行级别随系统启动而自动启动,以后可以使用service命令控制Apache的启动和停止。
service httpd start #使用service 启动httpd服务
lsof -i :80 #80端口以打开,httpd服务正常启动
service httpd stop #使用service 停止httpd服务
lsof -i :80 #80端口以打开,httpd服务正常停止
总结:
把要注册成服务的程序 复制到 /etc/init.d/ 或 /etc/rc.d/init.d/
目录下,其实前者是后者的软链接
cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
cd /etc/init.d/
chkconfig --add httpd #报错service httpd does not support chkconfig,
vim httpd 添加下面两句在文件的开头位置,注意有#号
# chkconfig: 345 85 15
# description: Apache is a World Wide Web server
chkconfig --add httpd #添加系统服务
chkconfig --list httpd #查看系统服务里是否已成功有httpd服务
service httpd start|stop #使用service命令管理服务