Skip to main content

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).


Popular posts from this blog

Creepy JavaScript Tracking

I recently began allergy shots so my new Monday morning routine includes me sitting in a doctor's office for 30 minutes (I must wait after receiving the shots and be checked by a nurse to make sure there was no reaction). With nothing else better to do while I waited last week, I started playing around with some JavaScript. This is what I came up with:
<html> <head> <title>Test</title> <script type="text/javascript"> window.onload = function () { var mX = 0,  mY = 0, sX = 0,  sY = 0, queue = [], interval = 200, recIntv = null, playIntv = null, b = document.body, de = document.documentElement, cursor = document.getElementById("cursor"), record = document.getElementById("record"), play = document.getElementById("play"); window.onmousemove = function (e) { e = e || window.event; if (e.pageX || e.pageY) { …

Composing Music with PHP

I’m not an expert on probability theory, artificial intelligence, and machine learning. And even my Music 201 class from years ago has been long forgotten. But if you’ll indulge me for the next 10 minutes, I think you’ll find that even just a little knowledge can yield impressive results if creatively woven together. I’d like to share with you how to teach PHP to compose music. Here’s an example: You’re looking at a melody generated by PHP. It’s not the most memorable, but it’s not unpleasant either. And surprisingly, the code to generate such sequences is rather brief. So what’s going on? The script calculates a probability map of melodic intervals and applies a Markov process to generate a new sequence. In friendlier terms, musical data is analyzed by a script to learn which intervals make up pleasing melodies. It then creates a new composition by selecting pitches based on the possibilities it’s observed. . Standing on ShouldersComposition doesn’t happen in a vacuum. Bach was f…

Geolocation Search

Services that allow users to identify nearby points of interest continue to grow in popularity. I'm sure we're all familiar with social websites that let you search for the profiles of people near a postal code, or mobile applications that use geolocation to identify Thai restaurants within walking distance. It's surprisingly simple to implement such functionality, and in this post I will discuss how to do so.

The first step is to obtain the latitude and longitude coordinates of any locations you want to make searchable. In the restaurant scenario, you'd want the latitude and longitude of each eatery. In the social website scenario, you'd want to obtain a list of postal codes with their centroid latitude and longitude.

In general, postal code-based geolocation is a bad idea; their boundaries rarely form simple polygons, the area they cover vary in size, and are subject to change based on the whims of the postal service. But many times we find ourselves stuck on a c…