This is a quick tutorial on using Maven2 to build PHP web applications and deploy them with Jetty. Maven is a powerful build system which makes projects of the same type to follow a similar structure. Jetty is a light weight HTTP server ideal for integration tests. It also has a highly configurable Maven plugin which can be hooked into the build life cycle.

Dependencies

Assuming you already have a Java SDK installed you’ll need to install Maven and PHP. On a Debian-based system the easiest way to do this is using apt-get.

sudo apt-get install maven2 php5-cgi php5-cli

maven-php-plugin

A PHP plugin has recently been released for Maven which allows you to run PHP unit tests and package PHP web applications.

Enable the maven-php-plugin by updating your ~/.m2/settings.xml file to contain the following:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <profiles>
    <profile>
      <id>profile-php-maven</id>
      <pluginRepositories>
        <pluginRepository>
          <id>release-repo1.php-maven.org</id>
          <name>PHP-Maven 2 Release Repository</name>
          <url>http://repo1.php-maven.org/release</url>
          <releases>
            <enabled>true</enabled>
          </releases>
        </pluginRepository>
        <pluginRepository>
          <id>snapshot-repo1.php-maven.org</id>
          <name>PHP-Maven 2 Snapshot Repository</name>
          <url>http://repo1.php-maven.org/snapshot</url>
          <releases>
            <enabled>false</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
      <repositories>
        <repository>
          <id>release-repo1.php-maven.org</id>
          <name>PHP-Maven 2 Release Repository</name>
          <url>http://repo1.php-maven.org/release</url>
          <releases>
            <enabled>true</enabled>
          </releases>
        </repository>
        <repository>
          <id>snapshot-repo1.php-maven.org</id>
          <name>PHP-Maven 2 Snapshot Repository</name>
          <url>http://repo1.php-maven.org/snapshot</url>
          <releases>
            <enabled>false</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>profile-php-maven</activeProfile>
  </activeProfiles>
 </settings>

Create a skeleton project from an archetype

mvn archetype:generate -DarchetypeGroupId=org.phpmaven -DarchetypeArtifactId=php5-web-archetype -DarchetypeVersion=1.0 -DgroupId=com.company -DartifactId=php-webapp 

You now have a Maven project set up to use PHP. When you package this project it will create a ZIP file which can be deployed to your server.

Use Jetty for deployment

Move the index.php created in src/main/webapp into src/main/php.

Create a web.xml file within src/main/webapp/WEB-INF with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Sample PHP Application</display-name>
    <servlet>
        <servlet-name>PHP</servlet-name>
        <servlet-class>org.mortbay.servlet.CGI</servlet-class>
        <init-param>
            <param-name>commandPrefix</param-name>
            <param-value>/usr/local/bin/php-cgi-fix</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>PHP</servlet-name>
        <url-pattern>/index.php/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.php</welcome-file>
    </welcome-file-list>
</web-app>

Create /usr/local/bin/php-cgi-fix and enter the following:

#!/bin/bash
export SCRIPT_FILENAME=$1
/usr/bin/php-cgi

Inside the pom.xml add the following plugin configuration:

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <version>6.1.10</version>
  <configuration>
    <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
    <webAppSourceDirectory>${basedir}/src/main/php</webAppSourceDirectory>
  </configuration>
</plugin>

Run the Jetty server with the following command

mvn jetty:run

If you have any services which are bound to port 8080, like Tomcat, you’ll have to stop these first.

When you navigate to http://localhost:8080/ you should see the PHP webapp listed. Clicking on this URL will execute the index.php.