How to get apache to serve a site via VirtualBox?

39,576

If you need to allow other machines in your physical network reach your VM or if the VM needs Internet access, use bridged networking. Otherwise, stick to host-only networking.

  1. Stop your VM and open the settings for it in the VirtualBox (OSE) Manager

  2. Go to the Network tab

  3. Select the network mode at your choice (bridged networking or host-only)

    If you want to use bridged networking, you've to select the right network adapter at Name: ____________. For wired connections, you'd select something named like eth0. Wireless connections are usually named wlan0 (the numbers may vary).

  4. Save the settings

  5. Start the Ubuntu VM

  6. When up, you can gather the IP address by running:

    sudo ifconfig
    

    The output should look similar to this:

    eth0      Link encap:Ethernet  HWaddr 08:00:27:f4:c3:7b  
              inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::a00:27ff:fef4:c37b/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:20 errors:0 dropped:0 overruns:0 frame:0
              TX packets:25 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:3244 (3.2 KB)  TX bytes:2512 (2.5 KB)
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    

    In the above output, 192.168.1.4 (on the second line) is the IP address that can be used in your Ubuntu host system to access your VM.

  7. Now open the hosts file in Ubuntu host machine:

    sudo gedit /etc/hosts
    

    (If you don't want to use gedit, replace the word with the name of your favorite editor. E.g. vim, nano.)

    Once the file is open, add this line, and save it:

    192.168.1.4   my-dummy-site.com
    
  8. Open any browser on your host machine and go to my-dummy-site.com to access your website, served right from VirtualBox.

( Special thanks to @iSeth for the help. Entirely based on this answer, bit is NOT the same. )

Share:
39,576

Related videos on Youtube

its_me
Author by

its_me

Updated on September 18, 2022

Comments

  • its_me
    its_me over 1 year

    This tutorial taught me how to create an Ubuntu Server (12.10, 64-bit) VM in VirtualBox on a Windows 7 host machine, install Apache and have it serve a site from a dummy domain accessible via the host machine's browser.

    It took a day and many attempts (although it seemed I was following the instructions as they were laid out, I was always doing something wrong) and I'd finally done it.

    I find Ubuntu as a much seamless development environment than Windows, and so I wanted to same thing on Ubuntu. So, this time, I created an Ubuntu Server VM in VirtualBox on Ubuntu host machine, installed Apache and configured it. But when I access the site via the dummy domain, I get the 'Server not found' error.

    Yes, I did modify the /etc/hosts file just as mentioned in the tutorial (which is for Windows 7). But I couldn't get it to work. Don't know what's wrong. Anyone know what else I should be doing?

    EDIT: If I am not clear enough, please ask. I am willing to clarify.

    • Davisein
      Davisein over 11 years
      Do you want to use the server from your computer (for example to develop) or from others in the network?
    • its_me
      its_me over 11 years
      The server is installed in a virtual machine (using virtualbox), and I want to access the test website served by the server from a browser on the host machine. Hope that's clear. I've posted the answer by the way (but can only mark it as answered after 2 days).
    • Davisein
      Davisein over 11 years
      Sure, I have already upvoted your answer which is very good (btw). I just wanted to recommend you to add both host-only and bridged (for internet access). And also the other answer is a requirement in a bridged envirnonment if you want to access to the VM from the outside. To make the question fully clear for others ;)
  • its_me
    its_me over 11 years
    I will try it out and let you know. Meanwhile, you said that "Only root can open ports < 1024", but I was still able to get it to work (please see my solution below).
  • Eric Carvalho
    Eric Carvalho over 11 years
    @its_me it's because your solution makes use of bridged networking.
  • its_me
    its_me over 11 years
    Ah, okay. Thanks for the clarification Eric. :)
  • its_me
    its_me over 10 years
    @Davisein Wrong. Bridged network mode allows your virtual machines to be accessed from the host machine, while also giving it (VM) access to the Internet connection used by the host. :)
  • Davisein
    Davisein over 10 years
    True. I don't delete my previous comment so that yours don't look weird but it's wrong. The system that does not allow the hosting and the guest machine to talk is NAT as said here virtualbox.org/manual/ch06.html.
  • Ethereal
    Ethereal almost 10 years
    Make sure IPTables isn't blocking the connection...
  • Chris Nielsen
    Chris Nielsen almost 7 years
    I followed these instructions to a T. If I open a browser on the Ubuntu VM, it is able to resolve the dns and serve my-dummy-site.com. However, if I open a browser outside of the Ubuntu VM (on my Windows 10 machine where I have my VM installed), it is not able to resolve my-dummy-site.com. It is able to resolve the IP 192.168.1.118 though! What could be the problem?