简体   繁体   中英

How to access Virtual Hosts setup on the VM from the Host Machine?

I want to access my virtual hosts (websites) that I've set up in my VirtualBox -> Ubuntu-Server 18. I have apache2 installed and everything is setup.

The host machine (macOS) can presently gain access to the default website of apache2 in the guest (Ubuntu-server) through a Port Forwarding rule I have assigned in the VirtualBox Network Settings:

127.0.0.1:8000 -> 10.0.2.15:80

I've set up a virtual host in my guest machine with the following configuration:

<VirtualHost *:80>

    ServerName dropn.taxi
    ServerAdmin admin@dropn.taxi
    DocumentRoot /var/www/dropn.taxi/public_html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

The DocumentRoot directory mentioned above exists and has the files necessary to serve a basic "Hello World" home page. I've tested it with my apache installed in my local machine as well. The website's not the problem.

But everytime I access localhost:8000 from the local machine, I'm greeted with an Ubnutu-Apache greetings page. Whereas when I try localhost:8000/dropn.taxi , I get a 404 page. I've researched quite a bit and the closest I can get to a solution is what is suggested in this page: Ubuntu Apache2 local virtual hosts url not found

Simply put: How can I access virtual hosts of the guest machine (VM) from the host machine?

Still not specific to my case. Would appreciate any help in this regards.

localhost in the apache2 server is set to point to /var/www/html by default. For the virtual hosts to work properly, the domain name accessing them must be what is defined in the ServerName field.

localhost:8000/dropn.taxi would translate to 127.0.0.1/dropn.taxi in the VM guest. Which means, it is trying to access the /var/www/html/dropn.taxi , which, ultimately does not exist, hence the 404 error.

The solution to this is to add dropn.taxi to the /etc/hosts file in both the Host Machine and the VM. The line would look like: 127.0.0.1 localhost dropn.taxi

While accessing from the Host Machine, we use dropn.taxi:8000 in the browser whereas the port :8000 is not used in the guest machine. This way, whenever accessing the virtual hosts, the domain name accessing them matches the ServerName field and it simply works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM