WAMP Tomcat Environment
 
   
 
 
 
  www.ourbabynamer.com
 
 
 
 
 
 
 

WAMP using Tomcat, PHP, MySql

Windows Vista / Tomcat / MySql / PHP
Tomcat 6.0.16
Java jdk/jre 1.6.0_03
MySql 5.0
PHP 5.2.5

Front Tomcat with Apache and mod_rewrite
Ilene Mortman, March 2008

Intro Downloaded The Basics 7 zip
JDK Tomcat Application Folder HTML
JSP Servlets PHP MySql
MySql and PHP MySql and Servlets Command scripts References

Below is a brain dump of the steps I took to setup a Vista machine for web development. It's a basic WAMP environment but Tomcat was used instead of Apache. If you are sure you will only be doing PHP development and no java develpment you are better off setting up with the Apache web server rather than the Tomcat servlet container. This setup allows both PHP pages and servlets to access MySql.

I usually tried to take the most basic level when doing the downloads (.zip) files rather than installers. While researching the answers to some of the problems I ran across, it also became apparent that sometimes jar and/or dll files were missing if you took the installers rather than the zip files, so its probably the safer bet.

I setup tomcat and mysql to run from the command line rather than as services because I had no need for them to be started automatically at machine startup and just found it easier this way. I created startup/shutdown scripts and put links to them in the quicklaunch bar.

This is a basic setup to get things up and running. There is no IDE or Ant, etc. integration discussed here. Just notepad editing and command line commands.

Related test code is generally listed at the bottom of each section.

Hope these notes save some other poor slob some time.

What I downloaded...
7z457.exe
apache-tomcat-6.0.16.zip
jdk-6-doc.zip
jdk-6u3-windows-i586-p.exe
mysql-connector-java-5.1.5.zip
mysql-essential-5.0.45-win32.msi
pecl-5.2.5-Win32.zip
php-5.2.5-Win32.zip

From the following sites...
http://www.7-zip.org/
http://tomcat.apache.org/
http://java.sun.com/javase/downloads/index.jsp
http://dev.mysql.com/downloads/connector/j/5.1.html
http://dev.mysql.com/downloads/
http://pecl.php.net/
http://www.php.net/

The Basics
7 zip was downloaded because I had problems using the native unzip on Vista. I did have some problems unzipping into specific directories from 7 zip so I unzipped to the desktop and moved the folders manually from there.

To set an environment variable in Vista...
Click on Start, Computer (on right), System Properties (on top), Advanced System Settings (top left), Continue (in response to the UAC), Environment Variables
Then Click on New or Edit in the System variables section as appropriate. When adding a path to a variable, put a semi-colon after the last path in the list and then add your path.

To bring up a command window...
Click on Start, and type in cmd in the bottom text field. If entering a command for a file in a particular directory don't forget to change to the directory which the file is in.

Editing Files...
To avoid any hidden format characters use notepad for editing/creating files.  


A.  Download 7 zip

  1. Download and run 7z457.exe. Move the 7-Zip folder to Program Files.
  2. You may also want to add a link to the exe to your quick launch bar.

B.  Download, Configure and Test JDK

  1. Download and run jdk-6u3-windows-i586-p.exe Move the jdk1.6.0_03 folder to Program Files\Java.
  2. Download and unzip jdk-6-doc.zip. Move the docs folder to Program Files\Java. Click on docs\index.html, click on API doc link and bookmark.
  3. Add C:\Program Files\Java\jdk1.6.0_03\bin to the PATH environment variable.
  4. Test by typing in at the command prompt...
    java -version
    javac -help

C.  Download, Configure and Test Tomcat

  1. Download and unzip apache-tomcat-6.0.16.zip. Unzip the zip file using 7-zip and place the folder under Program Files.
  2. Set the environment variable JAVA_HOME. For example,
    JAVA_HOME=C:\Program Files\Java\jdk1.6.0_03
  3. To test, start the server by double clicking on Program Files\apache-tomcat-6.0.16\bin\startup.bat. Bring up a browser and type http://localhost:8080. You should see the tomcat home page. To stop the server double click on Program Files\apache-tomcat-6.0.16\bin\shutdown.bat.

