1. 程式人生 > >[樂意黎原創]Centos 7裡apache(httpd)自啟動

[樂意黎原創]Centos 7裡apache(httpd)自啟動

最近,Aerchi在折騰 ECS伺服器 (Centos 7),每次重啟後都要手動開啟apache服務,好繁瑣。

仔細研究了下:

Apache 的服務
第一、啟動、終止、重啟

systemctl start httpd.service #啟動

systemctl stop httpd.service #停止

systemctl restart httpd.service #重啟

第二、設定開機啟動/關閉

systemctl enable httpd.service #開機啟動

systemctl disable httpd.service #開機不啟動

第三、檢查httpd狀態

systemctl status httpd.service

需要將Apache註冊到Linux服務裡面啊!
註冊Apache到Linux服務
在Centos 7 安裝完Apache後,啟動關閉Apache可以通過如下命令實現: 

/usr/local/apache/bin/apachectl  start | stop | restart

如果想將httpd列入系統自動啟動的服務,可以直接將上述的apachectl檔案拷貝到 /etc/rc.d/init.d 中,然後在相應的啟動級別如3,5中加入連結即可。命令如下:

cp /usr/local/apache/bin/apachectl  /etc/rc.d/init.d/httpd     # 複製到init.d 並重命名為httpd

ln -s /etc/
rc.d/init.d/httpd /etc/rc.d/rc3.d/S61httpd

連結檔案的S61是啟動時的序號。當init.d目錄下有httpd指令碼後,我們就可以通過service命令來啟動關閉apache了。在任意路徑下執行:

service httpd start | stop | restart

這時有個問題就是:雖然apache已經可以自動啟動,但在linux的服務列表中卻看不到它,要新增服務,一般通過chkconfig --add xxx來實現,但需要指令碼中有相應的資訊才行,否則chkconfig就會提示:xxx 服務不支援 chkconfig。所以我們首先編輯httpd指令碼,在第2行(#!/bin/sh下面)新增如下注釋資訊(包括#):

# chkconfig: 35 61 61
# description: Apache

第一行的3個引數意義分別為:在哪些執行級別啟動httpd(3,5);啟動序號(S61);關閉序號(K61)。注意:第二行的描述必須要寫!
儲存後執行:

chkconfig --add httpd                    #所有開機模式下自啟動,另外chkconfig httpd on 表示345模式下自啟動

就將httpd新增入服務了。在rc3.d、rc5.d路徑中將來就會出現S61httpd的連結,其他執行級別路徑中會出現K61httpd的連結。

最後,修改 rc.local檔案新增自啟動

 vim /etc/rc.d/rc.local
新增如下:

13 touch /var/lock/subsys/local
 14 /etc/init.d/mysqld start
 15 #/usr/local/apache/bin/apachectl start
 16 #apachectl restart
 17 touch /usr/local/apache/bin/
 18 /usr/local/apache/bin/apachectl start

執行下面的命令檢視服務,就可以看到httpd的服務了。

chkconfig --list


systemctl list-unit-files   

------------------------------------------

樂意黎
2018-06-29