Sunny Affairs

How to get php and MySQL running with GlassFish

Thursday Oct 02, 2008

As the title of my blog post suggests, I am going to explain how to turn your computer into a complete web development workstation.

If you are someone like me who is already into web development and is using the AMP stack, you will have MySql already installed and running on your computer. Also, you may have php installed on your system. Thus to try out a new application server like GlassFish you would need to install it separately and then configure it to connect with your existing MySql database and also be able to run php on it. Incidentally I also had GlassFish installed on my computer alongwith Netbeans 6.1. The entire process is pretty straight-forward and simple but it took me quite a lot of googling to find a proper documentation that could help me integrate php and MySQL with GlassFish. Unfortunately until now, I could not find a single consolidated help anywhere. I managed to do it by taking information from various parts of the cloud and now I have created an unified documentation for this issue.

Before I start off I would like to clarify the state of my computer, cause the procedure might differ slightly depending on which Operating System and various configurations you have. Although I believe that the steps to be followed will be largely similar.
I am using Mac OS 10.5.5. I have MySql 5.0.51b installed on my computer using the Mac OS package setup. I have Apache 2.2.8 and php 5.2.6 pre-packaged with Mac OS. Also I have Netbeans 6.1 installed and my GlassFish v2 Update Release 2 came packaged with the Netbeans installer.

Now first to get started with GlassFish application server. My GlassFish installation path is: /Applications/Netbeans/glassfish-v2ur2. To generalize the path for all systems, I shall use $GLASSFISHHOME throughout this blog entry to denote the GlassFish installation folder. To be able to easily use the GlassFish command, it is best to add $GLASSFISHHOME/bin/ to the path of your Terminal/Command Prompt. GlassFish installation has a preconfigured domain domain1. You can read more in the GlassFish Quick Start Guide
To start the server issue the command: asadmin start-domain domain1

You can now access the default GlassFish Welcome Page by opening http://localhost:8080/.



Note that my Apache webserver is also running on port 80 and both of them do not interfere with each other. For you added information, this default welcome page is located here: $GLASSFISHHOME/domains/domain1/docroot/index.html

For administration you can open Admin Console by pointing your browser to http://localhost:4848/ and then login with your admin credentials. By default the username is admin and the password is adminadmin. Using this web-based GUI you can

  • Deploy and undeploy applications
  • Enable, disable, and manage applications
  • Configure resources and other server settings
  • Configure clusters and node agents
  • Manage server instances and clusters
  • Select and view log files
  • Configure load balancers






  • To try out a basic application download hello.war from http://glassfish.dev.java.net/downloads/quickstart/hello.war and place it $GLASSFISHHOME/domains/domain1/autodeploy. This is a special folder and any Web Application Archive (.war) placed in the folder is automatically deployed by GlassFish. You can run this application by pointing your browser to http://localhost:8080/hello.

    Now that your GlassFish server is running fine we can move towards php applications. In case of GlassFish php can be deployed using Quercus which is Caucho Technology's fast, open-source, 100% Java implementation of the PHP language. Download quercus-3.1.6.war. You can place this file in autodeploy and try to open it by pointing your browser to http://localhost:8080/quercus-3.1.6/. If you can view contents properly then php is working fine. quercus-3.1.6.war contains a php page (index.php) which is executed by default. You can unjar quercus-3.1.6.war to view the source.
    unjar -xf quercus-3.1.6.war
    You can edit index.php and add your own php source files and jar it into a new .war file. Make sure you include the WEB-INF and META-INF into your war package.
    jar -cf output.war file1 file2 file3 .... fileN

    Unfortunately no one would prefer to deploy php application as a war package. If you want to keep you source in a folder like you do in Apache, the same can be done here as well. Copy the three files quercus.jar, resin-util.jar and script-10.jar from WEB-INF/lib to $GLASSFISHHOME/lib/. Also add the following lines to $GLASSFISHHOME/domains/domain1/config/default-web.xml

    <servlet>
    <servlet-name>Quercus Servlet</servlet-name>
    <servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
    <init-param>
    <param-name>ini-file</param-name>
    <param-value>WEB-INF/php.ini</param-value>
    </init-param>
    </servlet>

    <servlet-mapping>
    <servlet-name>Quercus Servlet</servlet-name>
    <url-pattern>*.php</url-pattern>
    </servlet-mapping>


    Restart GlassFish so that the settings may take effect.
    asadmin stop-domain domain1 asadmin start-domain domain1

    Now you can place your php sources in $GLASSFISHHOME/domains/domain1/docroot and can open them in web-browser by pointing to http://localhost:8080/filename.php.

    If you want that index.php should open as the default page in a folder you need to add an entry to the welcome-file-list in $GLASSFISHHOME/domains/domain1/config/default-web.xml.

    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.php</welcome-file>
    </welcome-file-list>


    Finally to write applications that would interact with MySQL database, you need to download the jdbc-driver for MySQL from http://dev.mysql.com/downloads/connector/j/5.1.html and place mysql-connector-java-5.1.6-bin.jar in $GLASSFISHHOME/lib/. Now restart GlassFish and then login into the Admin Console. Go to Resources > JDBC > Connection Pools and create a new connection pool.



    Enter the following details:

    Name: (give an appropriate name)
    Resource Type: javax.sql.DataSource
    Database Vendor: mysql



    Select Next. Now enter the Data Source Class Name as com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource and add the following additional properties:
    serverName: 127.0.0.1 (just the localhost)
    port: 3306 (default port)
    user: (an existing user allowed to connect to your mysql db)
    password: (the users password)
    databaseName: (the name of the database you want to connect to)



    Leave everything else as the default values and click Finish.

    To test the connectivity with MySQL you can select your newly created connection pool and PING the database.





    A "Ping Succeeded" would confirm connectivity.

    Now you can write php scripts that would connect with MySQL using mysql_connect("localhost", "username", "password").

    I get the following warning whenever MySQL connection is being established by php for the first time after server restart since I am using an older MySQL database:

    /Applications/NetBeans/glassfish-v2ur2/domains/domain1/docroot/drug/connect.php:3: Warning: Your MySQL Connector/J JDBC 5.1.6 driver may have issues with column/table aliases and DESCRIBE statements. The recommended JDBC version is 3.1.14. [mysql_connect]

    It can be solved by using an older JDBC driver.

    Now that your php applications can run on GlassFish, you can exploit the features that Glass Fish has to offer. You may also download the Mysql-GlassFish packaged installer from here. Maybe it is easier to set things up with this installer.

    Also the Sun Student Reviews Contest is going on at present in which GlassFish and MySQL is needed to be used. Hopefully setting up GlassFish and MySQL would not be a hindrance in participating in this contest.

    [7] Comments
    Like this post? del.icio.us | furl | slashdot | technorati | digg