|
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:
- 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.
|
Figure 1: Package Manager Menu
|
- In the Package Manager window, click the checkbox next to amp-dev.
|
Figure 2: Package Manager Window
Click the image to enlarge it.
|
- 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:
|
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.
|
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:
- Start the NetBeans IDE from the Applications menu by choosing Applications > Developer Tools > Netbeans IDE.
|
Figure 5: Starting the NetBeans IDE
|
- In the NetBeans IDE, choose Tools > Options. The Options window opens.
- Click the PHP tab and enter the following value for PHP 5 Interpreter:
|
Figure 6: Specifying Sun Web Stack PHP
|
- 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:
- From the IDE, choose File > New Project. The New Project window opens.
- Choose PHP and PHP Application, then click Next.
|
Figure 7: Choosing New PHP Project
Click the image to enlarge it.
|
- Name the project
HelloWorld, and click Next.
|
Figure 8: Naming New Project
Click the image to enlarge it.
|
- In the Run Configuration pane, verify that the value of Project URL is set to
http://localhost/HelloWorld/ and click Finish.
|
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:
- Click the Projects tab, expand the Source Files node, and click on
index.php.
- 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:
|
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 ( ) 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.
|
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:
- Open the file
/etc/php/5.2/conf.d/xdebug.ini in an editor.
- Uncomment the first line of code in the file, then save the file.
- Restart the Apache server.
Further Information
|
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.
|
|