How to run a web server from Virtual Box

VirtualBox is a free, lightweight virtual machine simulator from Sun. The current version is 3.0.10.

One of the uses of VirtualBox is to run a web server or other application from a virtual operating system on your computer, whether it be Windows, Linux, or Mac.

So, the point of this short post is to describe how to access your web server from your host machine. For whatever odd reason, I simply could not find this documented *anywhere* that was simple. This requires no installation of extra software or configuration of anything on your computer or on your virtual server.

Ok, I’m assuming that you already have your server installed.

  1. Shut it down if its running.
  2. Open up Virtual Box. In the left panel, select your VM and then click the yellow Settings button above it.
  3. In the new view, click on the Network button off to the right along the top.
  4. By default, Adapter 1 should already be selected. If not select it, or if you are configuring a different adapter, select that one.
  5. You should see menus called Adapter Type, Attached To, and Name. Name is probably greyed out.
  6. Under Attached To, select Bridged Adapter. The Name menu should now be clickable.
  7. Under the Name menu, select the network adapter that your computer is using for its internet connection. I am on a Mac on wireless, so my network adapter is en1: Airport. Yours might be one of the others if you are connected via ethernet.
  8. Hit OK, and this window will go away.
  9. Now start your VM image and log in.
  10. Once it is started up, you have to find the IP address that the VM is now using.
  11. If you are running a Linux server like Ubuntu, type “ifconfig” on the command line. Look for “eth0” and the “ifnet addr” near it. In my case, it is 192.160.0.7.
  12. If you are running Windows, open your command prompt and type “ipconfig”. Look for a similar type of IP address.
  13. The IP address you found will be the one that you use to contact your virtual web server.
  14. Open a browser and type that number in as the address. If your server is up and running, you should now see a web page from it.

As an added bonus, its easy to configure a local name that points to this IP address and makes it easy to access your web page in the future. The steps are nearly identical for Mac, Linux, and Windows users. Since I am on a Mac, I will describe the process for us. For Windows users, do a quick search on where to find your Hosts file. The information I describe here applies, but your hosts file is in a different location.

  1. Mac users: Open a terminal.
  2. Type ‘sudo pico /etc/hosts’ and enter.
  3. Somewhere at the end of the file, add something like “localubuntu 192.168.0.7”. Of course, “localubuntu” is the address I want to use. Feel free to name it whatever you like. Also, the IP address has to match the one that you found in the earlier steps with “ifconfig” or “ipconfig”.
  4. Hit control-o and enter to save the changes. Closing the terminal window will save the changes system wide. No reboot should be necessary.
  5. Now go back to a browser and try going to “localubuntu” or whatever you named yours and you should now be hitting your web server.

Feel free to leave comments, suggestions, or questions about any of this.

Quick tips on UTF-8 encoding on a Mac running Java & Tomcat6

I ran into a problem today with UTF-8 encoding on my Mac(10.5.8), running a Java/Tomcat6 environment. It took a while to figure out, so I’m posting here in the hopes it helps others in the future.

Mac OS uses its own variant of the Java SDK. The default character encoding is MacRoman. (Read this page, scroll to “Character Encoding”). This can cause problems, such as my situation, where we are consuming a service that encodes its data as UTF-8, operates or transforms the data within our application, then outputs it again as UTF-8. The original UTF-8 data was being converted to MacRoman and then back out to UTF-8. Corruption ensued.

Depending on exactly what your problem is, I identified 2 general fixes. I only needed the first one, but I want to document the 2nd as well.

  1. Add URIEncoding to both the Java HTTP and AJP Connectors
  2. Add “-Dfile.encoding=UTF-8” to your JAVA_OPTS environment variable.

Fix #1:

  1. Edit your server.xml file. This is typically in /apache-tomcat/conf/server.xml.
  2. Look for the HTTP Connector, mine looks like this: <Connector port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443″ useBodyEncodingForURI=”true”/>
  3. Add the uriencoding to it: <Connector port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443″ useBodyEncodingForURI=”true” uriencoding=”UTF-8″/>
  4. Look for the AJP Connector: <Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″/>
  5. Add the uriencoding to it: <Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ uriencoding=”UTF-8″/>

Fix #2:

  1. In my setup, I have added my JAVA_OPTS and other environment variables to my ~/.bash_profile. I am assuming that you already have your dev environment setup and know where your JAVA_OPTS are. This is my setup.
  2. From the terminal: sudo pico ~/.bash_profile and find the line with your JAVA_OPTS. Again, this is my config line and yours may be different.
  3. Add “-Dfile.encoding=UTF-8” to it: export JAVA_OPTS=”-Xmx768m -XX:MaxPermSize=256m -Djava.awt.headless=true -Dfile.encoding=UTF-8″

It took me a few hours of searching around and it seems like these are the general settings that most people recommend. For me, it was the combination of both Connectors that made it work. It will probably be different for other people depending on your setup.

For extra info, visit these links for more info:

Tomcat and UTF-8

Apple’s Java Docs