C1.  Set-Up an Application Folder for your testing

  1. Note that folder names are case sensative so name as stated here.
  2. If they don't already exist create the folders apache-tomcat-6.0.16\conf\Catalina and apache-tomcat-6.016\conf\Catalina\localhost.
  3. In apache-tomcat-6.0.16\conf\Catalina\localhost create a "context fragment" xml file of the same name as your application directory e.g. phpFun.xml. Cut and paste the context line below and put into the context fragment file.
  4. Under apache-tomcat-6.0.16\webapps create a folder for your application, e.g. phpFun.
  5. In that folder create the folder WEB-INF and in that folder create the folders classes and lib.
    <Context debug="0" reloadable="true" privileged="true"/>
    

C2.  Test Tomcat with HTML

  1. Cut and paste the code below into a file using notepad and save in your application directory.
  2. Start tomcat. Bring up a browser and type http://localhost:8080/phpFun/test.html.
  3. Stop tomcat.
    <HTML>
    HTML TEST
    </HTML>
    

C3.  Configure and Test Tomcat for JSP

  1. Add the jsp-api.jar to the CLASSPATH environment variable. For example,
    CLASSPATH=C:\Program Files\apache-tomcat-6.0.16\lib\jsp-api.jar
  2. Cut and paste the code below into a file named test.jsp and save it in your application directory.
  3. Start tomcat. Bring up a browser and type http://localhost:8080/phpFun/test.jsp.
  4. Stop tomcat.
    <HTML>
    <HEAD><TITLE>JSP Test</TITLE></HEAD>
    <BODY>
    <H1>JSP Test</H1>
    Time: <%= new java.util.Date() %>
    </BODY>
    </HTML>
    

C4.  Configure and Test Tomcat for Servlets

  1. Add the servlet-api.jar file to the CLASSPATH environment variable. For example,
    CLASSPATH=C:\Program Files\apache-tomcat-6.0.16\lib\servlet-api.jar
  2. Servlets may be run from an application directory or from the ROOT directory using the invoker servlet. The advantage of using the invoker servlet is no further configuration is necessary. The advantage of using a folder per application is it is compartmentalized and easier to maintain and deploy. The current version of Tomcat is not configured to use the invoker servlet by default. To be able to run servlets using the invoker servlet do the following...
    1. In the apache-tomcat-6.0.16\conf\web.xml file uncomment the invoker servlet and its servlet mapping.
    2. In the apache-tomcat-6.0.16\conf\context.xml file add reloadable="true" and privileged="true" to the Context tag.
  3. To test a simple hello world servlet, cut and paste the code below into a file named HelloServlet.java using notedpad.
  4. To test using the invoker servlet place the HelloServlet.java file in the apache-tomcat-6.0.16\webapps\ROOT\WEB-INF\classes directory.
  5. Bring up a command window, change directories to the classes folder and type the command javac HelloServlet.java. The HelloServlet.class file will be created.
  6. Start Tomcat. Bring up a browser and type http://localhost:8080/servlet/HelloServlet.
  7. Stop Tomcat.
      import java.io.*;
      import javax.servlet.*;
      import javax.servlet.http.*;
    
      public class HelloServlet extends HttpServlet {
    
        public void doGet( HttpServletRequest request, 
                           HttpServletResponse response ))
          throws ServletException, IOException {
     
            response.setContentType( "text/html" );    
            PrintWriter out = response.getWriter();
            out.println( "" );
    	out.println( "" );
    	out.println( "HELLO SERVLET" );
    	out.println( "" );
    	out.println( "" );	
        }
      }
    
  8. To test using your application directory, place the HelloServlet.java file in the apache-tomcat-6.0.16\webapps\phpFun\WEB-INF\classes directory.
  9. Bring up a command window, change directories to the classes folder and type the command javac HelloServlet.java. The HelloServlet.class file will be created.
  10. Create a web.xml file in the WEB-INF directory. Cut and paste the lines below into the file.
  11. Start Tomcat. Bring up a browser and type http://localhost:8080/phpFun/HelloServlet.
  12. Stop Tomcat.
    <?xml version="1.0" encoding="ISO-8859-1"?> 
    <!DOCTYPE web-app PUBLIC 
      "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
      "http://java.sun.com/dtd/web-app_2_3.dtd"> 
    <web-app> 
    
    <servlet> 
      <servlet-name>HelloServlet</servlet-name> 
      <servlet-class>HelloServlet</servlet-class> 
    </servlet>
    
    <servlet-mapping>
      <servlet-name>HelloServlet</servlet-name> 
      <url-pattern>/HelloServlet</url-pattern>
    </servlet-mapping>
    
    </web-app>
    

