Not long ago, Sun released a bundled package of their “Sun Java System Application Server” (Glassfish) and MySQL. I took this as the reason to try out PHP on Glassfish with enabled MySQL support. The initial installation was quite simple but there were some problems with documentation about the usage of MySQL. For those of you who already have a MySQL server running and just want ot test your application on Glassfish, the installation basically is:

  1. Download Glassfish/MySQL Bundle
    Or if you already have glassfish, add the Mysql Connector/J (the jar from the archive) to GLASSFISH/lib
  2. Download Quercus (most recent) and unpack the following files from the archives:
    quercus.jar
    resin-util.jar           to       GLASSFISH/domains/domain1/lib
    script-10.jar
  3. Add the following to GLASSFISH/domains/domain1/config/default-web.xml:
      <servlet>
        <servlet-name>Quercus Servlet</servlet-name>
        <servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
        <init-param>
          <param-name>script-encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
          <param-name>database</param-name>
          <param-value>ExampleMySQL</param-value><!– See below –>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>Quercus Servlet</servlet-name>
        <servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
        <url-pattern>*.php</url-pattern>
        <init>
          <!– <php-ini>
            <sendmail_from>my_email_address</sendmail_from>
            <smtp_username>my_email_username</smtp_username>
            <smtp_password>my_email_password</smtp_password>
          </php-ini> –>
        </init>
      </servlet-mapping>
  4. Launch Glassfish and log in to the administration interface (by default: http://localhost:4848)
    Howto: Glassfish Quickstart Quide
  5. On Resources / JDBC / … in the administration interface add a new connection pool of type MySQL and set User/Password/Port/URI etc. in its properties (look through them when creating and don’t leave them blank). When done, “ping” (the button) your MySQL server from the connection pool view to test if it worked.
    Also add a new “JDBC Resource” that links to the pool and give it a name, in this case ExampleMySQL.
  6. Add a PHP file of your choice to GLASSFISH/domains/domain1/docroot and maybe restart Glassfish once to be sure the database changes are live.
  7. Run your MySQL-enabled PHP script (e.g. http://localhost:8080/index.php).

That’s basically everything. It took me some time to find out about the correct MySQL configuration (it seems that other tutorials I discovered at Google are quite outdated). Now, go and have fun!

Errrm: There are also enterprise clustering features implemented into Glassfish. I am wondering if this can scale a huge PHP site as well as lighttpd/php-fcgi. If anyone knows more details about this, please let me know. And mh: Just a thought: What about Glassfish + Quercus + MySQL + GridGain to scale at least the Webserver part into new dimensions? ;-)

2 Responses to “Glassfish + Quercus (PHP) + MySQL”

  1. glassfish Says:

    I tried the step you described here but unable to connect with mysql with function mysql_connect(”java:comp/env/jdbc/mydatabase”); or

    mysql_connect(”localhost”, “root”, “mypassword”,”mydatabase”);

    I am able to connect with PDO only, for example
    new PDO(”java:comp/env/jdbc/mydatabase”);

    Thanks.

  2. glassfish Says:

    Problem is fixed by adding new Mysql Connector/J http://dev.mysql.com/downloads/connector/j/5.1.html

    Thanks for the article.

Leave a Reply