If you're a developer or system administrator working with Windows Subsystem for Linux (WSL2), you might encounter challenges when trying to run a web server like Apache. In particular, getting Apache to work and be accessible from Windows can be tricky due to networking and port binding issues.
In this blog, I'll walk you through the process of setting up Apache on WSL2, solving the common networking problems, and enabling access from your Windows machine. Let’s dive in!
Step 1: Install Apache on WSL2
First, make sure you have WSL2 installed and set up on your Windows machine. If you haven't already, you can follow the official Microsoft guide to install WSL2 and a Linux distribution (Ubuntu is commonly used).
Once WSL2 is installed, open your WSL2 terminal (Ubuntu or other Linux distro) and install Apache by running:
sudo apt update
sudo apt install apache2
Step 2: Start Apache
After installation, you can start Apache using the following command:
sudo systemctl start apache2
However, Apache might not start right away if there’s an issue with port
80 (which is used for HTTP traffic).
It means that Apache is trying to bind to port 80 but another service is blocking it.
Step 3: Troubleshoot Port 80 Binding Issues
On WSL2, Apache may fail to start because IPv6 binding is enabled by default, and another service (or a previous Apache instance) might be occupying port 80. To resolve this, follow these steps:
- Check for existing processes using port 80:
sudo ss -tulpen | grep ':80'
- If nothing is using port 80, but Apache still fails to start, it's likely due to IPv6 binding.
Step 4: Disable IPv6 Binding (Temporary Fix)
- Open the
ports.conf
file:
sudo vim /etc/apache2/ports.conf
- Comment out the line where Apache listens on IPv6
([::]:80):
# Listen [::]:80
Now, Apache should start without issue, as it will no longer try to bind to IPv6.
Step 5: Access Apache from Windows
You can now test Apache from the Windows side:
Open PowerShell or your browser and visit: http://localhost
You should see the default Apache welcome page, confirming that Apache is running successfully!
- Save and exit the file
- Restart Apache:
sudo systemctl restart apache2
Note: This is just a temporary fix
Top comments (0)