D.  Download, Configure and Test PHP using Tomcat

  1. Download and unzip php-5.2.5-Win32.zip. Unzip the zip file using 7-zip and place the folder under Program Files.
  2. Add C:\Program Files\php-5.2.5-Win32;C:\Program Files\php-5.2.5-Win32\ext; to the PATH environment variable.
  3. Copy php.ini-dist, in e.g. C:\php-5.2.5-Win32, to php.ini
  4. Tomcat is primarily a (Java) Servlet engine, and because of this php doesn’t run natively so you need to download a servlet and configure it to handle php requests.
    1. Download pecl-5.2.5-Win32.zip.
    2. Uncomment the line (remove semi-colon at the beginning) in php.ini:
      ;extension=php_java.dll
    3. Extract php5servlet.dll from the pecl zip file using 7-zip and place it in the php folder C:\php-5.2.5-Win32.
    4. In the application directory which you created earlier, e.g. phpFun modify the file WEB-INF\web.xml which you also created earlier. Within the web-app tags add the following lines to the servlet and servlet-mapping sections respectively.
      	<servlet> 
       	 <servlet-name>php</servlet-name> 
       	 <servlet-class>net.php.servlet</servlet-class> 
      	</servlet> 
      	<servlet> 
      	 <servlet-name>php-formatter</servlet-name> 
      	 <servlet-class>net.php.formatter</servlet-class> 
      	</servlet> 
      	<servlet-mapping> 
      	 <servlet-name>php</servlet-name> 
      	 <url-pattern>*.php</url-pattern> 
      	</servlet-mapping> 
       
      	<servlet-mapping> 
      	 <servlet-name>php-formatter</servlet-name> 
      	 <url-pattern>*.phps</url-pattern> 
      	</servlet-mapping> 
      
    5. Extract php5srvlt.jar from the pecl lib using 7zip into e.g. C: and unjar it using the following command from a command window:
      jar svf php5srvlt.jar
    6. Modify the files c:\net\php\reflect.properties and servlet.properties to change the line library=phpsrvlt to library=php5servlet and save them. This indicates the file name of the dll file which is loaded by the Java application to serve the requests. This has no connection with the name of the jar file which can be anything.
    7. Re-create the jar file by typing the following command in the command window.
      jar cvf php5servlet.jar net\php\*.*
    8. Copy the jar file into the WEB-INF\lib directory of the application directory which you created earlier (e.g. apache-tomcat-6.0.16\webapps\phpFun\WEB-INF\lib ).
    9. Update September 2009, place the jar file in apache-tomcat-6.0.16\lib. If you have more than one application running PHP and put the jar file in the lib directory of each of the applications you will get an error.
      java.lang.UnsatisfiedLinkError: 
                  Native Library C:\php\php5servlet.dll 
                  already loaded in another classloader
  5. Cut and paste the code below into a file named test.php and save it in your application directory.
  6. Start tomcat. Bring up a browser and type http://localhost:8080/phpFun/test.php. You should get an information screen with php version and environment information.
  7. Stop tomcat.
    <?php phpinfo(); ?>
    

E.  Download, Configure and Test MySql

  1. Download mysql-essential-5.0.45-win32.msi. This is the essentials version. At this point I ventured from taking the most basic version as I believed this version would suit my purposes and I was running out of steam. The essentials version does not have an embedded server or benchmark suite so if you think you'll be needing these you'll need to take a different version. It does have a config wizard. Double click and run it. The wizard automatically sets MySql up as a service, I set it up as an application instead. Put the resulting folder in the Program Files directory.
  2. Add C:\Program Files\MySQL\MySQL Server 5.0\bin to the PATH environment variable.
  3. Test the MySql installation. From a command window type the following...
    • mysqld_nt --console to start
    • mysqlshow -u root
    • mysqladmin -u root to get a list of admin options
    • If you are running the test commands and getting the "Access denied for user 'ODBC'@localhost" error it is because you are trying to connect to the MySql DB server as anononymous user who doesn't have access to the specified DB. You must use the root user.
    • mysqladmin -u root shutdown to stop

