Score:0

Apache2: Webpage is only accessible via localhost but not via custom domain or IP address

cn flag

I'm just starting to learn Apache server.

I installed apache2(version 2.4.41) based on Ubuntu, WSL2 Windows10 and followed this tutorial to set up a webpage.

I created a webpage for testing. The path is:

/var/www/info.dev/html/index.html

The issue is, after I followed the tutorial step by step, my webpage only works via localhost:

localhost img

This is my /etc/hosts file:

host img

My info.dev.conf file:

config img

It won't work if I use a custom domain like info.dev or www.info.dev: domainimg

If I use a local IP address, I get this error: ip img

I've been stuck in this issue for several days and tried every solution I could google, but it seems like none of them works for me. I'll really appreciate it if someone could save my day.

Score:0
in flag

Based on the myriad of images you've shared, I would venture that you've edited the "wrong" hosts file. Rather than change the one in Ubuntu, you need to edit the one in Windows found at C:\Windows\System32\Drivers\etc\hosts. In there, you can add your records.

Replace:

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost

With:

127.0.0.1        localhost
127.0.0.1        info.dev

::1              localhost
::1              info.dev

You do not need to have a record for subdomains such as www.info.dev. Be sure to replace 127.0.0.1 with the IP address of your WSL2 Ubuntu instance. You can find this by typing: wsl hostname -I from PowerShell or Command Prompt.

In your Apache configuration file, you can update the ServerAlias to include a wildcard to ensure that all traffic for the domain is routed correctly:

ServerAlias info.dev *.info.dev

Having info.dev in both ServerName and ServerAlias is fine. You can exclude it from the alias if you prefer.

paladin avatar
kr flag
PS when using a _hosts_ file for name resolution, the _hosts_ file should be shared onto all computers within your LAN. Make sure some stupid Windows "antivirus/firewall"-software doesn't block your Windows _hosts_ file.
nickwfj123 avatar
cn flag
Thanks, matigo. After I modify the hosts file, local IP address works for my own page. But when I tried to use localhost, the web page is redirected to IIS windows page and when I tried custom domain info.dev, error prompts "Your connection is not private".
in flag
@nickwfj123 — Is your WSL2 environment accessible to Windows with `127.0.0.1` or is it using a different IP address?
nickwfj123 avatar
cn flag
@matigo - I'm not sure if we are saying the same IP address. I got it by the command: hostname -I, which is different from 127.0.0.1. A new problem I just found is when I remove these modifies above in hosts file, custom domain works, but only at the first time I open a new tab. Then if I refresh the page, something goes wrong again. error prompts "This site can’t be reached. Check if there is a typo in www.info.dev."
in flag
`localhost` will — ideally — only ever point to `127.0.0.1`, so typing this into a browser on a system that is not the server will not work. You will need to use the custom domain(s) that you set up. The error you see with `www` is also likely due to the hosts file on your Windows machine. You will need a line with the WSL instance’s IP for each subdomain.
nickwfj123 avatar
cn flag
Thanks. A line with local IP and subdomain pair works now. Does this mean I have to manually modify the hosts file each time I wanna generate new domains? I'm wondering if there is a way I can do via Ubuntu or something else to make the hosts file automatically set up.
in flag
I would strongly discourage having automated systems in WSL (or anywhere else) automatically update the `hosts` file on your Windows machine. There is a reason why one must be Admin to edit that file, and it’s not good to leave yourself open to DNS hijacking …
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.