CentOS 安装与配置 Apache HTTP Server 2.4

简介
Apache HTTP Server 是一个开源的HTTP服务器软件,它以高性能、稳定性以及可扩展性著称,本文将介绍如何在CentOS系统上使用yum命令安装和配置Apache HTTP Server 2.4版本。
安装 Apache HTTP Server 2.4
检查 yum 源 在安装之前,确保你的系统已经配置了正确的 yum 源,以下是添加 EPEL (Extra Packages for Enterprise Linux) 仓库的命令:
sudo yum install epel-release
安装 Apache HTTP Server 使用以下命令安装 Apache HTTP Server:
sudo yum install httpd
启动 Apache 服务 安装完成后,启动 Apache 服务:
sudo systemctl start httpd
检查服务状态 你可以使用以下命令检查 Apache 服务的状态:
sudo systemctl status httpd
设置 Apache 服务开机自启 为了在系统启动时自动启动 Apache 服务,使用以下命令:
sudo systemctl enable httpd
配置 Apache HTTP Server

访问 Apache 默认页面 在浏览器中输入服务器的IP地址或域名,你应该能看到Apache的默认欢迎页面。
配置文件路径 Apache 的配置文件位于
/etc/httpd/目录下,主要的配置文件是httpd.conf。修改默认文档根目录 默认情况下,Apache 将
htdocs目录作为网站根目录,你可以通过编辑httpd.conf文件来修改这个目录:
sudo nano /etc/httpd/conf/httpd.conf
找到 DocumentRoot 这一行,将其值修改为你想要用作网站根目录的路径,
DocumentRoot "/var/www/html"
- 设置用户和组 默认情况下,Apache 以
apache用户和组运行,如果你需要更改这些设置,找到以下行:
User "apache" Group "apache"
重启 Apache 服务 修改配置文件后,重启 Apache 服务以应用更改:
sudo systemctl restart httpd
安全配置
- 限制直接访问配置文件 为了安全起见,你应该限制对配置文件的直接访问,在
httpd.conf文件中找到以下行:
<Directory "/etc/httpd">
Order allow,deny
Allow from all
</Directory> 将其修改为:
<Directory "/etc/httpd">
Order allow,deny
Deny from all
</Directory> 限制目录访问 如果你有特定的目录需要保护,可以在相应的虚拟主机配置文件中设置访问控制。
<Directory "/var/www/html/protected">
Order allow,deny
Allow from 192.168.1.0/24
Deny from all
</Directory> FAQs

Q1:如何查看 Apache HTTP Server 的版本信息?
A1:使用以下命令可以查看 Apache HTTP Server 的版本信息:
httpd -v
Q2:如何修改 Apache HTTP Server 的监听端口?
A2:默认情况下,Apache 监听 80 端口,要修改端口,编辑 httpd.conf 文件,找到以下行:
Listen 80
将其修改为所需的端口号,
Listen 8080
然后重启 Apache 服务以应用更改:
sudo systemctl restart httpd

