Now that our server is up and running, we want to be able to access it from our host system. We’ll set it up so we can SSH to it, transfer files to it via SFTP, and make HTTP requests to Apache.

To do all this we need to edit the xml configuration file for our virtual machine:

  • On a Mac, the file is found at ~/Library/VirtualBox/Machines/<machine name>/<machine name>.xml

  • On Windows, it’s inside the .VirtualBox/Machines subdirectory in your home folder.


So for my machine, which I’ve called “Ubuntu Server,” I’m editing Machines/Ubuntu Server/Ubuntu Server.xml

At the top of the file you should see an <ExternalData> tag. Inside that tag, copy in the following tags:

<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" value="2222"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" value="22"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" value="TCP"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/HostPort" value="8888"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/GuestPort" value="80"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/Protocol" value="TCP"/>


These lines configure VirtualBox to forward requests to specific ports on the host system onto other specified ports on the guest system. For SSH, we’re forwarding port 2222 of the host system to port 22 of the guest system (where OpenSSH is listening). The same principle applies to the Apache configuration items, with port 8888 on the host mapping to port 80 on the guest.

With that done, save the xml file and restart your virtual machine.

If the machine fails to start, it’s likely to be because of a network interface configuration problem. In the lines we added, we specified pcnet as the network interface. To ensure that’s what your virtual machine is using, right-click on it in the main VirtualBox window and click Settings. In the Network tab, select one of the PCnet adapters from the Adapter Type drop-down. You should be able to restart your virtual machine with no problems now.

Now if you open a browser on your host system and point it to http://localhost:8888/ you should see the default Apache “It works!” page. Great!

Similarly, to SSH into your new server, SSH to port 2222 on localhost with the username you set during the Ubuntu server installation. (If you’re on Windows, you can use the PuTTY SSH client to perform the same function):

ssh -l <username> -p 2222 localhost


You’ll receive the usual “unknown host” security warning; type “yes” to connect and you’ll be prompted for your password. Upon entering it, you should be logged in to your server! Feel free to look around and make yourself at home.

While we’re still logged in, let’s do one more task: by default the Apache web root in Ubuntu Server is /var/www/, which your default user won’t have write permissions for. Let’s change that, so you can upload files to your web root with SFTP. Enter this command and hit return:

sudo chown <username> /var/www


To connect to your server with FTP, no extra configuration is necessary. OpenSSH gives you “free” FTP via the SFTP (SSH FTP) protocol. Most clients (FileZilla, for example) support it; just choose SFTP as the protocol, localhost as the server with port 2222, and your Ubuntu username and password. Choose /var/www/ as the default directory, and you should be able to transfer files to and from your server.

Let’s test that everything is working: create a php file named info.php containing the usual phpinfo call:

<?php
phpinfo();
?>


Use your FTP client to upload that file to your server’s /var/www/ folder. Now point your browser to http://localhost:8888/info.php, and you’ll see the PHP info page. The System row at the top of the table will tell you PHP is running on Ubuntu.

There you have it! You can test server configurations, brush up on your sysadmin skills, and develop your web sites and applications in a full Linux server environment running inside your usual desktop.

Source: http://www.sitepoint.com/blogs/2009/10/27/build-your-own-dev-server-with-virtualbox/