在 CentOS 上配置 Apache(httpd)端口,通常包括修改监听端口、调整防火墙以及(如有)SELinux 设置。下面以 CentOS 7/8 为例说明。
Apache 的主配置文件一般是:
/etc/httpd/conf/httpd.conf
找到如下行(默认是 80):
Listen 80
改成你需要的端口,例如 8080:
Listen 8080
如果你使用的是虚拟主机,也要检查:
/etc/httpd/conf.d/*.conf
里的 <VirtualHost *:80> 是否改为对应端口:
<VirtualHost *:8080>
systemctl restart httpd
设置开机启动(如未设置):
systemctl enable httpd
CentOS 7/8 默认使用 firewalld。
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
firewall-cmd --list-ports
如果启用了 SELinux,仅改端口还不够,需要允许 Apache 使用新端口。
semanage port -l | grep http_port_t
semanage port -a -t http_port_t -p tcp 8080
如果提示
semanage命令不存在:
yum install policycoreutils-python-utils -y
curl http://localhost:8080
http://服务器IP:8080
tail -f /var/log/httpd/error_log
如果你使用的是 CentOS Stream / Rocky / Alma 或 Nginx 反向代理 Apache,可以告诉我具体场景,我可以给你更针对性的配置。