Instant LAMP Server: Using Cloud-Init to Pre-configure Cloud Compute Instances

Apr 25 2013

Cloud compute instances are like disposable servers. They make a great platform for application development as well as deployment. But frankly it's no fun to spend time setting them up time and time again. Lately, I've been amassing a collection of tools and strategies for working with compute instances in the cloud. One thank I think is outstanding for its combination of simplicity and power is Ubuntu's Cloud Init.

To expedite setting up a LAMP (Linux/Apache/MySQL/PHP) server, I've created a very simple Cloud-Config YAML file that I can attach to a build request when I build a compute instance. In this post, I'll show how I do it. <!--break-->

A Cloud-Config File

Here's the file:


# Automatically update all of the packages
package_upgrade: true
package_reboot_if_required: true

# Install the LAMP stack for me
  - apache2
  - mysql-server
  - libapache2-mod-php5
  - php5-mysql

# Replace index.html with index.php
  - "echo '<?php phpinfo();' > /var/www/index.php"
  - "rm /var/www/index.html"

This file is submitted with my request to create a compute instance (more on this shortly). As the Nova system creates a new compute instance, it also feeds this file into the Ubuntu OS running on the new instance, which then reads it and acts accordingly.

The YAML file instructs the Ubuntu to do the following:

  1. Apply all package updates (so I start out with a secure OS)
  2. Install the following packages, along with their dependencies:
    • apache2
    • mysql-server
    • libapache2-mod-php5
    • php5-mysql
  3. Replace the default index.html with a very basic index.php that I can then use to verify in a browser that all worked well.

Fire Away

To start a new compute instance using that configuration, I use the hpcloud commandline client. There are other clients that can do the same thing, but this one is my favorite.

Here's what I do:

    $ hpcloud servers:add MyServer  xsmall -u ./lamp.yaml -i 81078 -k myKey -s Web

This is read as follows:

  • Add a new server...
  • named MyServer...
  • with size xsmall...
  • uploading ./lamp.yaml (the file we created above)...
  • using image 81078 (the ID for Ubuntu 12.04 LTS)...
  • using my SSH key named myKey...
  • and running in the security group Web (My security group that opens ports 80 and 443 for HTTP and HTTPS).

When I run this command, a few minutes later I get something like this: Created server 'MyServer' with id '1212483'. Using the command hpcloud servers, I can see my new server and get the IP address to connect to it.


Cloud-Init is a little tricky to debug. But there is one invaluable tool for helping you root out problems: /var/log/cloud-init.log. This gives a detailed read-out of what Cloud-Init did, and if it encountered any errors it will put them here.


The file I have shown here is very basic. But Cloud-Init configuration files can be much more complex. While it's not a terribly well-documented tool, there are plenty of examples in the source code. These show how to do things like mount network drives, add or manage users, add additional apt repos, and run arbitrary commands.