Running PHP on Tomcat for Windows

By | September 24, 2013
This setup allows you to call java from within PHP scripts and use PHP with Java based web pages or simply run PHP only based web sites on Apache Tomcat 7.0.42 with PHP 5.
I personally had to setup a similar development environment on my Windows laptop to be able to maintain some web sites.
 Because I ran into some hoops to get this setup and some lack of information I decided to create a post with a step-by-step direction on how to do it.
1. First install PHP. I installed the latest version of PHP for Windows which can be found here http://windows.php.net/download/. I installed the 5.5.4 x86 Thread Safe version.

2. For the installation I created a folder called PHP. Copy the zip file you downloaded to this folder and unzip it.

sec87

1. PHP installation folder

This is what my installation looks like (figure 1.). In order for applications/IDE’s to access PHP the recommendation is to add an environment variable called PHP_HOME. To do that I went to

Computer properties.

2.Computer properties.

Start and double click the Computer option. Select “Properties” from the menu (figure 2.).

Add a new variable to System Variables (figure 3.). You’ll have to re-start your computer for Windows to add the variable to the OS environment.

Also add the path to the folder where you installed PHP to the Path environment variable.

3. Environment variables

3. Environment variables

3. Next I installed the Apache Tomcat server. You can download the server from http://tomcat.apache.org/. I downloaded the 32-bit Windows zip distribution of Tomcat 7.0.42.

The download is a .zip file so I created a folder called Apache and unzipped the files in there.

The Apache Tomcat server requires the variable CATALINA_HOME setup before you can start it. Create an environment variable with that name and assign to it the path to your server’s installation folder which in my case was C:\Apache\apache-tomcat-7.0.42.

4. In order for the Apache server to run it requires the Java runtime to be installed. Go ahead, download and install the Java runtime from here http://java.com/en/download/ie_manual.jsp?locale=en.

sec90

4. Port settings

Once you are done downloading and installing the Java runtime you’ll have to create an environment variable called JRE_HOME. Create the variable and assign to it the path to Java’s runtime folder. In my case it was C:\Program Files (x86)\Java\jre7.

At this point the Apache server should be ready to be started. By default the server runs on port 8080 and not on port 80 as all the web servers. If you need to change this edit the file named server.xml in the floder C:\Apache\apache-tomcat-7.0.42\conf. Change the port number in the settings shown in figure 4.

5. You can now test your installation of Apache Tomcat. Open a Windows command prompt and execute the “startup.bat” DOS script. To shutdown just execute the “shutdown.bat” script. You’ll find these scripts in the folder C:\Apache\apache-tomcat-7.0.42\bin.

To test the server open your browser and type http://localhost:8080. You should be presented with Tomcat’s management page.

sec92

5. Tomcat management page.

6. The previous steps are still not enough to run PHP on Apache Tomcat. For this you need to install a tool called PHP Java Bridge. You’ll find the download page here http://php-java-bridge.sourceforge.net/pjb/download.php. This is a .war file that you’ll have to deploy under Apache. After the download completes unzip the file.

6. Deploy JavaBridge

6. Deploy JavaBridge

7. Install the PHP Java Bridge. To do this you’ll have to access the Manager App (see figure 5.). You’ll need to find out the user id and password. The file containing this information is called tomcat-users.xml and can be found in C:\Apache\apache-tomcat-7.0.42\conf. This is how I have updated it:

<tomcat-users>

<role rolename=”manager-gui”/>

<role rolename=”manager-status”/>

<role rolename=”manager-script”/>

<role rolename=”manager-jmx”/>

<user username=”tomcat” password=”tomcat” roles=”standard,manager-gui,manger-status,manager-script,manager-jmx”/>

</tomcat-users>

 

If your server is running when you make the changes above you’ll need to re-

7. JavaBridge home page.

7. JavaBridge home page.

start Apache.

Click the “Manager App” button and type the user id and password. They should both be “tomcat”. In the “Tomcat Web Application Manager” page go to the section called “WAR file to deploy” and select the JavaBridge.war file you downloaded and unzipped previously. Now press the “Deploy” button.

