方法一:使用源码安装apache的httpd,并systemctl 管理
(1)可以使用官网下载httpd的源文件Download - The Apache HTTP Server Project

(2)下载后使用Xftp上传至Linux终端

(3)解压
[root@gxy ~]# tar -xzf httpd-2.4.62.tar.gz
(4)进行源码编译
[root@gxy ~]# cd httpd-2.4.62/ #跳转至解压目录下
[root@gxy httpd-2.4.62]# ./configure --prefix=/usr/local/apache #执行源码编译测试

总结信息总会告知版本、安装路劲、编译语言。也会告知缺少依赖包,提示缺少依赖包,则选择安装依赖包。
(5)测试
[root@gxy httpd-2.4.62]# make


在测试总也会提示缺少依赖,同理。
(6)源码安装
[root@gxy httpd-2.4.62]# make install

(7)创建服务器
[root@gxy httpd-2.4.62]# vim /etc/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
正确的服务文件:确保你已经创建了正确的 systemd 服务文件,并且它位于 /etc/systemd/system/ 目录下。

(8)重启服务
[root@gxy httpd-2.4.62]# systemctl daemon-reload #守护程序重新加载
[root@gxy httpd-2.4.62]# systemctl start httpd
[root@gxy httpd-2.4.62]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /etc/systemd/system/httpd.service.
(9)在防火墙上放行80端口,仅供测试使用
[root@gxy httpd-2.4.62]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@gxy httpd-2.4.62]# iptables -nL

(10)测试
1、在linux端测试,使用curl指令
[root@gxy httpd-2.4.62]# curl http://192.168.88.4
2、在windows端使用浏览器进行测试

方法二: 使用 init.d 脚本
(1)下载源码 使用官网下载httpd的源文件Download - The Apache HTTP Server Project
(2)使用Xftp上传至Linux


(3)解压缩
[root@gxy ~]# tar -zxf httpd-2.4.62.tar.gz
(4)执行编译文件
[root@localhost httpd-2.4.62]# ./configure --prefix=/usr/local/apache

缺少依赖,则安装依赖
[root@localhost httpd-2.4.62]# yum -y install gcc #gcc编译语言
[root@localhost httpd-2.4.62]# yum -y install make #编译测试
[root@localhost httpd-2.4.62]# yum -y install autoconf
[root@localhost httpd-2.4.62]# yum -y install apr-devel #APR环境
[root@localhost httpd-2.4.62]# yum -y install apr-util-devel
[root@localhost httpd-2.4.62]# yum -y install pcre-devel
再次执行
[root@localhost httpd-2.4.62]# ./configure --prefix=/usr/local/apache

(5)make测试


(6)make install 下载源码编译


(7) 创建init.d/httpd执行脚本
[root@localhost httpd-2.4.62]# cd /usr/local/apache/
[root@localhost apache]# cd bin/
[root@localhost bin]# vim apachectl #apache自带源码,将源码移动至/etc/init.d/httpd路径下

[root@localhost bin]# cp apachectl /etc/init.d/httpd
添加启动级别,服务类型
脚本位置:确保你的 init.d 脚本位于 /etc/init.d/ 目录下。
脚本权限:确保脚本具有执行权限:sudo chmod +x /etc/init.d/httpd

httpd脚本内容
#!/bin/sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#chkconfig: 35 80 10
#description: Apache is an HTTP(S) server
#
#
# Apache control script designed to allow an easy command line interface
# to controlling Apache. Written by Marc Slemko, 1997/08/23
#
# The exit codes returned are:
# XXX this doc is no longer correct now that the interesting
# XXX functions are handled by httpd
# 0 - operation completed successfully
# 1 -
# 2 - usage error
# 3 - httpd could not be started
# 4 - httpd could not be stopped
# 5 - httpd could not be started during a restart
# 6 - httpd could not be restarted during a restart
# 7 - httpd could not be restarted during a graceful restart
# 8 - configuration syntax error
#
# When multiple arguments are given, only the error from the _last_
# one is reported. Run "apachectl help" for usage info
#
ACMD="$1"
ARGV="$@"
#
# |||||||||||||||||||| START CONFIGURATION SECTION ||||||||||||||||||||
# -------------------- --------------------
#
# the path to your httpd binary, including options if necessary
HTTPD='/usr/local/apache/bin/httpd'
#
# pick up any necessary environment variables
if test -f /usr/local/apache/bin/envvars; then
. /usr/local/apache/bin/envvars
fi
#
# a command that outputs a formatted text version of the HTML at the
# url given on the command line. Designed for lynx, however other
# programs may work.
LYNX="lynx -dump"
#
# the URL to your server's mod_status status page. If you do not
# have one, then status and fullstatus will not work.
STATUSURL="http://localhost:80/server-status"
#
# Set this variable to a command that increases the maximum
# number of file descriptors allowed per child process. This is
# critical for configurations that use many file descriptors,
# such as mass vhosting, or a multithreaded server.
ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`"
# -------------------- --------------------
# |||||||||||||||||||| END CONFIGURATION SECTION ||||||||||||||||||||# Set the maximum number of file descriptors allowed per child process.
if [ "x$ULIMIT_MAX_FILES" != "x" ] ; then
$ULIMIT_MAX_FILES
fiERROR=0
if [ "x$ARGV" = "x" ] ; then
ARGV="-h"
ficase $ACMD in
start|stop|restart|graceful|graceful-stop)
$HTTPD -k $ARGV
ERROR=$?
;;
startssl|sslstart|start-SSL)
echo The startssl option is no longer supported.
echo Please edit httpd.conf to include the SSL configuration settings
echo and then use "apachectl start".
ERROR=2
;;
configtest)
$HTTPD -t
ERROR=$?
;;
status)
$LYNX $STATUSURL | awk ' /process$/ { print; exit } { print } '
;;
fullstatus)
$LYNX $STATUSURL
;;
*)
$HTTPD "$@"
ERROR=$?
esac
(8)关闭防火墙
[root@localhost bin]# systemctl stop firewalld.service
(9)重启服务
[root@localhost init.d]# /usr/lib/systemd/systemd-sysv-install enable httpd
(10)测试
1.windows浏览器测试
2.linux的curl测试

总结:
1.无论使用 Systemd 还是 init.d 管理 httpd 服务,都要确保服务文件或脚本的内容准确,包括命令路径、服务依赖关系、权限设置等。
2.对于服务的启动和运行,要考虑不同系统环境和服务的特性,根据实际情况调整服务的启动顺序和运行级别。
4.在对服务进行修改或更新后,要进行充分的测试,确保服务的稳定性和可靠性。
5.在管理 Apache 服务时,无论使用哪种方法,都应该确保你了解如何检查服务状态、如何启动和停止服务、以及如何在配置更改后重启服务。此外,始终确保在进行任何更改后测试服务以验证其正常运行。
1334

被折叠的 条评论
为什么被折叠?



