Sun Java Solaris Communities My SDN Account

Article

Writing Your First PHP Web Application on the OpenSolaris OS

 
By Rick Palkovic, December 2008  

Writing a web application with PHP on the OpenSolaris OS is easy using the NetBeans IDE and the tools in Sun Web Stack. This article shows you how to create a simple PHP program in this environment.

The intended audience is developers who are new to the OpenSolaris OS and Sun Web Stack.

Contents
Requirements

In this tutorial, you use the OpenSolaris OS, the NetBeans IDE, and components of the Sun Web Stack to create a simple PHP program. Specifically, you will use PHP, the NetBeans IDE, and the Apache web server.

Download the required software as follows:

  • OpenSolaris OS – You can download OpenSolaris for free. You can also order a free OpenSolaris CD-ROM. This tutorial uses OpenSolaris 2008.11. See the Getting Started Guide for installation instructions.
     
  • Sun Web Stack – After you have installed and started the OpenSolaris OS, you can install the Sun Web Stack. Sun Web Stack is integrated with the OpenSolaris OS can be installed from the OpenSolaris repository, as described later in this tutorial. The NetBeans IDE is part of the Sun Web Stack installation.

For this tutorial, the author installed the OpenSolaris OS on Virtual Box 2.0.6, running on an Apple MacBook Pro with Mac OS X 10.5.5. Under Virtual Box, OpenSolaris was provided with a 20 GB fixed-size virtual disk and 1024 MB RAM. Virtual Box is free to download, but you don't need to use it to complete this tutorial. Instead, you can install OpenSolaris directly on your system's boot drive.

About Sun Web Stack

Web Stack is closely associated with a the OpenSolaris SFWNV Project, and is focused specifically on web tier components. Sun Web Stack tunes these components for use on the Solaris and OpenSolaris operating systems. The components in Sun Web Stack are open source. You can use them to build and deploy web applications on the Solaris and OpenSolaris operating systems.

Components available as part of the Sun Web Stack include:

  • A runtime that contains core libraries.
     
  • Bundled versions of Apache HTTP Server, PHP, and MySQL, pre-configured to work together. This version of MySQL is 32-bit, with client-side support to work with PHP.
     
  • A separate 64-bit version of MySQL for users who need larger caches for better performance on large databases.
     
  • Perl version 5.
     
  • A bundle of libraries required by various PHP extensions.
     
  • memcached, the distributed object cache system.
     
  • Python, Ruby and Rubygems.
     
  • The Squid web proxy cache.
     
  • Apache Tomcat server — the same version distributed by the Apache Software Foundation.
     
  • lighttpd, the light-weight HTTP server.
     
  • The NetBeans integrated development environment (IDE).
Tutorial

In this tutorial, you use the NetBeans IDE and other components from the Sun Web Stack on the OpenSolaris operating system. In this environment, you create a "Hello World" program in PHP.

The tutorial assumes that you have installed the OpenSolaris OS, as described in the Requirements section. The following steps assume that you are running in an OpenSolaris environment:

Installing Sun Web Stack

You can install Sun Web Stack from the command line or through the OpenSolaris graphical user interface (GUI). Instructions for a command-line installation can be found in the article Installing Sun Web Stack on OpenSolaris OS. This tutorial uses the GUI procedure.

To install Sun Web Stack with the GUI:

  1. On the desktop or in the OpenSolaris menu bar, click the Add More Software icon. Alternatively, from the menu bar choose System > Administration > Package Manager. The Package Manager window opens.
     
    Package Manager menu
    Figure 1: Package Manager Menu
     
  2. In the Package Manager window, click the checkbox next to amp-dev.
     
    Package Manager Window
    Figure 2: Package Manager Window
    Click the image to enlarge it.
     
  3. In the Package Manager window, click Install/Update and follow the prompts to install Sun Web Stack. Note that the installation includes the NetBeans 6.5 IDE and PHP 5.2.6.
Initializing Sun Web Stack

You will now initialize Sun Web Stack. Among other actions, initialization places Web Stack items in the Applications > Developer Tools menu.

