在 CentOS 上搭建的 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 环境中,“改端口”通常指修改 Apache 的监听端口(网页访问端口,默认是 80)。下面分步说明。
常见配置文件位置:
/etc/httpd/conf/httpd.conf/etc/httpd/conf/httpd.confvi /etc/httpd/conf/httpd.conf
找到这一行:
Listen 80
改成你需要的端口,例如 8080:
Listen 8080
如果有虚拟主机,也要改:
<VirtualHost *:80>
改为:
<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
CentOS 默认开启 SELinux,否则 Apache 无法绑定非 80 端口。
查看当前允许端口:
semanage port -l | grep http_port_t
添加新端口:
semanage port -a -t http_port_t -p tcp 8080
systemctl restart httpd
访问方式:
http://服务器IP:8080
如果你说的是 数据库端口(默认 3306):
vi /etc/my.cnf
或
vi /etc/my.cnf.d/server.cnf
添加或修改:
[mysqld]
port=3307
systemctl restart mariadb
# 或
systemctl restart mysqld
Listen)+ 防火墙 + SELinux如果你能说明:
我可以给你更精确的操作命令。