Once the deployment is complete you should see the folder /JavaBridge under the list of applications. Clicking the folder it should display JavaBridge’s home page.

8. We have now arrived to the last step of this setup. First go to the folder C:\Apache\apache-tomcat-7.0.42\webapps\JavaBridge\WEB-INF\lib and copy the files “JavaBridge.jar”, “php-script.jar” and “php-servlet.jar” to Tomcat’s C:\Apache\apache-tomcat-7.0.42\lib directory.

Next add the following entries to Tomacat’s web.xml file that you find in the folder C:\Apache\apache-tomcat-7.0.42\conf:

<listener>
php.java.servlet.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>PhpFacesServlet</servlet-name>
<servlet-class>php.java.faces.FacesServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>


<servlet>
<servlet-name>PhpJavaServlet</servlet-name>
<servlet-class>php.java.servlet.PhpJavaServlet</servlet-class>

<!– Allow non-local clients. These clients connect to the –>

<!– Leave this off if your server is connected to the internet. –>
<!–
<init-param>
<param-name>allow_http_tunnel</param-name>
<param-value>On</param-value>
</init-param>
–>

<!– Default it the java.log_level from the php.ini. –>
<!– See also the system property php.java.bridge.default_log_level –>
<!–
<init-param>
<param-name>servlet_log_level</param-name>
<param-value>2</param-value>
</init-param>
–>

<load-on-startup>0</load-on-startup>
</servlet>

<!– runs PHP scripts in this web app; remove it if you don’t need this –>
<servlet>
<servlet-name>PhpCGIServlet</servlet-name>
<servlet-class>php.java.servlet.fastcgi.FastCGIServlet</servlet-class>
<init-param>
<param-name>prefer_system_php_exec</param-name>
<param-value>On</param-value>
<param-name>php_include_java</param-name>
<param-value>Off</param-value>
</init-param>
<!– Allow us to override the java.hosts, java.servlet and –>
<!– java.socketname settings from your php.ini so that we can –>
<!– use the current VM (see X_JAVABRIDGE_OVERRIDE_HOSTS). –>
<!– Default is On. –>
<!–
<init-param>
<param-name>override_hosts</param-name>
<param-value>Off</param-value>
</init-param>
–>


<!– c:/php/php-cgi.exe –>
<!–
<init-param>
<param-name>php_exec</param-name>
<param-value>php-cgi</param-value>
</init-param>
–>

<!–
<init-param>
<param-name>max_requests</param-name>
<param-value>50</param-value>
</init-param>
–>

<!–
<init-param>
<param-name>use_fast_cgi</param-name>
<param-value>Off</param-value>
</init-param>
–>

<!– Set this to On in both, the JavaBridge/WEB-INF/web.xml and –>

<!– Default is Off. –>

<init-param>
<param-name>shared_fast_cgi_pool</param-name>
<param-value>On</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PhpJavaServlet</servlet-name>
<url-pattern>*.phpjavabridge</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PhpCGIServlet</servlet-name>
<url-pattern>*.php</url-pattern>
</servlet-mapping>

If Tomcat was running while you made the changes above make sure you re-start it now.

To test that Tomcat is now running PHP create a page called, for example “test.php” and add it to the existing “Examples” folder.  You should find this folder here C:\Apache\apache-tomcat-7.0.42\webapps\examples. Add the line phpinfo(); ?> to the “test.php” page.

When you browse the page you should be able to see something similar to the figure 8. below. Your Apache Tomcat server should now be fully setup to run PHP.

sec95

Figure 8. Test.php page

Drop us a comment or if you have any questions please contact us through our QualTech-Software Solutions or QualTechCloud Integrated Cloud solutions customer form

One thought on “Running PHP on Tomcat for Windows

  1. Pingback: Configure Caching in Apache and IIS 8 Part 1 – Measuring Performance | QualTech Software

Leave a Reply