Local network web pages failing to load while localhost works typically indicates a DNS resolution failure or a firewall rule blocking external interface access rather than the Apache service itself. The server is reachable via its loopback address, confirming the web server software is running correctly. However, other devices on the LAN cannot resolve the hostname or connect to the specific IP address assigned to the Raspberry Pi. This disconnect usually stems from misconfigured DHCP options, incorrect /etc/hosts entries, or UFW rules that restrict traffic to non-loopback interfaces.
Verifying Network Connectivity and Interface Status
Before diving into complex configurations, you must ensure the Raspberry Pi has a valid IP address on the local subnet. Use
ip addr show to list all network interfaces. Identify the interface connected to your router (often
eth0 for wired or
wlan0 for wireless). Check if the IP address falls within the same subnet as your client machine. If the IP is
169.254.x.x, the device failed to obtain an address via DHCP and is using a link-local address, which prevents communication with other LAN devices.
If the interface shows a valid IP, verify that it is not isolated by router settings. Some routers have "AP Isolation" or "Client Isolation" enabled, which prevents devices on the same Wi-Fi network from communicating with each other. Disable this feature in your router’s admin panel if it is active. Additionally, ping the default gateway from the Raspberry Pi using
ping -c 4 192.168.1.1 (replace with your actual gateway). If this fails, the issue is physical connectivity or switch/router configuration, not the web server.
Checking Firewall Rules with UFW
Ubuntu servers often come with the Uncomplicated Firewall (UFW) enabled. By default, UFW may deny incoming connections on public interfaces while allowing them on loopback. Since accessing via
localhost uses the loopback interface (
lo), it bypasses these restrictions. Accessing via the LAN IP triggers the firewall rules for the external interface.
Run
sudo ufw status verbose to inspect current rules. Look for lines that explicitly deny incoming traffic or allow only specific ports like SSH (
22/tcp) but omit HTTP (
80/tcp) or HTTPS (
443/tcp). If you see a rule denying incoming traffic, you must allow web traffic. Execute
sudo ufw allow http and
sudo ufw allow https. If you are using a non-standard port, replace
http with
allow 8080/tcp or your specific port number. After updating the rules, test access from another device on the network. If the problem persists, check if a more specific deny rule exists above the allow rule, as UFW processes rules in order.
For deeper insight into how Ubuntu handles these interfaces, you might find
How do Ubuntu-server network interfaces work? helpful for understanding the underlying network stack behavior.
Testing Localhost vs. LAN IP Behavior
To confirm the firewall hypothesis, compare responses from different addresses. From the Raspberry Pi itself, run
curl -I http://localhost. This should return a
200 OK header. Next, determine the LAN IP using
hostname -I and run
curl -I http://. If the second command hangs or times out while the first succeeds, the firewall is definitely blocking external requests. Ensure that your Apache configuration listens on
0.0.0.0 or the specific LAN IP, not just
127.0.0.1. In
/etc/apache2/ports.conf, verify that the
Listen directive does not restrict binding to localhost only.
DNS Resolution and Hostname Configuration
If you are accessing the site by hostname (e.g.,
http://raspberrypi.local) rather than IP, DNS issues are likely the culprit. Linux systems use
mDNS for
.local domains. Ensure the
avahi-daemon is installed and running on the Raspberry Pi with
sudo systemctl status avahi-daemon. On the client machine, ensure it supports mDNS resolution. Windows users may need the Bonjour Print Services or rely on NetBIOS names instead.
Alternatively, configure static DNS entries on your router’s DHCP server. Many routers allow you to assign a static lease and define a custom hostname for the DHCP reservation. This ensures that when the client requests the name, the router returns the correct IP. If you prefer manual configuration, edit
/etc/hosts on the client machine to include the Raspberry Pi’s IP and hostname. This bypasses DNS entirely for testing purposes. If direct IP access works but hostname access fails, the issue is strictly DNS-related, not a web server or firewall problem.
Apache Virtual Host and Binding Issues
Sometimes Apache is configured to serve content only for specific virtual hosts bound to certain IPs. Check the sites-enabled directory in
/etc/apache2/sites-available/. Open the default configuration file and look for the
<VirtualHost> tag. It should contain
<VirtualHost *:80> or
<VirtualHost _default_:80>. If it specifies a particular IP address that differs from the one assigned to the interface, external requests will be rejected.
Additionally, verify that the
ServerName directive matches the hostname you are trying to access. While not strictly required for basic access, mismatches can cause redirect loops or 403 errors depending on the configuration. Restart Apache after any changes using
sudo systemctl restart apache2. Monitor the error logs in
/var/log/apache2/error.log for clues regarding permission denied or binding failures.
For users dealing with broader connectivity challenges, such as VMs losing internet access, reviewing
Ubuntu 22.04 VM on KVM/virt-manager Not Connecting to Internet can provide insights into similar networking stack troubleshooting techniques applicable to various Linux environments.
Frequently Asked Questions
Why does localhost work but the LAN IP does not?
This discrepancy usually indicates that a firewall is blocking incoming traffic on the external interface while allowing loopback traffic. It can also mean Apache is bound only to 127.0.0.1. Check UFW rules and Apache's ports.conf to ensure listening on all interfaces.
How do I reset UFW to default settings safely?
You can reset UFW by running
sudo ufw reset, which clears all rules. Be cautious as this removes all existing protections. After resetting, manually add necessary rules like SSH and HTTP before enabling the firewall again with
sudo ufw enable.
What if my router assigns a different IP to the Raspberry Pi?
DHCP leases can expire or change if the server reboots. Set a static DHCP reservation in your router’s admin panel for the Pi’s MAC address. This ensures the Pi always receives the same IP, making firewall rules and DNS entries stable over time.
Can I use IPv6 to access the local server?
Yes, if your router and ISP support IPv6, you can access the server via its IPv6 address. Ensure Apache is listening on IPv6 interfaces by checking
ports.conf for
Listen [::]:80. Test connectivity using
curl -6 http://[ipv6_address] from a client with IPv6 enabled.
Why am I getting a 403 Forbidden error on the LAN IP?
A 403 error means the server reached Apache, but permissions prevented access. Check the ownership of your web root directory in
/var/www/html. Ensure files are readable by the www-data user. Incorrect permissions, such as 700 on directories, often cause this issue on fresh installations.