Dev-Box: Virtual Machines for Developers

This post goes out to all the coders using a Windows machine for development. Might be interesting for others aswell but I can’t say much about that as I’m a Windows user myself ;)

So what is this about? Well, some time ago I ran into smaller problems when trying to move a website from my local machine (which used XAMPP) to a real server (which was running on Ubuntu or some other Linux distro) and it was hard to fix the problems because of the differences between Windows and Linux servers.

I talked about this with a friend of mine and he told me about Virtual Machines (=VM). I had never heard of them before but what I heard was intriguing. Virtual Machines allow you to run a machine inside your machine, e.g. a Linux server inside your Windows machine. So you can use this VM with Linux to test your website locally before moving it to the real server. I know that no server is like the other and you might still run into some problems due to different library versions and so on, but you gotta admit that two Linux servers have way more in common than a Linux and a Windows server ;)

So in this post I’ll tell you how to setup your own VM with all the libs you need. Even if you haven’t worked with Linux before, you should be able to follow this tutorial/example easily.

Contents

Beware: VMs created with this tutorial are not secure and should only be used in a local environment. Please don’t use these instructions to setup a real server.

Getting started

First you have to get some player to run the vm in, e.g. VMware Player, VMware Server or VMware Workstation. The former ones are for free, but the later one costs quite a bit and is only suitable for companies or if you want to run multiple VMs at once.

Next thing you need is the VM itself. You can either download the basic version with nothing but the OS installed from thoughtpolice (I used Ubuntu 10.10 for this tutorial) or download the ready-to-go version that’s the result of this tutorial:
Basic version from thoughtpolice: Click here
Ready-to-go version: Click here

After you’ve installed the player you should propably take a look at the network settings aka Virtual Network Editor. For me it always worked with VMnet0 set to type “Bridged” and “Automatic” selected in the Bridged to dropdown. Now you can go ahead and load the VM into the player (File -> Open) and after that’s done you should edit the Virtual Machine settings and set the Network Apapter to Bridged aswell.Now fire it up and if all goes well you should be prompted to login. For both versions you can use the following credentials to login:

Basic Version
Username: notroot
Password: thoughtpolice

Ready-to-to Version
Username: root
Password: thoughtpolice

If you use the basic version you got the following options how to proceed:

  1. You can either prepend “sudo ” before all the commands listed below when executing them, which basically means “run the following command as root”
  2. Each time you login you run “sudo -s” once, which means that all commands executed in the current session will be run as root
  3. You proceed as in option 2 and afterwards you run “passwd” to change the password of the root user. Now you can login as root with your new password and don’t have to use sudo anymore.

To your information: If you are inside your player (e.g. clicked on the console to enter your login credentials) and can’t get back to windows, try pressing Ctrl + Alt. That one kept me trapped for a while ;)

First steps

First we want to make sure that all pre-installed libs are up-to-date. To do this we have to run two commands on the console:

apt-get update

This will make sure that all our sources (that’s what tells the server what libraries are available and so on) are up-to-date.

apt-get upgrade

And this one will update all your installed libs to the newest version that’s found in the sources.
The next step would be to change your root password, both for security and simplicity reasons. It’s way easier to enter a password that you know well than entering thoughtpolice! You can skip this step if you are using the basic version and used option 3.

passwd

And now you will be prompted to enter a new password. Easy as pie ;)
Finally you might want to change the keyboard layout. The US keyboard layout is the default and that’s not suitable for everyone. Just run this command and a setup dialog will popup and ask what layout you want to use:

dpkg-reconfigure console-setup

Connecting to your VM

As you’ve noticed you can use the player to access the VM and enter commands in the console. But as you get trapped inside the player and can’t copy/paste text from/to the console it’s rather tedious to work that way. That’s where so called SSH Clients come into play. They allow you to connect to your VM (or rather the server that’s running inside) via an external program that’s way easier to handle. One of such clients is Putty. I know that it hasn’t been updated in a while but it still works really great and doesn’t even have to be installed, it’s just a single executable that you run whenever you need to connect to a remote machine via SSH.

Now to get this working you need to install SSH on your VM. If you’ve downloaded the ready-to-go version that’s already the case and you can skip to the next paragraph, the rest must run this command in their console:

apt-get install openssh-server

This will install an SSH server on your console and enable you to connect to it from the outside.

