方法一:使用 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 地址。