在 CentOS 上安装 Apache2,您可以使用以下步骤:
首先,打开终端并使用以下命令安装 EPEL(Extra Packages for Enterprise Linux)仓库。EPEL 是一个由 Fedora 项目提供的扩展软件包仓库,包含了许多在默认 CentOS 仓库中找不到的软件包。
sudo yum install epel-release
安装完成后,使用以下命令安装 Apache2:
sudo yum install httpd
安装过程中,系统可能会提示您设置 Apache 的默认根目录(例如 /var/www/html)。按照提示操作即可。
安装完成后,启动 Apache 服务:
sudo systemctl start httpd
若要确保 Apache 服务在系统启动时自动运行,请执行以下命令:
sudo systemctl enable httpd
使用以下命令检查 Apache 服务的状态:
sudo systemctl status httpd
如果服务正在运行,您将看到类似以下输出的信息:
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-06-21 10:00:00 UTC; 1min ago
最后,打开浏览器并访问您的 CentOS 服务器 IP 地址或域名。如果 Apache 安装成功,您将看到“It works!”的默认欢迎页面。
注意:在运行 Apache 之前,请确保防火墙允许 HTTP 和 HTTPS 流量。以下是允许 HTTP 和 HTTPS 流量的命令:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload