[A Juju Adventure] Charming Lamp!
September 10, 2012 8 Comments
It is time to continue with my blog series on demonstrating how Ubuntu is the best environment to write up “connected” or “cloud backend” Android Apps. As you might know, the Android SDK allows you to set up a sandboxed environment to develop Mobile apps in your desktop, using Juju you can do the same for Cloud apps.
To walk you through how to put these great development tools together, I set out to accomplish:
Proof that you can access a Juju local environment from the Android Emulatordone!
- Using a few charms from the charm store plus a custom one, set up a MySQL database that can be exposed through a web service with simple commands/steps
- Develop a TODO list android app
- Connect the android app and the webservice, so they talk to each other.
Today is time to set-up our own Cloud environment in a PC. A good starting point for a web application is a LAMP stack. If you hope for you service to get popular, you might want to split out the database service and the web service into their own instances so they can be scaled easily.
When I set out to do this, I wanted to write an extremely simple PHP page that would expose a data table from a Mysql server. I looked up the available charms on the store. I found that you had a lot of charms for existing apps, but none that had the bare bones of a LAMP stack and just allowed you to install your own web pages. However, I did find a charm to deploy Mysql and a very handy tool to manage it (phpmyadmin). Taking this as a starting point I developed a generic LAMP charm.
The first thing the LAMP charm does is to install and configure a new instance with Apache and PHP5 in an LXC container. The new charm will then copy any files under /website inside the charm folder structure, into /var/www. It also allows you to specify a Bazaar branch. It will clone the branch into the webserver and copy the contents to /var/www. I keep my TODO application for this exercise here.
Using juju, you can set up a relationship with a Mysql service. A Mysql database is created by default at this time. You can change the name of the database as a configuration options. If you provide a file called mysql_config either on the /website folder or in the root of you Bazaar branch, this will be used to configure further the Mysql database.
The charm will store the details of the relationship in /var/webconfig. In there you can also find opendb.php, a basic script that will open the connection to the MySQL database for you.
So lets get started! Clone the Lamp branch to a folder of your choice. I have it under “~/Mycharm/precise/lamp”. (If you need to get started with juju check my previous post here)
#step1 - deploy Mysql as a service.
juju deploy mysql
#step2 - deploy phpmyadmin tool
juju deploy phpmyadmin
juju set phpmyadmin password="password"
#this makes phpmyadmin public - exposes port 80/tcp
juju expose phpmyadmin
#step3 - link phpmyadmin and mysql so the can talk to each other
juju add-relation phpmyadmin mysql
#step4 deploy lamp from your local folder
juju deploy --repository ~/mycharm local:lamp
#step5 set up your bazaar branch and publish your website
juju set lamp website-bzr="lp:~vtuson/+junk/mytodo_web"
juju expose lamp
#step6 configure your database and link lamp and mysql services
juju set lamp website-database="android_todo"
juju add-relation lamp mysql
Once Juju has completed all the commands, run “juju status”. Now go to the ip address for the LAMP services and visit the databaseweb.php page (e.g. http://192.168.122.174/databaseweb.php) , you get this rather ugly table:
|Id||Todo||Is it starred|
|1||hello this is a sample||1|
|2||yes it works!||0|
This table is displaying the contents of a Mysql database. You will get the same data if you visit your Myphpadmin service:
So there you are a full and scalable LAMP stack in your PC!
If you want to update the web service by pulling the latest version of the Bazaar branch, just run the following commands:
juju set lamp bzr-update="yes"
juju set lamp bzr-update=
With my web developer “hat” on, I can now hand this charm over to my Android app developer. They will be able to set up exactly the same environment locally to test their app without using expensive server time.
So “Using a few charms from the charm store plus a custom one, set up a MySQL database that can be exposed through a web service with simple commands/steps” – DONE!