在 CentOS 7 上安装 Apache Web 服务器
在本章中,我们将了解 Apache HTTP Server 诞生的背景,然后在 CentOS Linux 7 上安装最新的稳定版本。
Apache WebServer 简史
Apache 是一个存在已久的 Web 服务器。事实上,它几乎和 http 本身的存在一样久远!
Apache 最初是美国国家超级计算应用中心(也称为 NCSA)的一个相当小的项目。在 90 年代中期,"httpd"(当时的名称)是互联网上最受欢迎的 Web 服务器平台,拥有约 90% 或更多的市场份额。
当时,这是一个简单的项目。被称为网站管理员的熟练 IT 人员负责:维护 Web 服务器平台和 Web 服务器软件以及前端和后端站点开发。 httpd 的核心是它能够使用自定义模块(称为插件或扩展)。网站管理员也有足够的技能为核心服务器软件编写补丁。
20 世纪 90 年代中后期,httpd 的高级开发人员和项目经理离开 NCSA 去做其他事情。这使得最受欢迎的网络守护进程陷入停滞状态。
由于 httpd 的使用如此广泛,一群经验丰富的 httpd 网站管理员召开了一次峰会,讨论 httpd 的未来。他们决定协调并将最好的扩展和补丁应用到当前的稳定版本中。然后,当前的 http 服务器的鼻祖诞生了,并命名为 Apache HTTP 服务器。
鲜为人知的历史事实 − Apache 并非以美国原住民战士部落命名。事实上,它的创造和命名都带有一点不同:它由许多才华横溢的计算机科学家提供的许多修复程序(或补丁)组成:patchy 或 Apache。
在 CentOS Linux 7 上安装当前稳定版本
步骤 1 − 通过 yum 安装 httpd。
yum -y install httpd
此时,Apache HTTP Server 将通过 yum 安装。
步骤 2 − 根据您的 httpd 需求编辑 httpd.conf 文件。
使用默认 Apache 安装,Apache 的配置文件名为 httpd.conf,位于 /etc/httpd/。那么,让我们在 vim 中打开它。
在 vim 中打开 httpd.conf 的前几行 −
# # This is the main Apache HTTP server configuration file. It contains the # configuration directives that give the server its instructions. # See <URL:http://httpd.apache.org/docs/2.4/> for detailed information. # In particular, see # <URL:http://httpd.apache.org/docs/2.4/mod/directives.html> # for a discussion of each configuration directive.
我们将进行以下更改以允许我们的 CentOS 安装为来自 http 端口 80 的 http 请求提供服务。
Listening host and port
# Listen: Allows you to bind Apache to specific IP addresses and/or # ports, instead of the default. See also the <VirtualHost> # directive. # # Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses. # #Listen 12.34.56.78:80 Listen 80
从这里,我们将 Apache 更改为侦听某个端口或 IP 地址。例如,如果我们想在备用端口(如 8080)上运行 httpd 服务。或者,如果我们的 Web 服务器配置了多个具有单独 IP 地址的接口。
侦听
防止 Apache 将每个侦听守护进程连接到每个 IP 地址。这对于停止仅指定 IPv6 或 IPv4 流量很有用。或者甚至绑定到多宿主主机上的所有网络接口。
# # Listen: Allows you to bind Apache to specific IP addresses and/or # ports, instead of the default. See also the <VirtualHost> # directive. # # Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses. # Listen 10.0.0.25:80 #Listen 80
DocumentRoot
"文档根目录"是 Apache 在访问您的服务器时查找索引文件以提供请求的默认目录:http://www.yoursite.com/ 将从您的文档根目录检索并提供索引文件。
# # DocumentRoot: The directory out of which you will serve your # documents. By default, all requests are taken from this directory, but # symbolic links and aliases may be used to point to other locations. # DocumentRoot "/var/www/html"
步骤 3 − 启动并启用 httpd 服务。
[root@centos rdc]# systemctl start httpd && systemctl reload httpd [root@centos rdc]#
步骤 4 − 配置防火墙以允许访问端口 80 请求。
[root@centos]# firewall-cmd --add-service=http --permanent