PHP-on-Tomcat (Part II)

The original goal of being able to run PHP on top of Tomcat was to allow simple development of PHP based applications like WordPress and/or bbPress. To get these working I needed a database.

The wildly popular mySQL database has a nice installer for OS X. Mine installed to /usr/local. I did the install ages ago and forgot what the “root” database user password was. Ooops! No matter, there are some great resources online talking about how to go about resetting permissions. I opted for the incredibly easy, but utterly insecure method (from inside the terminal) :

  1. Stop the existing server
  2. Restart telling it to turn off permissions: mysqld_safe --skip-grant-tables --user=_mysql &
  3. Jump into the mysql shell application: mysql -u root mysql
  4. Change the “root” password: UPDATE mysql.user SET Password=PASSWORD('#########') where user='root';
  5. Persist privilages: FLUSH PRIVILEGES;

Next on the list was exposing my mySQL database to the Tomcat hosted PHP code. Step 1 was making sure I have mySQL JDBC drivers installed, Step 2 was creating a JNDI datasource in the context, and step 3 was getting the PHP to use it.

The officially supported mySQL Connector/J was a quick download. The JAR file for it was dropped into the $CATALINA_HOME/lib directly. Good to go. The context file was more of a challenge, under the $CATALINA_HOME/conf/Catalina/localhost I created an XML config file that matched the name of my webappphp.xml – where I declared the JNDI datasource. It took some tweaking and reading around but finally ended up coming together:

<Context reloadable="true">
    <Resource
        name="jdbc/mydata"
        auth="Container"
        type="javax.sql.DataSource"
        username="root"
        password="#########"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/test"
    />
</Context>

The documentation for Quercus states

Scripts can use the jndi name directly:

<?php

  // standard PHP
  //mysql_connect($host, $username, $password, $dbname);

  // using JNDI lookup
  mysql_connect("java:comp/env/jdbc/myDatabaseName");

?>

But that doesnt help me to run WordPress as I’m not about to go modifying their code. Never fear, there’s another option. The documentation for Quercus also says that you can put an entry into the WEB-INF/web.xml file that forces all database connections to go through the same underlying JNDI datasource:

.
.
.
    <!--
      Tells Quercus to use the following JDBC database and to ignore the
      arguments of mysql_connect().
    -->
    <init-param>
      <param-name>database</param-name>
      <param-value>jdbc/test</param-value>
    </init-param>
.
.
.

So, a quick update to point at my own datasource and wall was ready to go. I downloaded and unzipped wordpress into the PHP webapp directory, pointed a browser at http://localhost:8080/php/wordpress and found myself stepping through the famous 5 minute install. Less than 5 minutes later I was looking at the main page of a fully-functioning WordPress install.

Comments are closed.