To actually be able to connect to your VM you need one more thing: the IP address. If the network is configured correctly this command will list all interfaces and their attributes, one of them being the IP address:

ifconfig

The interface you should watch out for is eth0 and the value of “inet addr” is the IP address we are looking for. Now that we got the IP address we can start Putty enter the IP into the “Host Name” field and click “Open”. If all went well you should be prompted to login. Just use the credentials from above (with the new password you set…if you even bothered to change it).

Instead of entering your IP address each time you want to access your server either via SSH or in your browser you could go ahead an edit your hosts file and add an entry with your server’s IP address. The hosts file is usually located here: C:\Windows\System32\drivers\etc and can be used to translate an IP address into a host name. For example if your VM’s IP address was 192.168.178.152 you could add this entry and your VM would from now on be available under the host name “my_vm” (additionally to the IP address of course):

192.168.178.152    my_vm

Whenever I tell you to enter the IP address of the VM, you can enter the host name instead if you’ve setup your hosts file properly.

Adding some web to your server

Now that we are connected we can install the important stuff: Apache, PHP, MySQL…our staff of life.
Apache and PHP are simple, just run these two commands and you are done:

apt-get install apache2
apt-get install php5

If you now open your browser and go to http://ip-address-of-my-vm (or http://host-name-of-my-vm) you should see the standard “It works!” message from apache. To see if PHP was installed properly aswell we need to create a simple php file to run in the browser. First switch to your webroot by entering this command:

cd /var/www

There’s nothing in there except the index.html (which is shown when opening your VM’s IP in the browser) and now we need to create a new file. This can be done by opening the pre-installed editor and supplying a non-existand filename as a parameter:

nano info.php

Where “nano” is the pre-installed editor and info.php the (in this case non-existant) file we want to edit. Now that the editor is open you can enter plain php, e.g. this:

<?php phpinfo(); ?>

To close the editor you need to press Ctrl+X and confirm that you want to save the modified buffer. After that you can open the info.php file in your browser and should see the standard phpinfo() output.
If how ever your browser asks you to download the .php file instead of displaying it, you need to run this command to restart your apache2 server:

/etc/init.d/apache2 restart

Setting up MySQL

What else do we all need? Exactly, a database! I prefer MySQL and that’s why it’s featured in this tutorial, but you could (more or less) easily install PostgreSQL, or whatever else you prefer, instead. Anyway, installing MySQL is as simple as all the stuff before has been. Run the below commands and follow the on-screen instructions:

apt-get install mysql-server
apt-get install php5-mysql

This will install the newest MySQL server available, 5.1.49 as of this moment. To see what version was installed you can run this command and enter your root password:

mysql -u root -p

After entering the root password you choose during the installation, you’ll find yourself in the MySQL command client and should see something like this at the top:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.1.49-1ubuntu8.1 (Ubuntu)

To get back to the console simply press Ctrl+C. If however you could not connect and get a “Connection refused” error, then you have to edit the MySQL config file and change the bind-address value:

nano /etc/mysql/my.cnf

By pressing Ctrl+W you can search for a text in the file you are editing. Search for “bind-address” and comment out the whole line by adding “#” in front.

Now that we have MySQL running we need some easy way to access the database and what easier way is there than phpMyAdmin? Let’s install it:

apt-get install phpmyadmin

During the installation you will be asked to make some configuration choices, here’s what I chose:

  • apache2
  • Yes
  • my MySQL root password
  • my MySQL root password (and confirmed it)

After the install is done you can see if it worked and go to http://ip-address-of-my-vm/phpmyadmin. If you get a 404 error don’t worry it’s easy to fix. First we have to open the apache2 configuration file

nano /etc/apache2/apache2.conf

and now we scroll to the bottom of the file and add this line and save the changes:

Include /etc/phpmyadmin/apache.conf

Now after restarting the apache2 server with the following command you should be able to open phpmyadmin in your browser:

/etc/init.d/apache2 restart

and now you can login with username ‘root’ and the password you chose.

Accessing your files

Now the last thing you propably want to do is use the console to edit your files. As a Windows user you are used to some nice GUI and propably have been working with your favorite editor for quite some time now. That’s where  a handy lib called “Samba” comes in handy: It let’s you share files and folders between Linux and Windows machines. Now install is easy, configuration is where it might get tricky:

apt-get install samba smbfs

