Tuesday, February 19, 2013

My 2013 Dev Environment

Windows 7 + Ubuntu 12.10 (in Virtual machine)

Running a webserver in Windows can be a pain in the ass. It's slow and lack of fun (if you like unix CLI).
This is a guide to setup a development environment optimized for performance. According to my observation, Apache in Ubuntu serves a dynamic web page roughly 5 times faster then Apache in Windows on the same hardware.



Step 1. Get Ubuntu Server Edition

Why not Ubuntu Desktop Edition? We're setting a web server here, GUI is extra fishes. According to their FAQ http://www.ubuntu.com/download/desktop/alternative-downloads since version 12.04, both using the same kernel and not much difference except lack of GUI and Apache.

Get the iso image here, http://www.ubuntu.com/download/server. Slow? Try torrent instead. http://www.ubuntu.com/download/desktop/alternative-downloads

Step 2. Get Virtualbox

Get the windows installer here, https://www.virtualbox.org/wiki/Downloads and not forget the VirtualBox Extension Pack.

Step 3. Setup Ubuntu 12.10 on Virtualbox

Create a new virtual machine. Select type to Linux. Select version to Ubuntu (64 bit obviously if your copy of Ubuntu not 32 bit). Follow through the setup wizard, it's pretty simple and easy.

Tips: Mount the Ubuntu image as CD ROM. Virtualbox Settings > Storage > Add CD/DVD Device.

On Ubuntu Server installation, install Open SSH, Apache, Samba. Reboot and set the server name.
echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdn

Install PhpMyAdmin if you like.
sudo apt-get install phpmyadmin

Install Guest Additions in VirtualBox.
sudo apt-get install dkms
sudo apt-get install virtualbox-ose-guest-x11

Set the permissions
sudo usermod -G vboxsf -a 

Reboot and check the auto-mount shared folders if you setup any.
sudo reboot
ls /media

You can make a symbol link if you want to put your files in Windows host. I don't recommend this idea because Windows filesystem is slow.
sudo ln -s /media/sf_www/ /var/www

Tips: Shutdown or reboot the Ubuntu virtual machine.
sudo reboot
sudo shutdown -h now

Alternatively, you may want to use PUTTY to SSH into Ubuntu guest. http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Step 4. More web server setup:

Enable Apache mod rewrite.
sudo a2enmod rewrite

Install Xdebug.
sudo apt-get install php5-xdebug

Install Java if you needed .
sudo apt-get install openjdk-7-jre-headless

Install Drush for Drupal junkies.
sudo apt-get install php-pear
sudo pear channel-discover pear.drush.org
sudo pear install drush/drush

Step 5. Linking the Host and Guest

Now, the marriage of Windows 7 and Ubuntu 12.10. We need them to talk to each other. First, shutdown the virtual machine.

Open the Settings > Network. We leave Adapter 1 stays default as NAT. Click advanced > Port Forwarding. Let's add port 80 and 22 for both HTTP and SSH.

Click Adapter 2. Enable the network adapter and attached it to Host-only Adapter. Else Windows host can't find Ubuntu guest.

Goto Virtualbox Preferences > Network and edit the host-only adapter. Disable DHCP Server. From here you'll notice the IP to your Ubuntu guest is 192.168.56.1.

Now we're going to assign a static IP in Ubuntu guest.
sudo vim /etc/network/interfaces

And add the eth1 as follows.
# The host-only network interface
auto eth1
iface eth1 inet static
address 192.168.56.101
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.255

Step 6. Setup Samba.

Now, here we want to open and edit files from your Windows environment. The way I do it here is insecure because I hard coded the permissions. But anyway, as long as it works in my dev env and this is not production server, it's fine. (chuckle)
sudo vim /etc/samba/smb.conf

Add an entry.
[varwww]
  comment = Server root directory
  path = /var/www
  browsable = yes
  guest ok = yes
  read only = no
  writeable = yes
  create mask = 0644
  directory mask = 0755
  map archive = yes
  map system = yes
  map hidden = yes
  force user = www-data
  force group = www-data

Restart the Samba server.
sudo service smbd restart
sudo service nmbd restart

Finally, go to Windows Explorer and type \\192.168.56.101 you should be able to see the files.