温馨提示×

apache在centos怎样配置端口

小樊
38
2026-10-17 14:29:32
栏目: 智能运维

在 CentOS 上配置 Apache(httpd)的端口,通常涉及修改监听端口、开放防火墙以及(如有)SELinux 设置。下面以 CentOS 7/8 为例说明。


一、修改 Apache 监听端口

1. 编辑配置文件

Apache 的主配置文件通常位于:

/etc/httpd/conf/httpd.conf

找到并修改 Listen 指令,例如改为 8080:

Listen 8080

如果你使用了虚拟主机,也要检查:

/etc/httpd/conf.d/*.conf

例如:

<VirtualHost *:8080>
    DocumentRoot /var/www/html
    ServerName example.com
</VirtualHost>

2. 检查配置并重启

httpd -t
systemctl restart httpd

二、配置防火墙(firewalld)

CentOS 默认使用 firewalld。

1. 开放端口(如 8080)

firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload

2. 查看已开放端口

firewall-cmd --list-ports

三、SELinux 设置(非常重要)

如果启用了 SELinux,必须允许 Apache 使用新端口。

1. 查看当前允许的 HTTP 端口

semanage port -l | grep http_port_t

2. 添加新端口(如 8080)

semanage port -a -t http_port_t -p tcp 8080

如果提示 semanage: command not found,先安装:

yum install policycoreutils-python-utils

四、验证

本地测试

curl http://localhost:8080

远程测试

浏览器访问:

http://服务器IP:8080

五、常见端口说明

  • 80:HTTP 默认端口(需 root 权限)
  • 443:HTTPS
  • 1024 以上端口一般无需特殊权限

如果你使用的是 CentOS Stream / Rocky / Alma 或 Apache 2.4+,步骤基本一致。如果你有具体端口号或报错信息,可以发出来我帮你具体分析。

0 踩