After the install is done, we open the config file and here’s what I my config looks like/yours should look like:

[global]
netbios name = Linux-PC
workgroup = WORKGROUP
server string = Linux Samba Server
load printers = no
show add printer wizard = no
disable spoolss = yes
use client driver = yes
log file = /var/log/%m.log
lock directory = /var/lock
max log size = 0
security = User
encrypt passwords = true
smb passwd file = /etc/samba/smbpasswd
socket options = IPTOS_LOWDELAY TCP_NODELAY
security mask = 0777
create mask = 0777
fstype = NTFS
max smbd processes = 10
map to guest = Bad User
csc policy = disable
hide dot files = yes
hide files = /./.*/aquota.*/
veto files = /aquota.*/
block size = 4096
guest ok = no
getwd cache = Yes
lm announce = yes
lm interval = 120
max xmit = 8192
display charset = UTF8
unix charset = UTF8
status = no
deadtime = 1
level2 oplocks = True
dns proxy = no

[www]
comment = Homepage
path = /var/www
force group = www-data
force user = www-data
browseable = yes
public = yes
writable = yes
valid users = samba
read list =
create mode = 0775
directory mode = 0775

Prior to editing/overwriting the config file you should make a copy of the original file so you will have the original settings as a reference and to re-use as necessary:

cp /etc/samba/smb.conf /etc/samba/smb.conf.original

Easiest way for you to replace the existing content of the config file with the above is by opening nano, entering the new content and then overwriting the existing file:

cd /etc/samba
nano

Now copy the above list of config values and right-click into the editor, this will paste the before copied content. Now press Ctrl+X, confirm the first question and when prompted for a filename enter “smb.conf” and when asked to overwrite the existing file confirm again. The above value for “valid users” tells Samba what users may connect to the shared folder, in this case the user “samba”. By default that user does not exist so we have to add it and set a password. First we add the user to the system, then we add it to samba. Just enter the following commands and follow the instructions:

adduser samba
smbpasswd -a samba

After that’s done we restart the Samba service (not really sure that’s necessary but better save than sorry) and we should be able to connect to our server by mapping a network drive:

restart smbd

Now try mapping a network drive to \\ip-address-of-my-vm\www and when prompted for a username/password just enter your credentials for the samba account. If you can access the folder but don’t have permission to write to it, then run this command and it should be fixed:

chmod a+wrx /var/www

What else is there to do?

Plenty, but for now you are good to go. In further blog posts I’ll tell you how to setup a mail server, connect to SVN/Git and some more useful libs you propably even haven’t heard of yet ;) To end this post I’ll give you a list of the most important commands you’ll need when working (most of them already used above):

Run command X with root privileges if current user is not root:

sudo X

Become root for complete session:

sudo -s

Change to directory X:

cd X

Get contents of current directory:

dir

Open file in text editor nano:

nano filename

Copy a file:

copy oldfilename newfilename

Move/rename a file:

mv oldfilename newfilename

Create a new directory X:

mkdir X

Remove directory X:

rmdir X

Update apt-get sources:

apt-get update

Update installed packages:

apt-get upgrade

Install package X:

apt-get install X

Remove package X:

apt-get –purge remove X

Restart Apache:

/etc/init.d/apache2 restart

Beware: VMs created with this tutorial are not secure and should only be used in a local environment. Please don’t use these instructions to setup a real server.

If you run into any problems feel free to either post in the comments or have a look at the Ubuntu Community Documentation. It helped me quite a bit when I started out with Linux and wrote this article.

I’m looking forward to your comments, criticism and suggenstions on how to improve this further/what you want to see in the future.


Comments

