In my previous blog post, I’ve shown you how to effectively use Docker for development teams. But containerization technologies like Docker, as much as they are cost effective in terms of memory and processor power, have a learning curve which while not too steep, do require a good chunk of time to be spent on learning. That’s why older virtualization techniques like VirtualBox are still being used to create environments for development and QA teams. And yes, they are quite effective!

This short tutorial will show you how to setup one of the little intricacies when developing in VirtualBox, namely being able to store and edit your code in the host machine, while running it inside the guest OS. Will be using CentOS 7 as a guest, while the host is not of any importance.
I will also show you how to make the VirtualBox guest accessible in the hosts browser.

The following prerequisites are assumed to be fulfilled:

Let us begin by making the guest OS accessible to the host. Open the Preferences window in VirtualBox by pressing Ctrl+G (Mac: Cmd+G) and then navigate to Networking.

Create a new Host-only network adapter vboxnet0 with the following parameters:

  • Set the IPv4 address to: 192.168.100.1
  • IPv4 network mask: 255.255.255.0

Now we are going to activate the DCHP server so we won’t need to set static ip address and change configuration files in the guest OS. In the DHCP tab set the following values:

  • check Enable server
  • set Server address to: 192.168.100.100
  • Lower address bound: 192.168.100.101
  • Upper address bound: 192.168.100.101 (this way the guest OS will always be assigned the same address)

Once you are finished, open the settings of your VirtualBox appliance and navigate to the network settings as in the picture bellow. Leave Adapter 1 enabled and set to NAT.

Enable Adapter 2 and set the following paramters:

  • Attached to: Host-only adapter
  • Name: The adapter we have created – vboxnet0
  • If not checked, do check Cable connected

Good job. With this step finished we should have access to the server from the browser. So open it and enter http://192.168.100.101 in the address bar. The Apache default page should be shown.

Now let us make the changes to run your code in the guest OS. Open the virtual appliance settings page again and navigate to Shared folders. Once there add the projects (directories) you would like to run in the Machine folders list. Don’t click on any checkbox there.

Save the changes and run the virtual appliance. Open the terminal and SSH to the machine (192.168.100.101). Once there elevate your privileges (become root) and edit your /etc/fstab. Add the following line at the end of the file:

testing-com /media/sf_testing.com vboxsf  rw,uid=48,gud=48  0   0

testing-com is the name of the share we’ve created in the settings window a minute ago.
/media/sf_testing.com is the directory of the share in your guest. In CentOS and RedHat all shares are placed in the /media directory by default and they all have sf_ prefix.
vboxsf is the VirtualBox file system which makes all the magic happen.

One more thing that is peculiar in the line above is ‘48‘. That is your Apache user and group id. We want to give the ownership of the shared directory to the web server so the code execution  runs smoothly. To be able to find the UID and GID of Apache in your system (which really, it should be 48) run: id apache in your terminal.

Once this is done you can reboot your guest OS. Next time you log in you should have /media/sf_testing.com directory with apache:apache ownership.

Last thing we need to do is set up our Apache configuration. Now you can do this anyway you want. Either set the web server directly, or create a virtual host. But in any case, set the DocumentRoot of the site in the default directory: /var/www/html. Simply create a symlink to the shared directory and don’t forget to create it as the apache user. Otherwise the owner will be the root user and the web server may experience permission issues when running code. Execute the following command:

sudo -u apache ln -s /media/sf_testing.com /var/www/html/testing.com

And that’s it! Now you can edit your code in the host and run it in the guest machine. Happy coding and if you have any remarks or questions, leave a comment or drop me a line.

 

 


parg0

Gjoko Pargo

0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.