温馨提示×

centos怎么配apache端口

小樊
43
2026-10-03 00:13:16
栏目: 智能运维

在 CentOS 上配置 Apache(httpd)端口,一般分为 修改监听端口、开放防火墙、SELinux(如有) 三步。


一、修改 Apache 端口

1. 编辑配置文件

sudo vi /etc/httpd/conf/httpd.conf

找到这一行(默认是 80):

Listen 80

改成你想要的端口,例如:

Listen 8080

如果你有多个端口:

Listen 80
Listen 8080

如果是虚拟主机,也要检查 VirtualHost 中的端口是否一致。


2. 重启 Apache

sudo systemctl restart httpd

查看状态:

sudo systemctl status httpd

二、配置防火墙(firewalld)

CentOS 7+ 默认使用 firewalld。

开放端口(以 8080 为例)

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

查看已开放端口:

sudo firewall-cmd --list-ports

三、SELinux 配置(非常重要)

如果启用了 SELinux,只开防火墙仍可能访问不了。

1. 查看 SELinux 状态

getenforce

如果是 Enforcing,需要允许 Apache 使用新端口。

2. 添加 SELinux 端口策略

sudo semanage port -a -t http_port_t -p tcp 8080

如果提示 semanage 不存在:

sudo yum install policycoreutils-python-utils -y

3. 验证

sudo semanage port -l | grep http_port_t

四、测试访问

浏览器或命令行访问:

curl http://服务器IP:8080

常见问题

1. 端口被占用

sudo netstat -tunlp | grep 8080

2. 本地能访问,外网不能

  • 云服务器需配置 安全组
  • 检查 firewalld / SELinux

3. 改回 80 端口

编辑 httpd.conf 改回 Listen 80,重复上述步骤即可。


如果你用的是 CentOS 6(service httpd) 或 Apache + Nginx 反向代理,可以告诉我,我可以给你对应方案。

0 踩