Sunday, October 5, 2008

KB: NetBeans and Zend Framework on Ubuntu

Guide for setting up NetBeans with the Zend Framework on Ubuntu Linux.

Contents

Overview

I have been wanting to try the Zend Framework for some time and now that NetBeans 6.5 has support for PHP, I thought I would give it a shot. The process I went through to get it working, while not difficult, had a few curves that made it worth documenting. Most of this guide was compiled from other articles on the subject and is specific to getting the Zend Framework working with NetBeans 6.5 on Ubuntu Linux 8.04. This guide will result in a development environment and project that is ready for the Zend Framework Quick Start Tutorial.

Install the Application Stack

This guide will use XAMPP for the application stack. While one can install the required components individually from the Ubuntu repositories, XAMPP makes the process virtually painless.

1. Download XAMPP from here.
2. Extract the archive to /opt:
sudo tar xvfz xampp-linux-1.6.8a.tar.gz -C /opt
3. Add the control panel to the desktop. In a terminal run:
vim ~/.local/share/applications/xampp-control-panel.desktop
and add the following to the file and save:
[Desktop Entry]
Comment=Start/Stop XAMPP
Name=XAMPP Control Panel
Exec=gksudo "python /opt/lampp/share/xampp-control-panel/xampp-control-panel.py"
Icon[en_CA]=/usr/share/icons/Tango/scalable/devices/network-wired.svg
Encoding=UTF-8
Terminal=false
Name[en_CA]=XAMPP Control Panel
Comment[en_CA]=Start/Stop XAMPP
Type=Application
Icon=/usr/share/icons/Tango/scalable/devices/network-wired.svg
"XAMPP Control Panel" will show up in your applications menu under "Other". Use the Menu Editor to move it around if you so desire.
4. Open the control panel and click "Start XAMPP".
XAMPP Control Panel
5. Once XAMPP is up and running, open your browser and go to http://localhost/ and you should see the XAMPP test page:
XAMPP Test Page

Install the Zend Framework

1. Download the Zend Framework from here.
2. Create a directory for your PHP libraries in /opt:
cd /opt
mkdir phplibs
3. Extract the ZendFramework to /opt/phplibs:
sudo tar xvfz ZendFramework-1.6.1.tar.gz -C /opt/phplibs
4. Create a symbolic link to the Zend directory (optional), I do this to make managing project configurations easier when I upgrade to new versions of Zend:
cd /opt/phplibs
ln -s ZendFramework-1.6.1 ZendFramework

Create the NetBeans Project

1. Open NetBeans and create a new PHP project named "quickstart". Leave the NetBeans project metadata in the project directory (default).
PHP Project - Name and Location
Click "Next".

2. Modify the project's run configuration. Set "Run As" to "Local Web Site" and "Project URL" to "http://localhost.quickstart.com"
PHP Project - Run Configuration
Click "Finish".

3. Modify NetBean's PHP options. Select Tools->Options and click the "PHP" tab. Set the PHP 5 Interpreter to [XAMPP_HOME]/bin/php and add /opt/phplibs/ZendFramework/library/Zend to the global include path.
NetBeans Options
Click "OK".

Create a Project Specific Virtual Host

There are a number of ways to setup NetBeans to work with a PHP Project. You can create your project directly in the DocumentRoot ([XAMPP_HOME]/htdocs) of your web server, configure NetBeans to copy your project files to the DocumentRoot ([XAMPP_HOME]/htdocs) of your web server or use a virtual host to link your project to your web server. This guide will use the virtual host approach.

1. Check that mod_rewrite and virtual hosts are enabled in [XAMPP_HOME]/etc/httpd.conf:
LoadModule rewrite_module modules/mod_rewrite.so
Include etc/extra/httpd-vhosts.conf
2. Add a virtual host for your project, in this example "localhost.quickstart.com" by editing [XAMPP_HOME]/etc/extra/httpd-vhosts.conf as follows (comment out or remove the two existing sample virtual hosts):
NameVirtualHost *:80

<virtualhost 127.0.1.2:80>
    ServerName localhost.main.com
    ServerAlias localhost.main.com *.localhost.main.com
    ServerAdmin admin@localhost
    DocumentRoot "/opt/lampp/htdocs/"
</virtualhost>

<virtualhost 127.0.1.3:80>
    ServerName localhost.quickstart.com
    ServerAlias localhost.quickstart.com *.localhost.quickstart.com
    ServerAdmin admin@localhost

    DocumentRoot "/home/krdavis/Projects/quickstart/public/"

    DirectoryIndex index.php

    CustomLog /opt/lampp/logs/quickstart.local.access.log common
    ErrorLog /opt/lampp/logs/quickstart.local.error.log

    <directory /home/krdavis/projects/public/quickstart>
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </directory>
</virtualhost>
Note #1: The first vhost allows you to continue to access the XAMPP applications that are installed as part of the distribution.
Note #2: The second vhost is used to host the NetBeans project you created earlier. The DocumentRoot and Directory are set to the location of this project.

3. Add the following two entries to /etc/hosts:
127.0.1.2 localhost.main.com
127.0.1.3 localhost.quickstart.com
4. Open the control panel and click "Stop" and then "Start XAMPP" to enable the new configuration.

Zend Framework Quick Start Tutorial

1. Work through the Quick Start Tutorial until you get to the section "Create a Bootstrap File". Modify the include path as follows so the application can pickup the framework from the location we installed it to earlier (you can use a relative or absolute path here):
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/'));
set_include_path(
 APPLICATION_PATH . '/../library'
 . PATH_SEPARATOR . '/opt/phplibs/ZendFramework/library'
 . PATH_SEPARATOR . get_include_path()
);
2. When you finished the tutorial section "Create an Action Controller & View", navigate to http://localhost.quickstart.com and you should see:
Zend Quick Start Tutorial - Hello
To Top