Differences between revisions 7 and 8
Revision 7 as of 2008-09-27 09:53:44
Size: 10151
Editor: ?ChrisLale
Comment: Mustn't call lh_config more than once
Revision 8 as of 2008-09-27 09:57:49
Size: 10151
Editor: ?ChrisLale
Comment: typo in lamp-update-rcd.sh script
Deletions are marked like this. Additions are marked like this.
Line 303: Line 303:
update-rc-d lamp defaults 90 10 update-rc.d lamp defaults 90 10

Debian Live CD with a LAMP webserver

?TableOfContents(3)

Description

A live CD is a great way to allow people to learn about interactive websites of the sort built using [http://drupal.org/ Drupal], [http://www.joomla.org/ Joomla!], etc. This guide explains how to build a Debian Live CD using LAMP (Linux, Apache, MySQL and PHP). The process has been tested for Drupal using Lenny (Testing).

Outline

  • Build a functioning Drupal system on the host computer.
  • Backup the MySQL database to a .sql file.
  • Set up a build environment on the host.
  • Build a Live image including Apache2, MySQL and PHP by including
    • a packages list in config/chroot_local-packageslists
    • a PHP test file and the .sql file in config/chroot_local-includes
    • an init script to configure a MySQL password and to create/import the database.
  • Test the image
    • Set up the virtual machine on VirtualBox.

    • Set up networking between the host and the virtual machine for transferring files.
  • Build a new image adding an X desktop and the Drupal filesystem.
  • Test the final image.

Initial build

Create the build environment on the host

The command lh_config sets up the initil filesystem for building the live CD. You will need to use a switch (--packages-lists) to include a list of extra packages to be installed and, optionally, a switch (--bootappend-live) to set a locale at boot time.

{{{$ cd $ mkdir live $ cd live $ lh_config \

  • --packages-lists local-pkg-lamp \ --bootappend-live "locale=en_GB.UTF-8 keyb=uk"

}}}

Alternative to lh_config

lh_config should only be run once. If you know that you are going to run it with parameters, now is the time to do it. After this, you can include other features by editing the files in config/ manually. For instance, instead of running

$ lh_config \
     --packages-lists local-pkg-lamp \
     --bootappend-live "locale=en_GB.UTF-8 keyb=uk"

you can just run

$ lh_config 

and set the LH_PACKAGES_LISTS variable by editing config/chroot:

{{{# $LH_PACKAGES_LISTS: set package list to install # (Default: local-pkg-lamp) LH_PACKAGES_LISTS="local-pkg-lamp" }}}

and the LH_BOOTAPPEND_LIVE variable by editing config/binary

{{{# $LH_BOOTAPPEND_LIVE: set boot parameters # (Default: empty) LH_BOOTAPPEND_LIVE="locale=en_GB.UTF-8 keyb=uk" }}}

so that the packages list and the locale are set at boot.

Packages list

To save time, use the base system initially and add the extra essential packages using this packages list:

{{{# local-pkg-lamp # packages list for live-helper(7) with basic LAMP

# You need the locales package if you want to set a # different locale/keyboard map locales

# Apache2 apache2 php5 libapache2-mod-php5 php5-mysql

# mysql mysql-server mysql-client mysql-common

# php php5 libapache2-mod-php5 php5-mysql

# console browser for testing lynx

# ssh and scp for networking between host and guest openssh-client openssh-server }}}

Put the package list file local-pkg-lamp in config/chroot_local-packageslists.

{{{config/chroot_local-packageslists/local-pkg-lamp }}}

Test files

Create this Apache/PHP test file (phpinfo.php):

{{{<?php phpinfo (); ?> }}}

Put phpinfo.php into config/chroot_local-includes/var/www, creating directories as necessary.

{{{config/chroot_local-includes/var/www/phpinfo.php }}}

Put the MySQL export file (eg mydb.sql) into config/chroot_local-includes/etc

{{{config/chroot_local-includes/etc/mydb.sql }}}

Build the image

Build the image with lh_build (as root). This may take some time. Packages are downloaded and cached, so any subsequent builds should be quicker. (Remember to run lh_clean before a rebuild.)

{{{# lh_build }}}

The image will eventually be built - binary.iso.

Test the image

Set up VirtualBox to test the image as discussed in ["DebianLive/Virtualbox"]. In addition set up networking between the host and the virtual guest to enable copying of files between the two using scp. Do this as follows.

Set up a network bridge as described at http://samiux.wordpress.com/2007/07/11/bridge-network-interface-on-virtualbox/. This shell script, run as root, does the job.

{{{#!/bin/sh # networking.sh # Set up networking in Virtualbox. Run as root

# Load the VirtualBox driver module into the kernel. modprobe vboxdrv

# Create TAP interface - tunctl -t tap1 -u chris

# where “chris” is the username in the host

# Create a br0 bridge - brctl addbr br0

# Make your real network interface be promiscuous - ifconfig eth0 0.0.0.0 promisc

# Link your real network interface to bridge br0 - brctl addif br0 eth0

# Assign an IP to the br0. If you are using DHCP - dhclient br0

#Or, if you assign an IP yourself - # ifconfig br0 192.168.1.102 # the IP may be different from yours.

# Link TAP to bridge br0 - brctl addif br0 tap1

# Activate TAP interface - ifconfig tap1 up

# Change the permission of /dev/net/tun - chmod 0666 /dev/net/tun

# At the VirtualBox startup panel, choose “Host Interface” and add “tap1″ to “Interface Name”. }}}

For alternative methods of doing this, see also For alternative methods see also http://wiki.debian.org/VirtualBox file:///usr/share/doc/virtualbox-ose/README.Debian.html

Run binary.iso on the virtual machine.

In order to use scp, you need to know the IP address of the host and the guest. The guest at least gets its IP from DHCP, perhaps the host too. An easy way to discover the local IP is to ping a non-existent IP on the network. The local IP appears on the line telling you that the remote IP is unreachable. Do this for the host and the guest once they guest is running in VirtualBox. For example, pining non-existent address 192.168.1.44 reveals that you are pinging from 192.168.1.20:

{{{$ ping 192.168.1.44 PING 192.168.1.44 (192.168.1.44) 56(84) bytes of data. From 192.168.1.20 icmp_seq=1 Destination Host Unreachable }}}

There is probably a more elegant way of doing this.

Check that Apache works

In the virtual machine (guest), go to the URL "localhost" using the console browser Lynx. If you see "It works!", then it works!

{{{$ lynx localhost It works! q Are you sure you want to quit? y }}}

Check that PHP works

Go to the URL "localhost/phpinfo.php" using the console browser Lynx in the guest. If you see the PHP version number and lots of other information, then everything is OK.

{{{$ lynx localhost/phpinfo.php PHP Logo PHP Version n.n.n-n ... q Are you sure you want to quit? y }}}

Check that MySQL works

Manually set up a password for root. (This will be lost on reboot.)

mysqladmin -u root password new-password

Create the database "mydb" and import data from the mydb.sql file.

{{{$ mysqladmin -u root -pnew-password create mydb $ mysql -u root -pnew-password mydb < /etc/mydb.sql }}}

Check the database. {{{$ mysql -u root -pnew-password > show databases; > quit }}}

These commands will later be put in an init file which will run during boot.

Shut down the guest in VirtualBox.

{{{Machine -> Close Power off the machine -> OK }}}

Build an image with a working LAMP webserver

Modify the build environment

Clean out the previous build on the host. (Downloaded packages remain cached.) As the root user:

{{{# lh_clean P: Cleaning chroot ... }}}

Include the X Window System, a desktop and a browser in the image by adding these lines to config/chroot_local-packageslists/local-pkg-lamp.

{{{#xfce xorg xfce4 iceweasel desktop-base gdm }}}

Prepare an init script to configure MySQL

Based on the init script template from http://www.debian-administration.org/articles/28

# /etc/init.d/lamp
#

# Some things that run always
touch /var/lock/lamp

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting script lamp "
    echo "Setting root password for MySQL"
    mysqladmin -u root password new-password
    echo "Importing MYSQL database mydb"
    #create db
    mysqladmin -u root -pnew-password create mydb
    #import db
    mysql -u root -pnew-password mydb < /etc/mydb.sql
    echo "Leaving script lamp"
    ;;
  stop)
    echo "Stopping script lamp"
    echo "Nothing to do"
    ;;
  *)
    echo "Usage: /etc/init.d/lamp {start|stop}"
    exit 1
    ;;
esac

exit 0

Save the init script as "lamp" as config/chroot_local-includes/etc/init.d/lamp.

You need to create symlinks in the image to the init script for each of the runlevels. You can make sure that the mysql daemon will already be running when the init script is invoked by assigning it a priority greater than the mysql init script. Priority 90 will do this. You can accomplish this using another script that invokes update-rc.d in the chroot before the binary is created. This script should be saved as config/chroot_local-hooks/lamp-update-rcd.sh and be made executable (chmod 755).

{{{#!/bin/sh # lamp-update-rcd.sh

# Make the init script executable chown root:root /etc/init.d/lamp chmod 755 /etc/init.d/lamp

# Create symlinks for each runlevel # eg /etc/rc2.d/S90lamp for runlevel 2, etc update-rc.d lamp defaults 90 10 }}}

These two scripts will create the MySQL root user password and import the database mydb during boot.

Include the Drupal filesystem

Put a copy of your Drupal files into config/chroot_local-includes/var/www.

Build and test the image

# lh_build

Create the final image

Run the image.

In Xfce, set Iceweasel as the startup program.

In Iceweasel, set your Drupal CMS as the home page.

Use scp to copy the contents of the virtual guest's /home/user directory to the host's config/chroot_local-includes/etc/skel. This will ensure that Icweasel will run your website on startup.

Exit the virtual machine and rebuild.

{{{# lh_clean # lh_build }}}