How to Configure Apache in WSL for Access from Other Devices on same network

WSL Apache Web Server Configuration to access devices on same network

How to Set Up Apache in WSL to Access from Other Devices on same network

Apache port forwarding Apache Server Guide WSL Apache Networking Apache WSL Tutorial Apache Setup WSL Windows Subsystem for Linux Apache WSL Networking Configure Apache on WSL

This guide explains how to set up Apache in WSL (Windows Subsystem for Linux) so it can be accessed from both Windows and other devices on the same network.


1. Install Ubuntu Distro on Windows

  1. Ensure WSL is installed, and you have an Ubuntu distribution set up.
  2. Set a password for root and create a new user.

Access WSL from Another Device on the Same Network

Install OpenSSH

  1. Install OpenSSH on WSL:
    sudo apt update
    sudo apt install openssh-server
  2. Check if the SSH service is running:
    sudo service ssh status

    If the service is not running, start it with:

    sudo service ssh start

Enable SSH on Boot

  1. Enable SSH to start on boot:
    sudo systemctl enable ssh --now
  2. Check the ssh service status:
    service sshd status

Configure WSL on Windows to Access from Other Devices

  1. Find the IP Address of WSL:
    ip addr

    Note the IP address for the next step.

  2. Open a new Windows Terminal with administrator privileges and run:
    netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=[wsl_ip_address]
  3. Verify the port proxy:
    netsh interface portproxy show all

Configure Windows Firewall to Access WSL

  1. Run wf.msc to open Windows Advanced Firewall:
    1. Add a new inbound rule to allow traffic on port 22.
  2. Find the IP address of the Windows machine:
    ipconfig

Access WSL from Another Device on the Same Network

  1. From another device, SSH into the WSL instance using the Windows machine’s IP address and WSL username:
    ssh username_or_root@[windows_ip_address]

2. Set Up Apache in WSL

Install Apache

  1. Open your WSL terminal.
  2. Update your package manager and install Apache:
    sudo apt update
    sudo apt install apache2 -y

    The -y flag automates the installation process by answering "yes" to prompts.

  3. Check if Apache is running:
    sudo service apache2 status
  4. If it’s not running, start Apache:
    sudo service apache2 start

Test Apache Locally

  1. Use curl to test if Apache is serving the default page:
    curl http://localhost
    You should see the HTML of Apache’s default page printed in the terminal.
  2. Open a browser on Windows and navigate to:
    http://localhost
    You should see the default Apache welcome page.

3. Configure Apache to Listen on All Interfaces

By default, Apache may only listen to local requests. To make it accessible from Windows and other devices, modify its configuration:

  1. Open the Apache ports.conf file:
    sudo nano /etc/apache2/ports.conf
  2. Ensure the following line is present:
    Listen 0.0.0.0:80
  3. Save the file and restart Apache:
    sudo service apache2 restart

4. Find Your WSL IP Address

WSL operates in its own network environment, so it has a unique IP address. Find it by running:

ip addr

or

hostname -I

You will see something like 172.28.x.x. Note this down, as it will be used for port forwarding.


5. Set Up Port Forwarding on Windows

To access Apache via http://localhost on Windows, forward traffic from Windows’ localhost to WSL’s IP address using netsh:

  1. Open Command Prompt as Administrator.
  2. Add a port forwarding rule:
    netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=80 connectaddress=<WSL-IP> connectport=80
    Replace <WSL-IP> with the IP address you found in Step 4.
  3. Verify the rule:
    netsh interface portproxy show all
    It should display:
    Listen on ipv4:             Connect to ipv4:
    0.0.0.0         80          172.28.x.x      80

6. Configure Windows Firewall

Allow external traffic to port 80 on Windows:

  1. Open Windows Defender FirewallAdvanced Settings.
  2. Go to Inbound RulesNew Rule:
    • Select PortTCP → Enter 80.
    • Choose Allow the connection → Apply to all profiles.
    • Name it (e.g., Allow Apache Port 80).

7. Test Locally

  1. From Windows, test Apache:
    curl http://localhost
  2. Open a browser on Windows and navigate to:
    http://localhost
    You should see the default Apache page.

8. Test from Another Device on the Same Network

  1. Find the Windows host’s IP address:
    ipconfig
    Look for the IPv4 Address under your active network adapter.
  2. From another device on the same network, open a browser and navigate to:
    http://<windows_ip_address>
    Replace <windows_ip_address> with your Windows machine’s IP.

9. Troubleshooting

  • If Not Accessible:
    • Check Windows Firewall rules and ensure port 80 is allowed.
    • Verify netsh interface portproxy show all displays the correct forwarding.
  • Check Apache Logs: In WSL, inspect the logs for errors:
    sudo tail -f /var/log/apache2/access.log
    sudo tail -f /var/log/apache2/error.log

By following these steps, you can configure Apache in WSL to be accessible both from Windows and from other devices on the same network. Let me know if you have any questions!