This tutorial describes how to set up the PHP and database stack in the Jetty web server. First we set up a web application (webapp for short) in Java which can interpret .php files using Quercus. Then we setup the H2 database engine. Last, we setup the part where PHP can connect to H2 while actually thinking it is MySQL that it's connecting to (using H2's MySQL compatibility mode, because PHP does not have support for H2). Here we go!
Ultra short walk-through
- Make sure you have Maven installed.
- Download latest version of Quercus (scroll down, get the WAR file).
- Go to the directory where you downloaded Quercus and install it in your local maven repository by running the Maven command (e.g. for Quercus version 4.0.25):
$ mvn install:install-file -Dfile=quercus-4.0.25.war -DgroupId=com.caucho -DartifactId=quercus -Dversion=4.0.25 -Dpackaging=war - Checkout git repository:
$ git clone https://github.com/webdevelopersdiary/jamp.git
$ cd jamp - Run web server:
$ mvn jetty:run - Wait until you see "[INFO] Started Jetty Server" in the console output and point your browser to http://localhost:8080/
- The document root is located in src/main/webapp/
- Connect to the database in your code using "$pdo = new PDO('mysql:');", mysql_connect() doesn't work due to a driver incompatibility, for more information on this keep on reading.
The long version
Setting up Quercus is quite straight-forward. Download the WAR file of the latest version of Quercus (in my case 4.0.25) and install it in your local Maven repository by running this command from the same location as where you downloaded quercus-4.0.25.war:
mvn install:install-file -Dfile=quercus-4.0.25.war -DgroupId=com.caucho -DartifactId=quercus -Dversion=4.0.25 -Dpackaging=war
<project>
...
<dependencies>
...
<dependency>
<groupId>com.caucho</groupId>
<artifactId>quercus</artifactId>
<version>4.0.25</version>
<type>war</type>
</dependency>
...
</dependencies>
...
</project>
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.4.v20120524</version>
<configuration>
<scanIntervalSeconds>1</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
...
</project>
echo '<?php phpinfo();' > src/main/webapp/phpinfo.php
mvn jetty:run
Congratulations! If you made it this far, you have successfully setup a Java web server that can interpret PHP files! But, we're not there yet, we still have to add database support.
<project>
...
<dependencies>
...
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.167</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
...
</dependencies>
...
</project>
<?php
$pdo = new PDO('mysql:');
$sth = $pdo->prepare('SELECT NOW()');
if($sth) {
$sth->execute();
if($now = $sth->fetchColumn()) {
printf('Successfully executed query on H2 at %s.', $now);
} else {
echo 'Could not execute query.';
}
} else {
echo 'Could not prepare query.';
}
So now you should have your ultra-portable platform-independent PHP, web server and database stack up and running. Happy experimenting! :-)
I would like to see a working version of Code Igniter using the ActiveRecord class running on this stack. Also, it would be nice to have H2's MySQL compatibility mode compatible with Quercus' MySQL driver, so we can run applications that use mysql_connect() on the stack (e.g. phpMyAdmin).
If you want to call PDOStatement::fetchObject(), you should call PDOStatement::fetchObject('stdClass') instead. This is a bug in Quercus (remember I said in the beginning that sometimes Quercus' implementation differs from PHP's). Acccording to the PHP Manual, the default first argument for PDOStatement::fetchObject should be 'stdClass', it seems the folks at Caucho forgot to implement this.
If you want to use a persistent database file instead of the temporary in-memory database, go to src/main/webapp/WEB-INF/jetty-env.xml and change line
<Set name="url">jdbc:h2:mem:;MODE=MYSQL</Set>
<Set name="url">jdbc:h2:filename;MODE=MYSQL</Set>