方法一:使用 systemd 服務文件
安裝所需依賴
yum install gcc make apr-devel apr-util-devel pcre-devel
1.下載源碼包
wget http://archive.apache.org/dist/httpd/httpd-2.4.62.tar.gz
2.解壓源碼
tar -xf httpd-2.4.62.tar.gz
cd httpd-2.4.62
3.編譯安裝
指定安裝目錄
./configure --prefix=/usr/local/apache?
編譯
make
make install
4.創建 systemd 服務文件
創建 /usr/lib/systemd/system/httpd.service 文件,并添加以下內容:
[Unit]
Description=The Apache HTTP Server
After=network.target[Service]
Type=forking
PIDFile=/usr/local/apache/logs/httpd.pid
ExecStart=/usr/local/apache/bin/httpd -k start
ExecReload=/usr/local/apache/bin/httpd -k restart
ExecStop=/usr/local/apache/bin/httpd -k stop
PrivateTmp=true[Install]
WantedBy=multi-user.target
5.啟動服務
systemctl daemon-reload
systemctl start httpd
systemctl enable httpd
6.測試
打開網頁瀏覽器,在地址欄中輸入服務器的 IP 地址。
方法二:使用 init.d 腳本
安裝所需依賴
yum install gcc make apr-devel apr-util-devel pcre-devel
1.下載源碼包
wget http://archive.apache.org/dist/httpd/httpd-2.4.62.tar.gz
2.解壓源碼
tar -xf httpd-2.4.62.tar.gz
cd httpd-2.4.62
3.編譯安裝
指定安裝目錄
./configure --prefix=/usr/local/apache?
編譯
make
make install
4.創建 init.d 腳本
進入?/usr/local/apache /bin/目錄,打開apachectl
cd /usr/local/apache /bin/
vim apachectl
拷貝
cp apachectl /etc/init.d/httpd
編輯文件,添加如下兩行代碼
# chkconfig: ? - 85 15
#description:? Apache?is an HTTP(S) server
添加運行級別,并設置自啟動
?chkconfig --level 35 --add httpd
?chkconfig --level 35 ?httpd on
5.啟動服務
systemctl start httpd
systemctl status httpd
6.測試
打開網頁瀏覽器,在地址欄中輸入服務器的 IP 地址。