在 CentOS 上,Apache 的服务名通常是 httpd(不是 apache2,apache2 是 Debian/Ubuntu 的叫法)。下面以 CentOS 7/8 为例说明如何修改 Apache 端口。
vim /etc/httpd/conf/httpd.conf
找到这一行(默认是 80):
Listen 80
改成你想要的端口,例如:
Listen 8080
如果有虚拟主机配置,也要检查:
vim /etc/httpd/conf.d/*.conf
确保 VirtualHost 中的端口一致,例如:
<VirtualHost *:8080>
CentOS 默认使用 firewalld
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload
如果是 iptables(较少见):
iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
service iptables save
如果启用了 SELinux,需要允许 Apache 使用新端口:
semanage port -a -t http_port_t -p tcp 8080
查看已允许的端口:
semanage port -l | grep http_port_t
systemctl restart httpd
设置开机启动(如未设置):
systemctl enable httpd
netstat -tunlp | grep httpd
# 或
ss -tunlp | grep httpd
浏览器访问:
http://服务器IP:8080
如果你用的是 CentOS + Apache2(自己编译或特殊环境),或者 Docker / 云服务器,可以告诉我具体环境,我可以给你更精确的方案。