To initialize Sun Web Stack:

  • From the OpenSolaris menu bar, choose Applications > Developer Tools > Web Stack Initialize. After initialization, your menu should look like the following figure:
     
    Initialized Menu
    Figure 3: Initialized Sun Web Stack Menu

Note: If your Developer Tools menu doesn't have the items shown in the figure, restart the GUI by opening a Terminal window and typing the command pkill gnome-panel. The new menu items should then become available.

Starting the Web Server

In this tutorial, your PHP application will be deployed on a local instance of the Apache web server. To start the web server from the GUI:

  • From the OpenSolaris toolbar, choose Applications > Developer Tools > Start Apache2/MySQL Servers.
     
    Start Servers Menu
    Figure 4: Start Servers Menu
     
Configuring the NetBeans IDE

Before you create a PHP project in the NetBeans IDE, you must configure the IDE to use the Sun Web Stack version of PHP.

To direct the NetBeans IDE to the Sun Web Stack PHP installation:

  1. Start the NetBeans IDE from the Applications menu by choosing Applications > Developer Tools > Netbeans IDE.
     
    Starting the NetBeans IDE
    Figure 5: Starting the NetBeans IDE
     
  2. In the NetBeans IDE, choose Tools > Options. The Options window opens.
     
  3. Click the PHP tab and enter the following value for PHP 5 Interpreter:
     
    /usr/php/bin/php
    
     
    Specifying Sun Web Stack PHP
    Figure 6: Specifying Sun Web Stack PHP
     
  4. Click OK in the Options window to commit your changes.
Creating a PHP Project in the NetBeans IDE

To create a PHP project in the NetBeans IDE:

  1. From the IDE, choose File > New Project. The New Project window opens.
     
  2. Choose PHP and PHP Application, then click Next.
     
    Choosing New PHP Project
    Figure 7: Choosing New PHP Project
    Click the image to enlarge it.
     
  3. Name the project HelloWorld, and click Next.
     
    Naming New Project
    Figure 8: Naming New Project
    Click the image to enlarge it.
     
  4. In the Run Configuration pane, verify that the value of Project URL is set to http://localhost/HelloWorld/ and click Finish.
     
    Setting Run Configuration
    Figure 9: Setting Run Configuration
    Click the image to enlarge it.
     
Editing the index.php File

When you create the project, the NetBeans IDE creates an index.php source file, and the project is ready to run. Of course, the deployed web page will be empty unless you enter some code in the index.php file. You will now add some trivial code to the file that will display "Hello World" in red characters.

To edit the file in the IDE:

  1. Click the Projects tab, expand the Source Files node, and click on index.php.
     
  2. Enter the following code between the <body> and </body> tags in the index.php file:
     
    
    <font size="3" color="red" face="Arial">
       <?php
       echo "Hello World!";
       ?>
    </font>
     
    Your edited file should look like this:
     
    Edited index.php File
    Figure 10: Edited index.php File
     
Running the Application

Your application is now ready to run. Choose Run > Run Project in the IDE, or click the Run symbol  ( Run Icon )  in the toolbar. The IDE should start your internet browser and display the following window. If the IDE can't find your browser, you can open the browser manually and type in the URL http://localhost/helloworld/index.php.

HelloWorld Output Window
Figure 11: HelloWorld Browser Window
 
Conclusion

For more realistic projects, you may want to enable the PHP Xdebug extension. The Xdebug extension helps you debug PHP scripts by providing valuable trace, parameter, and memory allocation information. With the Xdebug extension enabled, you can debug your PHP script in the NetBeans IDE.

To enable the Xdebug extension:

  1. Open the file /etc/php/5.2/conf.d/xdebug.ini in an editor.
     
  2. Uncomment the first line of code in the file, then save the file.
     
  3. Restart the Apache server.
     
Further Information
Rate and Review
Tell us what you think of the content of this page.
Excellent   Good   Fair   Poor  
Comments:
Your email address (no reply is possible without an address):
Sun Privacy Policy

Note: We are not able to respond to all submitted comments.
Rick Palkovic Rick Palkovic is a staff writer for Sun Developer Network. He has written about the Solaris OS and Java technologies for longer than he likes to admit, composing everything from man pages to technical white papers.
 

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.