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-config-tools.pl
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.

Compiling

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 \
--enable-thread-safe-client
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 \
--enable-rewrite
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 \
--with-openssl-dir
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 libphp5.so 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
crontabs-1.10-8
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).

Comments

Popular posts from this blog

Writing a Minimal PSR-0 Autoloader

An excellent overview of autoloading in PHP and the PSR-0 standard was written by Hari K T over at PHPMaster.com , and it's definitely worth the read. But maybe you don't like some of the bloated, heavier autoloader offerings provided by various PHP frameworks, or maybe you just like to roll your own solutions. Is it possible to roll your own minimal loader and still be compliant? First, let's look at what PSR-0 mandates, taken directly from the standards document on GitHub : A fully-qualified namespace and class must have the following structure \<Vendor Name>\(<Namespace>\)*<Class Name> Each namespace must have a top-level namespace ("Vendor Name"). Each namespace can have as many sub-namespaces as it wishes. Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system. Each "_" character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR . The "_" character has no special ...

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 Shoulders Composition doesn’t happen in a vacuum. Bach wa...

Learning Prolog

I'm not quite sure exactly I was searching for, but somehow I serendipitously stumbled upon the site learnprolognow.org a few months ago. It's the home for an introductory Prolog programming course. Logic programming offers an interesting way to think about your problems; I've been doing so much procedural and object-oriented programming in the past decade that it really took effort to think at a higher level! I found the most interesting features to be definite clause grammars (DCG), and unification. Difference lists are very powerful and Prolog's DCG syntax makes it easy to work with them. Specifying a grammar such as: s(s(NP,VP)) --> np(NP,X,Y,subject), vp(VP,X,Y). np(np(DET,NBAR,PP),X,Y,_) --> det(DET,X), nbar(NBAR,X,Y), pp(PP). np(np(DET,NBAR),X,Y,_) --> det(DET,X), nbar(NBAR,X,Y). np(np(PRO),X,Y,Z) --> pro(PRO,X,Y,Z). vp(vp(V),X,Y) --> v(V,X,Y). vp(vp(V,NP),X,Y) --> v(V,X,Y), np(NP,_,_,object). nbar(nbar(JP),X,3) --> jp(JP,X). pp(pp(PREP,N...