Saturday, September 26, 2009

Creating a CentOS-Based LAMP Virtual Image

In doing some preliminary research and planning for a client's new project, I determined his current in-house deployment platform would not be sufficient given his requirements. Specifically, the project calls for a moderate amount of URL re-writing and the ability to programmatically FTP files to a remote host. The client is running IIS on Windows Server 2008; I’m not too keen on ISAPI rewrite and IIS Rewrite seems to have fallen off the face of the Internet, and the ftp_ssl_connect() function is only available in PHP if both the ftp module and OpenSSL support are statically built-in so we would have to maintain a build environment for him, too. A LAMP-stack makes more sense. Apache can rewrite URLs with mod_rewrite and compiling PHP is a more supported practice on Linux than it is on Windows.

I discussed the obstacles and some possible solutions with the client and he's okay with LAMP. Instead of bringing in more hardware, though, I suggested taking advantage of virtualization. I assured him I could create a virtual platform that would provide us with everything we need, appear as a new machine on his network, and run directly on top of Windows Server 2008.

Installing CentOS

Originally I wanted to use the new Slackware64, but VMware-Tools proved too much of a struggle to install and I didn't feel comfortable using it for a client's project because of that. I eventually settled on CentOS 5.3 instead.

I fired up the trial version of VMWare Workstation to configure a basic machine image... though I have VMware Workstation 6.5, I chose to set the virtual machine's hardware compatibility for Workstation 5 and compatible with ESX Server. I figured this will give us some flexibility if we need to move the image to bare-metal in the future. CentOS is built from RHEL sources, so I was able to set the Guest Operating System as Red Hat Enterprise Linux 5 and use any Red Hat-specific documentation VMware has.

I tried to keep the installation small, so I unchecked everything in Anaconda-- including the Base packages. I still got packages what I feel are unnecessary dependencies (Requiring wireless-tools on a sever installation, for example. Seriously, Red Hat!), but I guess I can live with it and it won't matter much to the client.

Once CentOS was installed and booted and I was logged in, I needed to install some packages (and their dependencies) with yum that I didn't install during the installation:
  • autoconf

  • curl-devel

  • freetype-devel

  • gcc

  • gcc-c++

  • libjpeg-devel

  • libpng-devel

  • libxml2-devel

  • lynx

  • make

  • ncurses-devel

  • ntp

  • openssl-devel

  • patch

  • perl

  • sendmail

  • wget

  • which

  • zlib-devel
Notice I didn't install Apache, MySQL, or PHP. That's because I like to compile and install the major software from source. This way I can make sure they're up to date and configure their builds exactly how I need them.

Configuring Mapped Directories

I want to keep the application's data separate from the virtual image so I wouldn't be constrained by the size of the image (trying to explain why he couldn't save more than a gig of data when it was running on a physical server with 100 gigs of free drive space wouldn't be fun). The next task was to create shared data directories on the host and install VMware-Tools so I could map them. I created a directory shared as apache to hold the bulk of the application's code (.php, .html, etc), and mysql to hold the database's tables.

The VMware documentation describes the VMware-Tools installation process in detail, but it's no more difficult than selecting "VM" -> "Install VMware tools..." in VMware Workstation, and then proceeding to install the VMware-Tools RPM in CentOS.
mount /dev/cdrom /media
rpm -Uvh /media/ VMwareTools-7.8.5-156735.i386.rpm
umount /media
VMware adds the following to /etc/fstab:
# Beginning of the block added by the VMware software
.host:/ /mnt/hgfs vmhgfs defaults,ttl=5 0 0
# End of the block added by the VMware software
That entry will make the shared folders on from the host operating system accessible as /mnt/hgfs/apache and /mnt/hgfs/mysql. Everything within them owned by root with global read, write, and execute permissions. There's not much that can be done about the lax permissions, but I could at least have the files owned by a more appropriate user than root. I wanted to have them each mounted under /srv instead of /mnt/hgfs as well to be a little more LSB compliant (suck it, /var/www!), so I replaced their entry with my own:
.host:/apache   /srv/apache   vmhgfs   defaults,ttl=5,uid=99,gid=99   0 0
.host:/mysql /srv/mysql vmhgfs defaults,ttl=5,uid=27,gid=27 0 0
It would be nice if future version of VMware will have a more flexible HGFS driver-- but this will be sufficient for the task at hand. At last I could install Apache, MySQL, and PHP.


There isn't anything too exciting about installing Apache, MySQL, and PHP from source to talk about, so I'll just share with you my configure options.
MySql Enterprise 5.0.88sp2
./configure \
--prefix=/usr/local/mysql \
--localstatedir=/srv/mysql \
--with-unix-socket-path=/tmp/mysql.sock \
--with-mysqld-user=mysql \
--without-debug \
--with-archive-storage-engine \
--with-csv-storage-engine \
--with-federated-storage-engine \
--disable-maintainer-mode \
--enable-assembler \
--enable-largefile \
--enable-local-infile \
Apache 2.2.13
CFLAGS=-O3 ./configure \
--prefix=/usr/local/apache \
--with-pcre \
--disable-status \
--enable-mods-shared=all \
--enable-so \
--enable-ssl \
--enable-setenvif \
PHP 5.0.3
CFLAGS=-O3 ./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-pdo-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-gd \
--with-jpeg-dir=/usr/lib \
--with-freetype-dir \
--with-curl \
--with-openssl \
--enable-ftp \
After that I needed to open CentOS's firewall to allow HTTPS traffic using system-config-securitylevel-tui, and change the security context of the module for Apache because SELinux is enabled.

Final Housekeeping

There were only a few minor housekeeping things to attend to after I had everything installed. I had to add a couple kernel parameters and configure ntp according to VMware's Time Keeping Best Practices for Linux so the time didn't drift. It was also important that I configure logrotate to rotate Apache and MySQL's log files as I did not install them via RPM. Otherwise they could grow unwieldy and use up all the space I had allocated for the virtual image.

So in short order I had not only a sane platform for deployment, but one I could easily clone and use for development as well. The client only needs the free VMware Player software to use the image. The data directories are on the host operating system alongside the image so they are not constrained by the size of the image and can be backed-up independently of the image. When necessary, upgrading the virtual platform can be done independently of the data.

Update 10/04/2009: It appears the above procedure didn't install a cron daemon, though it did install crontab files-- now isn't that interesting!
rpm -qa | grep cron
yum install vixie-cron resolved the issue. Don't forget to issue chkconfig crond on so it starts automatically, and /etc/init.d/crond start to start cron for the current session (so you don't have to reboot).