PHP with JBoss

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Run Both PHP & JSP Using JBOSS Web Server With MySQL As Back End

Download the necessary files

1.Download[1] a JDK on the Sun's website and install it.
2.Download [2] the JBossWeb Win32 zipped archive [ 27 Mb ] on the JBoss.org website.
3.Download [3] the native Win32 PHP module archive [ 8 Mb ] here. 
4.Download [4] MySQL 5.x from here.
5.Download [5] MySQL-Connector-Java from here.

Install MySQL v5.x

Run the executable file and use the following settings:
1. Typical Setup
2. Skip Sign-Up
3. Make sure "Configure the MySQL Server now" is checked
4. "Detailed Configuration"
5. "Developer Machine"
6. "Multifunctional Database"
7. "InnoDB Tablespace Settings" - leave everything default
 8. "Decision Support (DSS)/OLAP"
9. Make sure "Enable TCP/IP Networking" and "Enable Strict Mode" are checked and leave the port number at 
   3306 (at this point, if you have a firewall, it will usually try to access itself on the localhost)
10. "Best Support For Multilingualism" - makes UTF-8 the default character set
11. Check "Install As Windows Service"
12. I recommend checking "Include Bin Directory in Windows PATH"
13. Enter your root password and I would recommend not checking "Enable root access from remote machines"
14. Then hit "execute" and it'll install and set it up.

Install JDK 1.6 Update 3

Install JDK 1.6 Update 3 as usual with default path and settings.

Extract the archives

1.Extract jbossweb-1.0.1.GA-windows-i686.zip to the root of your c: drive.
2.Extract php5servlet-windows-i586-SP1.zip to the root of your c: drive.
3.Move the contents of c:\php5servlet-windows-i586-SP1\PHP folder to c:\php5.
4.Make a copy of the C:\php5\bin\php.ini-recommended to %WINDIR%\php.ini.
5.Open %WINDIR%\php.ini and change “extension_dir” value to c:/php5/bin/ext/. 
  If you want to use PHP short tag <? change short_open_tag value to On. 
  Uncomment “extension=php_mysql.dll” and “extension=php_mbstring.dll” and 
  append “extension=php_mysqli.dll”.

Configure the PHPServlet (All contexts)

1. Find and uncomment this line:
 <Listener className="org.apache.catalina.servlets.php.LifecycleListener"/>
in the file: C:\jbossweb-1.0.1.GA\server\default\deploy\jbossweb.sar\server.xml
2. Modify (if you want) the default port the server will be listening to, here on port 8080:
 <Connector protocol="HTTP/1.1" port="8080" address="${jboss.bind.address}" redirectPort="8443" xpoweredBy="true"/>
3. Find and uncomment the servlets (.php & .phps) declarations:
   <servlet>
       <servlet-name>php</servlet-name>
       <servlet-class>org.apache.catalina.servlets.php.Handler</servlet-class>
       <init-param>
         <param-name>debug</param-name>
         <param-value>0</param-value>
       </init-param>
        <load-on-startup>6</load-on-startup>
   </servlet>
   <servlet>
       <servlet-name>phps</servlet-name>
       <servlet-class>org.apache.catalina.servlets.php.Highlight</servlet-class>
   </servlet>
 in the file: C:\jbossweb-1.0.1.GA\server\default\deploy\jbossweb.sar\conf\web.xml
4. Find and uncomment the servlets (.php & .phps) mappings:
   <servlet-mapping>
       <servlet-name>php</servlet-name>
       <url-pattern>*.php</url-pattern>
   </servlet-mapping>
   <servlet-mapping>
       <servlet-name>phps</servlet-name>
       <url-pattern>*.phps</url-pattern>
   </servlet-mapping>
 in the file: C:\jbossweb-1.0.1.GA\server\default\deploy\jbossweb.sar\conf\web.xml
5. Add/Modifiy one line in the <welcome-file-list> element:
  <welcome-file-list>
       <welcome-file>index.html</welcome-file>
       <welcome-file>index.htm</welcome-file>
       <welcome-file>index.php</welcome-file>
       <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  in the file: C:\jbossweb-1.0.1.GA\server\default\deploy\jbossweb.sar\conf\web.xml

Notes

1.You need to restart the server to validate changes in the %windir%/php.ini
2.Keep your JSP or PHP files in the following directory 
C:\jbossweb-1.0.1.GA\server\default\deploy\jbossweb.sar\ROOT.war

Connect PHP with MySQL

1. Create your database and table:
 Open MySQL Command Line Client.
 mysql> create database test;
 mysql> use test;
 mysql> create people(
        id integer,
        name varchar(20),
        item varchar(20)
        );
2. Create your PHP file named “index.php” and save it in 
   C:\jbossweb-1.0.1.GA\server\default\deploy\jbossweb.sar\ROOT.war.
   <?php
        $username = "root";
        $password = "mat3";
        $hostname = "localhost";	
        $dbh = mysql_connect($hostname, $username, $password)
        or die("Unable to connect to MySQL");
        print "Connected to MySQL";
        $selected = mysql_select_db("test",$dbh) or die("Could not select first_test");
        if (mysql_query("insert into people values('1','Indranil','Burger')")) {
            print "successfully inserted record";
        }
        else {
           print "Failed to insert record";
        }
        mysql_close($dbh);
   ?>

Open your browser and type http://localhost:8080/index.php to test your page.

3. Create your JSP file named “fetch.jsp” and save it in  
   C:\jbossweb-1.0.1.GA\server\default\deploy\jbossweb.sar\ROOT.war.
   <%@ page import="java.sql.*" %>
   <%
     String connectionURL = "jdbc:mysql://localhost/test";
     Connection connection = null;
     Statement statement = null;
     ResultSet rs = null;
   %>
   <html>
     <body>
       <%
         Class.forName("com.mysql.jdbc.Driver").newInstance();
         connection = DriverManager.getConnection(connectionURL, "root", "mat3");
         statement = connection.createStatement();
         rs = statement.executeQuery("SELECT * FROM people");
         while (rs.next()) {
            out.println(rs.getString("name")+"
"); } rs.close(); %> </body> </html>

Open your browser and type http://localhost:8080/fetch.jsp to test your page.


Reference

http://samaxes.com/php-tutorial/jbossweb.php
http://samaxes.com/php-tutorial/jbossweb.php

This article is provided by

Indranil Basu
Engineer
MAT3 Impex Private Limited,
Kolkata, West Bengal,
India.
http://www.mat3impex.com
http://www.nltr.org
E-mail - indranil@mat3impex.com