LAMP stack on Glassfish

Posted by Dick on August 26, 2008

Quercus is a cracking library from Caucho , the guys who make the Resin appserver. It lets you run PHP on a servlet engine, which sounds like fun.

  • It’s (about) as fast as mod_php
  • you avoid some of those C-based security nightmares
  • it’s small, self-contained and platform neutral
  • Quercus PHP methods are Java methods, so monitoring/profiling is potentially quite sane
  • you can call into JMS queues etc. from PHP

(Ok, that last one didn’t sound so much like fun).

hello world thing

Quercus ships as a WAR file , you just stick your own PHP into it and deploy.

mkdir war; cd war
jar xvf ../quercus-3.1.6.war
echo '<?php phpinfo(); ?>' > index.php
jar cvf ../phpinfo.war .
asadmin deploy ../phpinfo.war

http://localhost:8080/phpinfo does about 950 reqs/sec .

live fire test

It’d be a bit more realistic to try a full, database backed app.

My conversational PHP is no better than my Java. Fortunately, there are around 870 million pimply teenagers who can code against a LAMP stack, so I have plenty of demo apps to choose from.

I’ve gone for Wordpress because I’ve been running it around the place for years.

create a database and connection pool

(Well, I say ‘database’, but I actually mean MySQL since that’s all Wordpress supports. If you already have a MySQL connection pool on your appserver, skip to the ‘build a war’ bit below).

I’ve managed to avoid MySQL so far, so I had to run the (very nice, 64-bit, 5.1) OSX installer .


 export PATH=$PATH:/usr/local/mysql/bin
  mysql_secure_installation # set a root password
  # create a database and user.
  mysqladmin -u root -p create wordpress
  echo "grant all on wordpress.* to 'wordpress'@'localhost' identified by 'wordpress'; " | mysql -u root -p

Add the mysql JDBC driver to $GLASSFISH-HOME/lib, then bounce the appserver.

cp mysql-connector-java-5.1.6-bin.jar /Applications/NetBeans/glassfish-v2ur2/lib/
asadmin stop-domain;asadmin start-domain

Create the connection pool:

asadmin create-jdbc-connection-pool  --datasourceclassname \
com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource \
 --restype javax.sql.DataSource --property \
 User=wordpress:Password=wordpress:URL=jdbc\\:mysql\\://localhost/wordpress\
 wordpress_pool

check it works:

asadmin ping-connection-pool wordpress_pool
Command ping-connection-pool executed successfully.

Finally, make a JNDI reference to it:

asadmin create-jdbc-resource --connectionpoolid=wordpress_pool jdbc/wordpress

build a war to deploy

Get wordpress and quercus tarballs/WARs.

tar zxvf wordpress-2.6.1.tgz # makes a wordpress/ folder
cd wordpress
unzip ../quercus-1.3.6.war # DON'T replace index.php when prompted.
#edit WEB-INF/web.xml and uncomment these lines
   
database
jdbc/wordpress
   
jar cvf ../awesomeblog.war .
asadmin deploy ../awesomeblog.war

Then browse to http://localhost:8080/awesomeblog. It’ll prompt for missing wp-config.php, but
it doesn’t matter what you specify all DB connections are managed by the jdbc/wordpress pool.

Filled out a few test posts, and it manages a good 50 requests/sec without any tuning.

alternatives

It’s pretty simple to enable appserver wide PHP support if you fancy that. Personally I’d rather have standalone WAR files, but YMMV.

Homework assignments:

  • to cluster this, you’ll need a shared directory for file uploads.
  • mass hosting is going to need one WARfile per blog
  • set table_prefix for each blog (so they can share a connection pool)
  • roll up the deployment in an ant / rake task
Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

Comments