PHP and MySQL in Tomcat with Quercus

Posted on Updated on

I was recently shown Quercus which is the PHP 5 engine implemented in pure Java and hence, can run in a servlet engine like Tomcat. Caucho suggest running it in Resin but for work reasons, I needed to get it running in Tomcat.

It was very easy to get PHP working, but the MySQL integration took a bit more figuring out. All the info I could find on the web assumed you knew all about DBs in Tomcat and didn’t seem to spell it out.

So here is what I did to get it running in Tomcat 6.0

Add the required jars to tomcat’s lib directory

  1. mysql-connector-java-5.1.5-bin.jar (from mysql’s download page)
  2. quercus.jar (in the quercus zip from caucho)
  3. resin-util.jar (in the quercus zip from caucho)
  4. script-10.jar (in the quercus zip from caucho)

Add the database to tomcats context.xml file

<Resource name="jdbc/mydatabasehandle" auth="Container"

Note: the jdbc url is all one line (up to mysqlEncoding=utf8) but is wrapped by the blog layout.
Note: Those really are &amp; (ampersand A-M-P semicolon). They are passed into the JDBC driver, and it must then convert html chars into text.

Add Quercus support to your web.xml

The quercus zip has an example web.xml file. These pieces below are mostly based on that.
<servlet-name>Quercus Servlet</servlet-name>
<!-- Tells Quercus to use the following JDBC database
and to ignore the arguments of mysqli_connect(). -->
<!-- tell tomcat about .php files -->
<servlet-name>Quercus Servlet</servlet-name>
<!-- tell tomcat about my php file called "myscript" (no extension)
so that URLs like /myscript/somepath will get "/somepath"
in php's $_SERVER["PATH_INFO"] variable. -->
<servlet-name>Quercus Servlet</servlet-name>

Use mysql in your PHP code

$dbh = mysqli_connect();
if ($result = mysqli_query($dbh, $sql)){
while ($row = mysqli_fetch_object($result)){
print $row->fieldName;

Other useful bits and pieces

  • Note the use of the mysqli methods. I was not successful in getting the plain old mysql commands to work.
  • $request is the java request object, so you can do stuff like $request->getUserPrincipal() if you are doing authentication in Tomcat like I was.

7 thoughts on “PHP and MySQL in Tomcat with Quercus

    […] PHP and MySQL in Tomcat with Quercus By netwolfuk Use mysql in your PHP code. $dbh = mysqli_connect(); if ($result = mysqli_query($dbh, $sql)){ while ($row = mysqli_fetch_object($result)){ print $row->fieldName; } } mysqli_close($dbh); … Net Wolf’s Blog – […]

    darjeeling's me2DAY said:
    September 4, 2008 at 01:01

    다즐링의 생각…

    톰캣에서 PHP 돌리기 리접 갈켜주다가 -_-;;…

    max said:
    September 4, 2008 at 13:59

    thanks !

    Mark Bhatamrekar said:
    November 14, 2008 at 06:44

    Nice article, just that I am stuck on the second page of installation,

    Here is the problem :
    My Enviroment : Apache Tomcat 5.5, MySQL 5.0, on windows

    I tried to to install wordpress with your Quercus using your info and

    WIth all the installation, tables are getting created, I am able to go blog/wp-admin/install.php, but the second page is blank, it never comes up.
    (If I go and see the table, user has been created, but my second page does not come)

    Here is what I did, if you can help me out, (I can send you my sample servlet code if you would like)
    –Added the 4 jar files
    –Changed context.xml to add JNDI resource
    –modified web.xml in my application,
    * with Quercus Servlet and mappings as mentioned by you
    * add index.php to the Welcome file list
    — changed the formatting.php and config.php

    Please do let me know if you need any more info

    Mark Bhatamrekar said:
    November 16, 2008 at 08:26

    Thank for the help!
    I was able to install wordpress 2.6.3 and wordpress 2.5.1on tomcat5.5,
    Everything works like a charm except, I am not able to add Categories to the posts. Is anybody facing this problem.
    Here are few changes to make ,
    • Make sure activation.jar and mail.jar (are you in you lib folder, you can get these from JavaMail API)
    • Wordpress worked for me with MySQL Connector version 3.1.14 , some how it fails with 5.0 versions

    shreyansh shah said:
    September 24, 2010 at 06:50


    Thanks for nice article. But i am using pdo to connect database.
    $pdo = new PDO(“java:comp/env/jdbc/mysql”); but here pdo returns null value. please send me a solution for this.


    Sinoea Kaabi said:
    November 4, 2011 at 16:42

    Great article,
    just one thing I am having problems with getting the pretty permalinks

    I am using Apache2.2 HTTPd server, Tomcat5.5, Java6, Quercus and Word Press.

    Any idea how to solve that?

    Don’t know where and how I should configure the Apache httpd.conf file or
    the UrlRewriteFilter.

    I have been struggling for 2 days now.

    I’ll buy you a beer if you can help.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s