How to Port Forward a Raspberry Pi Web Server

Hosting a web server on a Raspberry Pi is an excellent way to deploy websites or applications from home, but making it accessible from the internet requires configuring your network router. This article provides a step-by-step guide to setting up port forwarding for your Raspberry Pi, covering how to assign a static IP address, configure your router’s port forwarding rules, and secure your server against external threats.

Step 1: Assign a Static IP Address to Your Raspberry Pi

By default, your router assigns temporary IP addresses to devices using DHCP. If your Raspberry Pi reboots and its IP address changes, your port forwarding rules will break. You must lock in a permanent local IP address.

  1. Open the terminal on your Raspberry Pi or connect via SSH.
  2. Find your current IP address and default gateway (router IP) by running: hostname -I ip route | grep default
  3. Edit the DHCP configuration file to set a static IP: sudo nano /etc/dhcpcd.conf
  4. Add the following lines at the bottom of the file, replacing the placeholder IPs with your actual network details:
interface eth0
static ip_address=192.168.1.100/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

(Note: Use interface wlan0 if your Pi is connected via Wi-Fi instead of Ethernet). 5. Save the file and reboot your Raspberry Pi.

Step 2: Locate Your Router’s Management Console

To direct outside internet traffic to your Raspberry Pi, you need to log into your home router’s admin panel.

  1. Open a web browser on any computer connected to your home network.
  2. Enter your router’s gateway IP address (usually 192.168.1.1 or 192.168.0.1) into the URL bar.
  3. Log in using your router’s administrative credentials. If you have never changed these, they are typically printed on a sticker on the physical router.

Step 3: Configure the Port Forwarding Rules

The exact layout varies by router manufacturer, but the core settings remain identical across all models.

  1. Navigate to the advanced settings menu and look for a section labeled Port Forwarding, Virtual Server, or NAT.
  2. Create a new port forwarding rule with the following specifications:
  1. Save the rule and apply the changes to your router.

Step 4: Test and Secure Your Connection

Your web server is now technically accessible to the public internet, meaning it is time to test the connection and implement basic security measures.

  1. Find your public IP address by searching “what is my IP” on a search engine from your home network.
  2. Disconnect a smartphone from your home Wi-Fi to use cellular data, then type your public IP address into the phone’s mobile browser. If the page loads, your port forwarding is successful.
  3. Because exposing ports to the internet opens your device to potential attacks, immediately change the default password of your Raspberry Pi, install a firewall like UFW (Uncomplicated Firewall), and look into setting up an SSL certificate via Let’s Encrypt to encrypt your traffic over port 443.