Use Ubuntu Server as web server on Mac OS X via VirtualBox

I have successfully installed Ubuntu 12.10 Server edition using VirtualBox 4.2.6.

I have set up terminal SSH access to the Ubuntu guest from my Mac OS X (host)

I have installed nginx, mysql, etc by using this gist.

With this bash command, running from my Mac terminal via SSH:

bash -c "$(curl -fsSL https://raw.github.com/gist/4372049)" <mysqlPassword>

I want to continue to use Mac OS X (host) to install my favorite editors and put my codebase in my host.

How do I use the ubuntu server (guest) as a web server? How do I also test my website via browser in my host?


Solution 1:

The solution has two parts.

  1. We need to allow your ubuntu server (guest) to be accessible via your browser in host.

  2. We need to use shared folders so that ubuntu server (guest) can access those files.

Allow ubuntu server (guest) to be accessible via your browser in host

We are going to add new rule in the same NAT > Port forwarding rule in the settings seen here

The new rule is called web. Host port is 8888 or anything that is larger than 1024. Guest port is 80.

Type reboot in your terminal and try http://localhost:8888 and you should see

Welcome to nginx!

assuming that in your host machine you matched 127.0.0.1 with localhost in your /etc/hosts file

Why can't we use http://localhost?

Because we use NAT setting in virtual box, we cannot translate anything less than 1024 to the guest port 80.

Why don't we use Bridged interface instead of NAT?

That is possible, but the disadvantage is you need to assign a static IP to the guest OS. It is also troublesome as it also requires you to be on a network.

My advice is that it is not worth the trouble. Simply using http://localhost:8888 is good enough.

Setting up of shared folders

Log in back as root into your ubuntu guest

Type the following.

sudo apt-get update
sudo apt-get install dkms
sudo apt-get install virtualbox-guest-additions
sudo apt-get install build-essential linux-headers-$(uname -r)
sudo apt-get install virtualbox-ose-guest-x11

The purpose of this is to install something called Virtual Box Guest Additions

You need to install these packages inside the guest ubuntu OS.

Do use these same bash commands whenever you update your VirtualBox

Shutdown gracefully on the guest OS.

Now select settings on the guest OS and go to shared folders to add the folder you want to share from Mac OS X

enter image description here

Select automount and permanent to make your life easier.

Run the ubuntu server (guest) again. Log in as root.

Make sure that your www-data or whichever user you use to access the /var/virtual folders also belongs to the group vboxsf. You cannot change the group that automounts the shared folders so you can only add users to the group.

The command to add www-data to vboxsf is:

sudo adduser www-data vboxsf

Now create symlinks inside your /var/virtual to point to the appropriate folders and your setup of a webserver on an actual ubuntu server will work.