温馨提示×

centos如何配置apache端口

小樊
34
2026-10-05 21:42:22
栏目: 智能运维

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


一、修改 Apache 监听端口

1. 编辑配置文件

Apache 的主配置文件一般是:

/etc/httpd/conf/httpd.conf

找到如下行(默认是 80):

Listen 80

改成你需要的端口,例如 8080:

Listen 8080

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

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

里的 <VirtualHost *:80> 是否改为对应端口:

<VirtualHost *:8080>

二、重启 Apache 服务

systemctl restart httpd

设置开机启动(如未设置):

systemctl enable httpd

三、配置防火墙(firewalld)

CentOS 7/8 默认使用 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 命令不存在:

yum install policycoreutils-python-utils -y

五、验证

本地测试

curl http://localhost:8080

浏览器访问

http://服务器IP:8080

六、常见问题

1. 无法访问

  • 防火墙未开放端口
  • SELinux 未授权端口
  • Apache 未重启

2. 查看错误日志

tail -f /var/log/httpd/error_log

如果你使用的是 CentOS Stream / Rocky / Alma 或 Nginx 反向代理 Apache,可以告诉我具体场景,我可以给你更针对性的配置。

0 踩