What this is
This guide explains how to change the port used by Apache or Nginx (for example from 80 to 8080).
What it is for
- Run multiple web services on the same server
- Avoid conflicts (port already in use)
- Test a staging site on a different port
Prerequisites
- SSH + sudo
- Know which web server you use
Step-by-step
Step 1) Confirm what is listening on ports
sudo ss -lntp | grep -E ":80|:8080"
Apache: change port
Ubuntu/Debian:
sudo nano /etc/apache2/ports.conf
Change/add:
Listen 8080
Also update your site config if needed (<VirtualHost *:8080>).
RHEL-based:
sudo nano /etc/httpd/conf/httpd.conf
Change:
Listen 8080
Nginx: change port
Edit the server block:
sudo nano /etc/nginx/sites-available/default
Change:
listen 8080;
RHEL note: configs often in /etc/nginx/nginx.conf or /etc/nginx/conf.d/.
Step 2) Test configuration
Apache:
sudo apachectl configtest || sudo httpd -t
Nginx:
sudo nginx -t
Step 3) Open the new port in the firewall
UFW:
sudo ufw allow 8080/tcp
firewalld:
sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload
Step 4) Restart service
Apache:
sudo systemctl restart apache2 || sudo systemctl restart httpd
Nginx:
sudo systemctl restart nginx
Step 5) Verify
curl -I http://127.0.0.1:8080
Conclusion
Port changes require 3 things: update config, open firewall, restart and verify.