24 responses to “Dev-Box: Virtual Machines for Developers”

  1. If you had selected Ubuntu Server (instead of regular Ubuntu) you would be presented with the option to install OpenSSH and the entire LAMP stack during OS setup. Could save yourself a lot of time running all the individual apt-get commands and ensuing setup.

    1. Not sure what you mean. I didn’t actually run the Ubuntu install, that was already done. I got the VMWare Image with Ubuntu Server 10.10 pre-installed from this website: http://www.thoughtpolice.co.uk/vmware/

  2. Nice tutorial. Virtual machines are great for development.

    I use VirtualBox instead of VMWare. I haven’t used VMWare before, but in VirtualBox you can set up shared folders that are automatically mounted when your VM boots up. When you do this, the files are owned by the user “root” and the group “vboxsf”. A neat trick I learned is to modify the Apache (or nginx) and PHP configs so that those services run as the same user & group your files are mounted under. That way they’ll have proper permissions when you try and do file manipulation through your web app.

    1. With samba you can use the force-group and force-user settings to force all changes to files to be done by the set user instead of the user connected to the shared folder.

      Someone on Twitter told me about VirtualBox. I’ll check it out and maybe write another tutorial or a comparison of VirtualBox and VMWare.

  3. Good post. But the real deal (if you’re using VMWare, don’t know if this exists for other virtualization systems) is to go here:
    http://www.vmware.com/appliances/
    In the VMWare appliance marketplace you’ll find virtually anything you’ve ever need, preinstalled to download and go. Need an Oracle DB server? Or a LAMP server? Grab one, run it and off you go!

    Hope this helps

    1. I’m not yet sure what appliances are, but I’ll look into it :)

  4. Install tasksel and installing php, openssh, samba etc will be much much simpler.

  5. Shared folder can be better option then samba, quicker and more light weight. I think shared folders are supported in vmware player and virtualbox.

    1. According to google (http://www.visoracle.com/download/debian/sharedfolders.html) these shared folders can only be used by the guest system to access folders in the host. Or are you talking about something else? The less I have to install, the better!

  6. I’m doing the same with virtualbox 4.0.4 and Ubuntu 10.10 for guest & host. This way I can move the whole environment between computers and archive it when my project is done.

    Works like a charm – except the one or twice crashes a day sadly …

    I hope they’ll be fixing this soon. In the meantime I’m trying to set up KVM.

    All without costing it a penny.

    1. Ah, right. Moving them around is another great thing you can do with VMs, totally forgot to mention that.

      And if you use VMware Player it’s for free aswell…and no crashes for me so far ;)

  7. […] last post about setting up a Virtual Machine to use as your local webserver was quite the success (hits […]

  8. For those wondering if shared folders work in other players, I can tell you that shared folders work pretty much the same across:

    1 – VirtualBox
    2 – VMWare
    3 – Parallels Desktop for Mac

    I use all of the above and there is little difference in how shared folders work across the three.

  9. steve elek Avatar
    steve elek

    You are helping people to get websites since 2004, and haven’t heared about the virtual machnies; and you are implementing your php apps on windows?… :) linux is the main plattform for developing php apps, as the server machines principally linux based.
    But anyway, good learning and a good post.

    1. When I started coding back in 2004 I had no idea what was really going on ;) And by saying “some time ago” in the first paragraph of my post I mean around 2007!

      Anyway, thanks for your interest and I think we agree, that developers should know at least the linux basics! It’s just not enough to know basic php/mysql

  10. whoah this blog is excellent i love reading your articles. Keep up the great work! You know, a lot of people are looking around for this info, you can help them greatly. I appreciate, cause I found just what I was looking for. You have ended my four day long hunt! God Bless you man. Have a great day. Bye

  11. Couldnt have stated it much better my self! Superb examine.

  12. […] a while, but I’m back with another useful tutorial for all those out there using VMs like my Dev-Box. Last time I talked about getting SVN to work on your VM and now time it’s all about setting […]

  13. […] I promised in my initial Dev-Box post, I promised to give further useful instructions/tips for your development VM, so here we go: […]

  14. […] Behler has provided a complete guide to helping you get a development virtual machine instance up and running quickly complete with […]

  15. […] like it and since I am a SysAdmin, I prefered a server. Last year I came across this post http://www.davidbehler.de/2011/03/virtual-machine-for-developers/ which is a good example of how to use a VM instead of […]

  16. […] by some of the comments about my inital “Dev-Box” post, I’ve started experimenting with Virtual Box. Supposedly you can use the same VM on both […]

  17. […] servers that are accessible from the outside. Doing this on your dev machine, like the one from my tutorial, is a good idea though and something I’ll definitely add in its next […]

  18. […] zu diesem Thema gibt es nun ein Einsteiger-Tutorial von David Behler: Dev-Box: Virtual Machines for Developers. Darin erklärt er Schritt für Schritt, wie er auf einem Windows-Rechner ein Ubuntu 10.10 […]

Leave a Reply to steve elek Cancel reply

Your email address will not be published. Required fields are marked *

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