端口配置教程

学习如何修改常见软件的端口配置,掌握Docker端口映射技巧

常见软件端口修改

Nginx 端口修改

修改 Nginx 监听端口,避免与其他服务冲突

# 编辑 nginx.conf 文件
sudo nano /etc/nginx/nginx.conf

# 修改 listen 指令
server {
 listen 8080; # 从默认的 80 改为 8080
 listen [::]:8080;
 
 server_name _;
 # ... 其他配置
}

# 重启 Nginx
sudo systemctl restart nginx
记得在防火墙中开放新端口:sudo ufw allow 8080

Apache 端口修改

修改 Apache HTTP 服务器的监听端口

# 编辑端口配置文件
sudo nano /etc/apache2/ports.conf

# 修改 Listen 指令
Listen 8080 # 从默认的 80 改为 8080

# 编辑虚拟主机配置
sudo nano /etc/apache2/sites-available/000-default.conf

<VirtualHost *:8080>
 # ... 其他配置
</VirtualHost>

# 重启 Apache
sudo systemctl restart apache2

MySQL 端口修改

修改 MySQL 数据库服务器的监听端口

# 编辑 MySQL 配置文件
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

# 找到并修改 port 配置
[mysqld]
port = 3307 # 从默认的 3306 改为 3307
bind-address = 127.0.0.1

# 重启 MySQL 服务
sudo systemctl restart mysql

# 连接时指定新端口
mysql -u root -p -P 3307

SSH 端口修改

修改 SSH 服务端口,提高服务器安全性

# 编辑 SSH 配置文件
sudo nano /etc/ssh/sshd_config

# 找到并修改 Port 配置
Port 2222 # 从默认的 22 改为 2222

# 重启 SSH 服务
sudo systemctl restart sshd

# 使用新端口连接
ssh user@server -p 2222
⚠️ 修改前请确保防火墙已开放新端口,避免被锁定在服务器外

Node.js 应用端口配置

配置 Node.js 应用的监听端口

# 在代码中设置端口
const port = process.env.PORT || 3001;
app.listen(port, () => {
 console.log(`Server running on port $${port}`);
});

# 使用环境变量启动
PORT=3001 node app.js

# 或在 package.json 中配置
{
"scripts": {
"start":"PORT=3001 node app.js",
"dev":"PORT=3001 nodemon app.js"
 }
}

Docker 端口映射

学习 Docker 容器的端口映射配置

# 映射单个端口
docker run -p 8080:80 nginx

# 映射多个端口
docker run -p 8080:80 -p 8443:443 nginx

# Docker Compose 配置
version: '3.8'
services:
 web:
 image: nginx
 ports:
 -"8080:80" # 主机端口:容器端口
 -"127.0.0.1:8081:80" # 绑定到特定IP

实用技巧和工具

端口占用检测

检查端口是否被占用的常用命令

# Linux/macOS 检查端口占用
netstat -tulpn | grep :8080
lsof -i :8080
ss -tulpn | grep :8080

# Windows 检查端口占用
netstat -ano | findstr :8080
Get-NetTCPConnection -LocalPort 8080

# 杀死占用端口的进程
kill -9 $(lsof -t -i:8080) # Linux/macOS
taskkill /PID <PID> /F # Windows

防火墙端口开放

在不同操作系统中开放端口的方法

# Ubuntu/Debian (UFW)
sudo ufw allow 8080
sudo ufw allow 8080/tcp
sudo ufw allow from 192.168.1.0/24 to any port 8080

# CentOS/RHEL (firewalld)
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp

# iptables
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
sudo iptables-save > /etc/iptables/rules.v4

# 查看防火墙状态
sudo ufw status
sudo firewall-cmd --list-all

端口转发技术

使用不同工具进行端口转发

# SSH 端口转发
ssh -L 8080:localhost:80 user@remote-server
ssh -R 8080:localhost:80 user@remote-server # 反向转发

# socat 端口转发
socat TCP-LISTEN:8080,fork TCP:localhost:80

# nginx 反向代理
server {
 listen 8080;
 location / {
 proxy_pass http://localhost:3000;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 }
}

# netcat 端口转发
mkfifo backpipe
nc -l 8080 0<backpipe | nc localhost 80 1>backpipe

开发环境端口管理

开发环境中的端口规划和管理技巧

# 推荐的开发端口分配
3000-3099 # 前端开发服务器
4000-4099 # 后端 API 服务
5000-5099 # Python/Flask 应用
8000-8099 # 通用 Web 服务
9000-9099 # 监控和工具

# 使用环境变量管理端口
# .env 文件示例
FRONTEND_PORT=3000
BACKEND_PORT=4000
DATABASE_PORT=5432
REDIS_PORT=6379

# docker-compose.yml 中使用环境变量
services:
 frontend:
 ports:
 -"$${FRONTEND_PORT}:3000"
 backend:
 ports:
 -"$${BACKEND_PORT}:4000"