E1.  Configure and Test MySql with PHP and Tomcat

  1. Add the environment variable PHPRC and set it to be your PHP directory, e.g. C:\Program Files\php-5.2.5-Win32.
  2. In php.ini uncomment the extension lines php_mysql.dll and php_mysqli.dll. The second dll is used to access functionality provided by MySql 4.1 and above.
  3. Cut and paste the code below into a file named testWmysql.php and put it in your test directory e.g. apache-tomcat-6.0.16\webapps\phpFun.
  4. Start Tomcat and Mysql.
  5. Bring up a browser and type http://localhost:8080/phpFun/testWmysql.php. You should get a few lines of related information.
  6. Stop MySql and Tomcat.
    <HTML>
    
    <BODY>
    
    <?php
    
    // CONNECT
    $dbServer = 'localhost';
    $dbUser = 'root';
    $dbPswd = '';
    $dbName = 'mysql';  // information_schema, mysql, test
    $link = mysql_connect( "$dbServer", "$dbUser", "$dbPswd" ) 
    	or die ( "Couldn't connect" );
    print "Connected successfully<BR>";
    
    
    // SELECT THE MYSQL DATABASE
    $select = mysql_select_db( $dbName ) 
    	  or die( "Could not select the database '" . $db . 
            	  "'.  Are you sure it exists?" );
    
    
    // PERFORM A SQL QUERY 
    $query = "SELECT * FROM user";
    $result = mysql_query( $query ) or die( "Query failed" );
    
    
    // PRINT THE RESULT OF THE FIRST ROW (row counting starts at zero)
    printf( "Host: %s
    \n", mysql_result( $result, 0, "Host" ) ); printf( "User: %s
    \n", mysql_result( $result, 0, "User" ) ); printf( "Grant privilege: %s
    \n", mysql_result( $result, 0, "Grant_priv" ) ); // FREE THE RESULT SET mysql_free_result( $result ); // CLOSE THE CONNECTION mysql_close( $link ); ?> </BODY> </HTML>

E2.  Configure and Test MySql with Servlets and Tomcat

  1. Download and unzip mysql-connector-java-5.1.5.zip. Unzip the zip file using 7-zip and place the folder under Program Files. ConnectorJ is the oficial JDBC driver for MySql.
  2. Copy mysql-connector-java-5.1.5-bin.jar to your webapp's lib directory, e.g. apache-tomcat-6.1.16\webapps\phpFun\WEB-INF\lib.
  3. Cut and paste the code below into a file named JavaWmysql.java and put it in your test folder's classes folder e.g. apache-tomcat-6.0.16\webapps\phpFun\WEB-INF\classes.
  4. Bring up a command window. CD to the appropriate directory and build using javac JavaWmysql.java.
  5. Add appropriate web.xml entries...
    For example in apache-tomcat-6.0.16\webapps\phpFun\WEB-INF\web.xml, add the following lines
    <servlet> 
      <servlet-name>JavaWmysql</servlet-name> 
      <servlet-class>JavaWmysql</servlet-class> 
    </servlet> 
    
    <servlet-mapping>
      <servlet-name>JavaWmysql</servlet-name> 
      <url-pattern>/JavaWmysql</url-pattern> 
    </servlet-mapping>
    
  6. Start Tomcat and Mysql.
  7. Bring up a browser and type http://localhost:8080/phpFun/JavaWmysql. You should get a few lines of related information.
  8. Stop MySql and Tomcat.
      import java.io.*;
      import javax.servlet.*;
      import javax.servlet.http.*;
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.SQLException;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
    
    
      public class JavaWmysql extends HttpServlet {
      
    
        public void doGet( HttpServletRequest request,
                           HttpServletResponse response )
     
          throws ServletException, IOException {
    
            response.setContentType( "text/html" );
            PrintWriter out = response.getWriter();
            out.println( "" );
    	out.println( "" );
    
            Connection conn = null;
    
    	// CONNECT
    	// SELECT THE MYSQL DATABASE
            try
            {
              String dbUser = "root";
              String dbPswd = "";
              String dbUrl = "jdbc:mysql://localhost/mysql";
              Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
              conn = 
                DriverManager.getConnection( dbUrl, dbUser, dbPswd );
              out.println( 
                "Database connection established.<BR>" );
    	  // could have also done...
              // DM.gC( 
              //   "jdbc:mysql://localhost/mysql?user=root&password=" );
    	  // To increase the portability, the connection parameters 
    	  // (host, database, user name, and password) may be stored 
    	  // in a Java properties file and read in at runtime. 
    
    
    	  // PERFORM A SQL QUERY 
       	  Statement s = conn.createStatement();
       	  s.executeQuery( "SELECT * FROM user" );
       	  ResultSet rs = s.getResultSet();
       	  int count = 0;
       	  while( rs.next() )
       	  {
                ++count;
    	    // PRINT THE RESULT OF THE FIRST ROW
    	    if (count == 1)
                {
    	      out.printf( "Host: %s<br>\n", 
                              rs.getString( "Host" ) );                  
    	      out.printf( "User: %s<br>\n", 
                              rs.getString( "User" ) );
    	      out.printf( "Grant privilege: %s<br>\n",
                              rs.getString( "Grant_priv" ) );
                }
              }
    
              // FREE THE RESULT SET 
       	  rs.close ();
       	  s.close ();
       	  out.println( count + " rows were retrieved" );
            }
    
            catch (Exception e)
            {
              //System.err.println
    	  out.println( 
                  "Cannot connect to the database server.<BR>" + 
                  e + "<BR>" );
            }
    
            finally
            {
              if (conn != null)
              {
                // CLOSE THE CONNECTION 
                try
                {
                  conn.close();
                  //System.out.println
    	      out.println( 
                      "Database connection terminated.<BR>" );
                }
                       catch (Exception e) { /* ignore close errors */ }
              }
            }
    
            out.println( "</body>" );
            out.println( "</html>" );  
    
         }
      }
    
    

F.  Create command scripts to start and stop Tomcat and MySql

  1. Create the CATALINA_HOME environment variable and set it to your Tomcat folder, e.g. C:\Program Files\apache-tomcat-6.0.16
  2. In the Program Files folder create a folder named Batch.
  3. Create the file startEnv.bat in the folder and add the following lines to it.
    REM
    REM  USE CALL to call another batch file from this batch file
    REM
    
    CALL "C:\Program Files\apache-tomcat-6.0.16\bin\startup.bat"
    mysqld-nt --console
    
  4. Create the file stopEnv.bat in the folder and add the following lines to it.
    REM
    REM  USE CALL to call another batch file from this batch file
    REM
    
    mysqladmin -u root shutdown
    CALL "C:\Program Files\apache-tomcat-6.0.16\bin\shutdown.bat"
    
  5. Drag the two .bat files over the Quick Launch bar to create Quick Launch entries for them.
  6. Test by running the startEnv.bat file and testing some of your previously created items.

G.  References

    7-Zip
    allow activex for local files
    Configure Tomcat 6 to serve applications in other than default directory (Apache-Tomcat forum at JavaRanch)
    Configuring & Using Apache Tomcat 6
    difference between Apache HTTP server and Tomcat (Apache-Tomcat forum at JavaRanch)
    Extracting the Contents of a JAR File (The Java™ Tutorials Deployment Packaging Programs in JAR Files)
    Interfacing MySQL with PHP and Perl DBI
    MySQL AB MySQL 5.0 Reference Manual 2.4.8.1 Choosing An Installation Package
    ONJava.com -- Top Ten Tomcat Configuration Tips
    Server Configuration Reference - The Context Container
    Using JDBC with MySQL, Getting Started
    UsingPhp - Tomcat Wiki
    Writing JDBC Applications with MySQL