Getting comfy with the VirtualBox based Bitnami LAMP Virtual Machine Stack for Drupal Development

A few days ago I shared my experience in setting up Bitnami LAMP Virtual Machine Stack using VirtualBox for Drupal development on my MacBook Air. That article serves well for the initial setup with the latest version of VirtualBox. Now, a few days later, I would like to share "what I really had to do" to get comfy with this local laptop/workstation development environment, the actual steps necessary for acquiring a truly useful tool.

Short List ("back to work")

  •    Obtain IP of running Bitnami Guest unless you are using static IP (recommended)
  •    Fix Local IP in /etc/hosts and login with terminal
  •    Mount codebase via sshfs and work with IDE, Atom, Sublime, etc. or else use Eclipse remoting.

Long List ("Let's get a Drupal project up and running and get back to work already")

  •    Static address
  •    Set hostname and server names on Guest if not using Static address
  •    Fix Local IP in /etc/hosts/ and login with terminal
  •    Make sure terminal is bash and not dash
  •    Checkout codebase
  •    Set files permissions and refresh files via rsync
  •    Create database via http://phpmyadmin.bitnamilampvm
  •    Refresh db via rsync if not included in codebase and load local db
  •    Setup local /etc/hosts, in the Guest vm setup the virtual hosts, and restart apache
  •    Run in browser
  •    Mount codebase via sshfs and work with IDE, Atom, Sublime, etc. or else use Eclipse remoting
 

Obtain IP of running Bitnami Guest unless you are using static IP (recommended)

When you first fire up the Bitnami Box, the useless terminal that comes up (it turns black after a while and you can't see the mouse cursor in it, you can't select text and copy it, and to get it to work after leaving it for a while you have to bang it on the side like an old TV set: click, then press Return, and the characters come back Cool) tells you the IP that the local DNS server on your router has assigned to it (a tremendously useful thing to happen, by the way: it is reachable via other computers on your network or via the whole internet via a service like DynDNS, for example). Of course, you didn't write that down when it first came up, did you? No worries. Just bang it on the side as described above, login if you haven't already, and type:

$ ifconfig

You'll see something like the following, with the IP address showing up somewhere:

eth0      Link encap:Ethernet  HWaddr 08:00:27:f7:5b:0f
inet addr:192.168.0.99  Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fef7:5b0f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:44766 errors:0 dropped:0 overruns:0 frame:0
TX packets:22851 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9013506 (9.0 MB) TX bytes:7036640 (7.0 MB)
...

Fix Local IP in /etc/hosts and login with terminal

Edit your Host (local) /etc/hosts as follows (probably on a single line):

# Bitnami vm's
192.168.1.99 bitnamilampvm drupal01.bitnamilampvm phpmyadmin.bitnamilampvm 
192.168.1.99 client-project2.bitnamilampvm server-project2.bitnamilampvm

Now you can login via your regular Mac terminal:

$ ssh bitnami@bitnamilampvm

Mount codebase via sshfs and work with IDE, Atom, Sublime, etc. or else use Eclipse remoting.

Install sshfs using homebrew or something. Then create a directory where you can mount any part of the box, and do something like the following:

$ sshfs -o no_readahead,noappledouble,nolocalcaches bitnami@bitnamilampvm:/home/bitnami bitnami

which would mount your bitnami user's directory (where I place all my projects and configure my virtual hosts) to the bitnami folder.

Once your files have appeared (sshfs is blazingly fast when the box is running locally), just open up a Mac terminal in the mounted directory and type:

$ atom

And start enjoying a cool environment.

If you do Eclipse remoting, you don't need sshfs of course.

Static address (recommended)

Yeah, it gets to be a bore editing your /etc/hosts file every time you restart the box. And what's worse, at my friend's house, the IP provider router keeps re-assigning the IP, which increments every 30 minutes or so, even though the lifetime is 24 hours. Go figure. So, what if I always get the same IP every time I fire up the box? Awesome!

The Bitnami box has its own little MAC assigned to it, so you can configure your router to reserve the same IP for it so it doesn't overlap with another device. Say 192.168.0.99. Now all you have to do is configure the Bitnami box Ubuntu to come up with the same IP when it's fired up.

Log into your Bitnami terminal as user bitnami (not your regular Mac Terminal, since the IP will change during the procedure), then magically transform yourself into root for your 15 minutes of fame.

# sudo -i

As root go ahead and save the original network interfaces configuration file

# cd /etc/network
# cp interfaces interfaces.ori

Then edit interfaces for a static address as per this handy guide, for example:

$ cat interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.xxx <- static ip, perhaps configured on your router for VBox MAC
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1 

Then copy file for future invocation

# cp interfaces interfaces.homesweethome

Simply restart the network and you're done!

# /etc/init.d/networking restart

Go back to being a humble non-superuser

# exit
$

Now edit /etc/hosts according to the static IP you have assigned, and login using your regular Mac Terminal.

If you don't like the idea of setting up a static IP, Set hostname and server names on Guest.

