Port Configuration Tutorials
Learn how to modify port configurations for common software and master Docker port mapping
Common Software Port Configuration
Nginx Port Configuration
Modify Nginx listening port to avoid conflicts with other services
# Edit nginx.conf file
sudo nano /etc/nginx/nginx.conf
# Modify listen directive
server {
listen 8080; # Change from default 80 to 8080
listen [::]:8080;
server_name _;
# ... other configurations
}
# Restart Nginx
sudo systemctl restart nginx
Remember to open the new port in firewall:
sudo ufw allow 8080
Apache Port Configuration
Modify Apache HTTP server listening port
# Edit port configuration file
sudo nano /etc/apache2/ports.conf
# Modify Listen directive
Listen 8080 # Change from default 80 to 8080
# Edit virtual host configuration
sudo nano /etc/apache2/sites-available/000-default.conf
<VirtualHost *:8080>
# ... other configurations
</VirtualHost>
# Restart Apache
sudo systemctl restart apache2
MySQL Port Configuration
Modify MySQL database server listening port
# Edit MySQL configuration file
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
# Find and modify port configuration
[mysqld]
port = 3307 # Change from default 3306 to 3307
bind-address = 127.0.0.1
# Restart MySQL service
sudo systemctl restart mysql
# Specify new port when connecting
mysql -u root -p -P 3307
SSH Port Configuration
Modify SSH service port to improve server security
# Edit SSH configuration file
sudo nano /etc/ssh/sshd_config
# Find and modify Port configuration
Port 2222 # Change from default 22 to 2222
# Restart SSH service
sudo systemctl restart sshd
# Connect using new port
ssh user@server -p 2222
⚠️ Ensure firewall has opened the new port before modification to avoid being locked out
Node.js Application Port Configuration
Configure Node.js application listening port
# Set port in code
const port = process.env.PORT || 3001;
app.listen(port, () => {
console.log(`Server running on port $${port}`);
});
# Start with environment variable
PORT=3001 node app.js
# Or configure in package.json
{
"scripts": {
"start":"PORT=3001 node app.js",
"dev":"PORT=3001 nodemon app.js"
}
}
Docker Port Mapping
Learn Docker container port mapping configuration
# Map single port
docker run -p 8080:80 nginx
# Map multiple ports
docker run -p 8080:80 -p 8443:443 nginx
# Docker Compose configuration
version: '3.8'
services:
web:
image: nginx
ports:
-"8080:80" # host:container
-"127.0.0.1:8081:80" # Bind to specific IP
Practical Tips and Tools
Port Usage Detection
Common commands to check if ports are in use
# Linux/macOS check port usage
netstat -tulpn | grep :8080
lsof -i :8080
ss -tulpn | grep :8080
# Windows check port usage
netstat -ano | findstr :8080
Get-NetTCPConnection -LocalPort 8080
# Kill process using the port
kill -9 $(lsof -t -i:8080) # Linux/macOS
taskkill /PID <PID> /F # Windows
Firewall Port Opening
Methods to open ports in different operating systems
# 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
# Check firewall status
sudo ufw status
sudo firewall-cmd --list-all
Port Forwarding Techniques
Port forwarding using different tools
# SSH port forwarding
ssh -L 8080:localhost:80 user@remote-server
ssh -R 8080:localhost:80 user@remote-server # reverse forwarding
# socat port forwarding
socat TCP-LISTEN:8080,fork TCP:localhost:80
# nginx reverse proxy
server {
listen 8080;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
# netcat port forwarding
mkfifo backpipe
nc -l 8080 0<backpipe | nc localhost 80 1>backpipe
Development Environment Port Management
Port planning and management tips in development environment
# Recommended development port allocation
3000-3099 # Frontend dev servers
4000-4099 # Backend API services
5000-5099 # Python/Flask apps
8000-8099 # General web services
9000-9099 # Monitoring and tools
# Manage ports with environment variables
# .env file example
FRONTEND_PORT=3000
BACKEND_PORT=4000
DATABASE_PORT=5432
REDIS_PORT=6379
# docker-compose.yml using environment variables
services:
frontend:
ports:
-"$${FRONTEND_PORT}:3000"
backend:
ports:
-"$${BACKEND_PORT}:4000"