Make sure terminal is bash and not dash

We always do this, don't ask me why... (can't remember why, but it's a good idea on any system you're going to be using for any length of time)

bitnami@linux:~/cotizador$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 29 2012 /bin/sh -> dash
bitnami@linux:~/cotizador$ sudo rm /bin/sh
[sudo] password for bitnami:
bitnami@linux:~/cotizador$ sudo ln -s /bin/bash /bin/sh
bitnami@linux:~/cotizador$ ls -l /bin/sh
lrwxrwxrwx 1 root root 9 May 15 12:22 /bin/sh -> /bin/bash

Checkout codebase

As explained in the previous article, my choice was to go ahead with virtual hosts instead of using the bitnami Drupal module, since I have more control with just using drush, etc., and set things up as I 'm used to and as on production site.

Make sure user bitnami has an ssh key and it is registered on github, pantheon, etc.

To create the key
$ ssh-keygen -t rsa -C "me@thatmail.com" 

Now provide ./ssh/id_rsa.pub to all your friends and relatives

Setup local github user credentials

 $ git config --global user.name "Me Too"
$ git config --global user.email "me@thatmail.com"

Grab your project

 $ git clone git@github.com:metoofool/greatproject.git drupiproj01

Set files permissions and refresh files via rsync

Your files directory probably won't be versioned in the repo, so you'll have to create the files directory and grab the assets from the legacy or production server. rsync gets it done (repeatedly)!

Create the file dir

bitnami@linux:~$ mkdir sites/default/files

Download the files from server

rsync -rlvz --size-only --ipv4 --progress -e 'ssh' \
  user@legacy.com:/home/user/public_html/sites/default/files/ /home/bitnami/drupiproj01/sites/default/files/ 

Set files permissions so both rsync and production will work in future

$ cd drupiproj01
$ sudo chown -R daemon:bitnami sites/default/files
$ sudo find sites/default/files -type f -exec chmod 664 {} \;
$ sudo find sites/default/files -type d -exec chmod 775 {} \;

There's more than one way to do it of course...

Create database via http://phpmyadmin.bitnamilampvm

Or any other way you want to. Then edit your settings.php file to reflect the local database.

Refresh db via rsync if not included in codebase and load local db

We always bear in mind, even if you're using an (recommended) "everything in code" approach, that every Drupal site has three basic components: Codebase, Database, assets in the files directory. For security and for reasons of unwieldliness, your database is probably not in the repo either, so bring it in via scp, rsync or what have you. Or if you already have the database downloaded to your laptop, you can sftp or scp it into the box. From any Mac terminal:

$ scp database.sql bitnami@bitnamilampvm:/home/bitnami/backup
Once your local settings.php file has been edited to reflect the local database, you can simply do
$ cd drupiproj01
$ drush sql-cli < ~/backup/database.sql

Setup local /etc/hosts, in the Guest vm setup the virtual hosts, and restart apache

We are going to configure our virtual hosts in the file /opt/bitnami/apache2/conf/extra/httpd-vhosts.conf. As explained in the last article, in order to enable virtual hosts, you need to edit /opt/bitnami/apache2/conf/httpd.conf, and uncomment the include directive for the virtual hosts file:

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Now add the virtual host configuration (notice we also had to include phpmyadmin, once we started using virtual hosts):

$ cat /opt/bitnami/apache2/conf/extra/httpd-vhosts.conf 
...
<VirtualHost *:80>
  ServerName phpmyadmin.bitnamilampvm
  ServerAlias phpmyadmin.bitnamilampvm
  DocumentRoot "/opt/bitnami/apps/phpmyadmin/htdocs"
  Include "/opt/bitnami/apps/phpmyadmin/conf/httpd-app.conf"
</VirtualHost>
<VirtualHost *:80>
 ServerName drupiproj01.bitnamilampvm
 DocumentRoot /home/bitnami/drupiproj01
 <Directory "/home/bitnami/drupiproj01">
   Options Indexes FollowSymLinks MultiViews
   AllowOverride All
   Require all granted
 </Directory>
</VirtualHost>
...

Restart Apache

$ cd /opt/bitnami
bitnami@linux:/opt/bitnami$ sudo ./ctlscript.sh restart apache
Syntax OK
/opt/bitnami/apache2/scripts/ctl.sh : httpd stopped
Syntax OK
/opt/bitnami/apache2/scripts/ctl.sh : httpd started at port 80

If the subdomain is new, don't forget to stick it into your /etc/hosts as described above.

Note: One stupid thing some stupid routers provided by stupid ISP's do is to remove all nameserver info from the /etc/resolv.conf file. If so, the Bitnami box won't be able to resolve external domains. if this happens, just copy over the Mac's /etc/resolv.conf file automatically generated by the router:

$ cat /etc/resolv.conf 
#
# Mac OS X Notice
#
# blah blah blah
#
nameserver 200.115.192.28
nameserver 190.55.60.129
nameserver 200.115.192.30

Run in browser

http://drupiproj01.bitnamilampvm

Now get to work!