|
Oracle + PHP
DeployPHP Series, Part 5: Zend Core for Oracle Quick Start
by Matt Kern
Install, deploy, and configure PHP and Oracle using the simplest method available: Zend Core for Oracle.
Review complete DeployPHP index
Since the early days PHP has been able to talk to Oracle—but it wasn't easy. Deploying PHP with Oracle was complicated at best, requiring developers and administrators to find, download, compile, link and configure multiple applications and packages. Unfortunately, this fact was counterintuitive for PHP users, as PHP has always been known for rapid deployment and quick time to market, not cumbersome processes better left to the C curmudgeons.
Thanks to the latest collaborative effort between Oracle and Zend, those days are history. Zend Core for Oracle allows developers to install, deploy, and configure PHP and Oracle just about as quickly and painlessly as possible. In this final installment of the DeployPHP series, you'll learn how that process works.
Why Do You Need Zend Core for Oracle?
If it were any easier... (Benefits for You). You might just be out of a job. Okay, not really; it's not quite that easy. And of course, someone will need to write code once you've got the power of Oracle and PHP at your fingertips.
Kidding aside, the greatest benefit in using Zend Core for Oracle is the simplicity of deployment. You will have more time to actually write code, rather than contending with environment variables and linking problems.
Running a close second in terms of benefits is the fact that—for the first time—there's a tightly integrated, fully tested and supported PHP platform available for Oracle. That's right, fully tested and supported.
Zend Core for Oracle also gives you instant visibility into the current state and configuration of your Oracle/PHP platform. You can see, at-a-glance, which version of each extension you're running, and you can get a feel for how the system is performing.
Updating your platform is a snap now, too. Zend Core for Oracle provides tools to alert you of packages and extensions that need updating. Zend Core for Oracle can even update your platform automatically on a preset schedule. And, if there's a problem, Zend Core for Oracle can rollback the changes, assuring a working known state.
What you end up with is a PHP and Oracle platform that feels an awful lot like an application server-based solution, but without all the heavy (and usually unnecessary) baggage.
But wait, there's more... (Benefits for Bosses). Of course, even if you're already sold on Zend Core for Oracle (and you haven't even installed it yet!) there's a rather large hurdle to get over: You've got to get your boss on board. Here's what they need to know:
- Zend Core for Oracle is reliable. The OCI8 extension has been completely refactored, resulting in the most reliable Oracle extension for PHP ever.
- Zend Core for Oracle is fully supported. There's no need for the boss to worry about you spending hours upon hours searching message boards, forums, and IRC channels for help. Management won't have to feel like they're out on a limb if they want to deploy PHP with Oracle any longer. Even the third-party extensions are tested and supported. Ultimately, Zend Core for Oracle should translate to increased productivity, which is something any manager can grasp.
- Finally, the power and simplicity of PHP can be leveraged with the safety net of support from Zend and the backing of Oracle.
Knowing Your Limits
A few words about currently supported platforms are in order before we get started with the installation process. At the time of writing (November 2005), Zend Core for Oracle has rather limited platform support: Red Hat Enterprise Linux 3 (not 4,), SUSE Enterprise Linux 9, AIX 5.2 and 5.3, and Solaris 10. As of this writing, the Windows version (Windows XP Professional/2000 Server family/2003 Server family) is in Beta and scheduled for GA by end of 2005. Now, it is possible to run Zend Core for Oracle on a number of other operating systems (many PHP developers don't have access to the big iron that the supported OSes run on anyway), but if you do so, you'll lose one of the biggest benefits of running Zend Core for Oracle: support. Note, however, there are no guarantees on unsupported platforms, and you might well burn a lot of time figuring things out for yourself.
In writing this article, it was relatively easy for me to get Zend Core for Oracle running on CentOS 4 talking to an Oracle 10g database running on Ubuntu Linux. And should you try to run RHEL4 or CentOS4, be sure to disable selinux with the following line in your /etc/sysconfig/selinux file:
SELINUX=disabled
You could certainly brave the selinux waters yourself, but it will take a fair amount of time to configure selinux by hand.
Install Apache
If you're going for pure speed for this deployment (likely the intended result), the easiest and quickest way to get a Web server is to simply use the Package Management application corresponding to your platform. For example, in the case of RHEL, that means using either the graphical Package Management application or rpm from the command line. To install Apache with the GUI tool from Red Hat just scroll your way down to Servers>Web Server in the Package Management application and select it. Then, choose Details off to the right to select/deselect the extra packages you want. Be sure to deselect php and any other packages that depend on php—this avoids any potential issues with conflicts from Zend Core for Oracle and the Red Hat-installed PHP package. (Well, that proves that in the end all you needed was the Web server to install Zend Core for Oracle!)
If you prefer using rpm from the command line, simply download the Apache 1.3.x or 2.0.x rpms from your favorite source and run:
rpm -ivh httpd.2.0.46.rpm
If you've been brave (and lonely without support!) and tried to install on a RHEL4-based OS, you'll likely need to download and install the httpd-suexec rpm as well due to dependencies.
Note that Zend Core for Oracle supports Apache compiled only in prefork mode. This should be no surprise to those of you with prior PHP experience; for those of you without it, this FAQ explains things quite well. Suffice it to say here that PHP and Apache 2 threaded MPMs have never "played well together."
Finally, if you're looking to optimize your Web server, or you just don't trust precompiled binaries and insist on compiling everything, you probably already know what to do. But if not, here's a simple rundown on compiling your own Apache Web server:
# tar xvzf apache_2.0.46.tar.gz
# cd apache_2.0.46
# ./configure -prefix=/usr/local/apache/htdocs \
--enable-module=so
# make
# make install
If you're wondering why we haven't defined the MPM module to compile above, it's because we don't have to. Apache defaults to compiling as prefork on *nix-based systems.
Once you've got Apache installed start up the Web server with:
# apachectl start
Point your browser at http://localhost and verify that the Apache default test page appears. If so, you can move on to install Zend Core for Oracle.
Install Zend Core for Oracle
The first thing you'll need to do is download the Zend Core for Oracle package (Zend Network registration required.) Once you unzip and untar the package you'll find two install scripts in the resulting directory. Most Linux systems should have no problem running the Text UI (curses based) version of the installer, aptly named install. If you prefer, run install-tty instead for a text-only installer.
# tar xvzf ZendCoreForOracle-v1.2.2-Linux-x86.tar.gz
# cd ZendCoreForOracle-v.1.2.2-Linux-x86
# ./install
Read the License Agreement and accept it. Then, you'll have what might amount to the biggest decision you'll have to make during the install process: Where to put Zend Core for Oracle? Most of you should accept the default of /usr/local/Zend/Core and watch the file names whiz by as Zend Core for Oracle is installed.
Beyond that, there are a few simple steps: First you'll be prompted for a password for accessing the Web UI for Zend Core for Oracle. Then, you'll be prompted for a username and password for a current Zend Network account. One of the best features of Zend Core for Oracle is visibility into the status of the platform. The Zend Network account is used to track updates, not only for Zend Core for Oracle but for installed PHP components, as well. If you don't have a current username and password for Zend Network you can always say "no" in this dialog and run the setup tool later to add it.
Remember, though, that without entering the Zend Network account info, you won't be able to check on updates via the Web interface, thereby relinquishing one of the key benefits of Zend Core for Oracle. (To clarify: You'll still be able to see what versions of each component are installed, but you won't know when updates become available.)
Next, you'll be asked on which Web server you'd like to install Zend Core for Oracle. At this point you're best served by choosing the Apache Web server you installed previosuly. Zend Core for Oracle will eventually support Oracle HTTP Server, which is based on Apache, but current support is spotty. Answer the following screens; they're more confirmation than anything else.
Your next decision will be how to install Zend Core for Oracle to Apache. The standard recommendation here is to install as an Apache Module. The options for FastCGI and CGI exist, but for most applications that would require Oracle as a backend, there's no reason not to choose the SAPI module.
Okay, your last big decision: If you've set up your Web server to use Virtual Hosts, you can select the one you'd like to deploy Zend Core for Oracle on. Keep in mind that you're really installing the Zend Core for Oracle Web UI to the virtual host you choose and that PHP will still be available to the other Virtual Hosts on that machine. For example, let's say you've got two virtual hosts: zco.acme.com and juniper.acme.com. You choose to install Zend Core for Oracle to zco.acme.com. Then, you'll access the Web UI for Zend Core for Oracle at http://zco.acme.com/ZendCore. However, juniper.acme.com will still serve PHP pages using Zend Core for Oracle's bundled PHP engine. Effectively, the configuration that you set up at zco.acme.com will apply to all the virtual hosts on that machine, unless you make changes outside Zend Core for Oracle (for example, adding php_flag directives to the virtual hosts in Apache).
At this point, you might just be feeling like something must be missing. Something must be wrong; it can't be this easy. But it is! There's no need to separately download and install the Oracle Client Libraries or to set environment variables, compile, link, and recompile. Zend Core for Oracle comes with everything you need (aside from the Web server and database) installed.
If you just can't stand not knowing the secret to that trick, it's simple really: Zend Core for Oracle includes the Oracle Instant Client Binaries preconfigured.
Let's take it for a test drive!
Get Set... (or, Configuring Your New Platform)
First, make sure you can access the Web UI for Zend Core for Oracle. Fire up a browser and go to http://yourhost.yourdomain.com/ZendCore. If you used Virtual Hosts, be sure that the hostname you're using now is the one on which you installed Zend Core for Oracle during the install process. You should be redirected to a login page where you'll need to enter the password you provided earlier. Upon a successful login you'll see the Control Center and know that all is well.
Before you do much else, you should take a reality check to see if your PHP application can communicate with Oracle. Create a file called oratest.php in your document root, most likely /var/www/html if you've gone with the plain-vanilla install of apache. Add the following code to the file, replacing the connection info in the call to oci_connect() with the parameters appropriate for your database. (Note that you're using the Oracle sample HR schema here, which is the schema included with Oracle Database 10g Express Edition, Oracle's free starter database. If you're missing this schema you can install it using Oracle's Database Configuration Assistant.)
<?php
$conn = oci_connect('hr', 'hr', '//aztec/orcl');
if (!$conn) {
$e = oci_error();
print htmlentities($e['message']);
exit;
}
$query = 'SELECT * FROM employees';
$sth = oci_parse($conn, $query);
if (!$sth) {
$e = oci_error($conn);
print htmlentities($e['message']);
exit;
}
$results = oci_execute($sth, OCI_DEFAULT);
if (!$results) {
$e = oci_error($sth);
print htmlentities($e['message']);
exit;
}
print '<table style="border: 1px solid gray;">';
while ($row = oci_fetch_array($sth, OCI_RETURN_NULLS)) {
print '<tr>';
foreach ($row as $item) {
print '<td style="border: 1px solid gray; padding:3px 5px 3px 5px;">'.($item?htmlentities($item):' ').'</td>';
}
print '</tr>';
}
print '</table>';
oci_close($conn);
?>
If the syntax of the connection string in the oci_connect() call looks strange to you, let me be the first to introduce you to the new Oracle 10g Easy Connect syntax. If you want the gory details on configuring naming methods for Oracle, you'll find them in the doc. Otherwise, it's quite simple. The last parameter can use the following syntax:
//hostname[:port]/service_name
Accessing http://yourhost.yourdomain.com/oratest.php should display a neat little table of results from the HR schema. If you see errors instead, make sure your Oracle database is running, and that there's a TNS listener available on the host running the database server. Here's the good news, though, even if there are errors: The trouble is probably your connection information (not Zend Core), especially if you see ORA- errors.
Changing the Configuration
Now that Zend Core for Oracle is installed you'll likely want to change a few things. There are two available methods for changing Zend Core for Oracle's configuration: a command line tool (setup) and the Web UI.
Login to Zend Core for Oracle as you did earlier and go to the configuration tab. From there you'll see you have options to view and update your configuration in several ways. Zend Core for Oracle provides an easy-to-use Web UI for making changes to your php.ini file on the Configuration>PHP and Configuration>Extensions tabs. The PHP sub-tab gives you access to all the core PHP directives such as include paths and error handling. The Extensions sub-tab provides access to the directives that are extension specific in php.ini.
Use the plus signs to expand each section and make appropriate changes to your PHP configuration. Keep in mind that you'll most likely need to restart the Web server if you make changes to the PHP configuration (unless you're running PHP as CGI). Zend Core for Oracle conveniently provides a way to do that from the Web UI as well in the upper-right corner of the PHP configuration screen.
Zend Core for Oracle includes all the most common extensions, but many of them are disabled by default. If you find you need, for example, support for the image manipulation library GD, you can enable the GD extension with two clicks of the mouse—one to enable it and another to restart the Web server.
All in all, Zend Core for Oracle provides an excellent level of abstraction for configuring PHP without the need to directly edit configuration files.
Also available from the Web UI Configuration screens are settings for all the installed Zend products: Zend Debugger, Optimizer, Extension Manager, and of course, Zend Core. You can enable and disable products, or change configuration settings specific to a particular product on those screens.
The command line tool can be started as follows:
# cd /usr/local/Zend/Core/setup # assuming you accepted the default install location
# ./setup
A note of caution here if you're running RHEL: Red Hat provides a curses-based setup program that's available in the system path by default, so if you neglect to add the ./ to the setup command you may not run the correct setup program. If that happens simply exit out and rerun the command from the current directory as shown above.
The command line setup tool provides access to many configuration changes, the most important of which I'll briefly cover here. For example, if you didn't set your Zend Network account info during the initial setup, choose 2 Update Zend Core from the root menu of the setup tool. You'll be presented with another menu, where the bulk of the configuration options can be found. Choose 4 Configure to update your account info and follow the resultant menu. You'll see on that next screen that you can also change your preferences for the cron jobs that check for updates. That allows you to choose whether or not you want the updates applied automatically or if you'd rather just be notified they exist.
From this Configure menu, you can update to the latest version of Zend Core just by choosing Update Zend Core. You can also choose to remove extensions that you may not want available to administrators. Or, you can rollback previous configuration changes. Rollbacks can be very useful if you find a bug in a newly installed package and need to get back to a known state.
Another very useful nugget that can be found in the command line tool's menus is the password for the Web UI. If you should need to change the password for your Zend Core for Oracle installation, just run the setup tool, and choose option 1 at the first menu. You'll be prompted for a new password by entering it twice. Once the Web server is restarted, the change will take effect.
The command line tool also provides access to deploy to other Web servers, change your Web server configuration, uninstall, and change the Web UI password. Most of the screens in the tool will look familiar as they're the same screens you saw during the initial install. For more information on specific uses of the tool refer to the Zend Core for Oracle Installation and Maintenance Guide PDF.
Tweaks
As easy as this process is, I still have to recommend a few tweaks.
First, security: Because a default installation of Apache will not protect your .php and .inc files from displaying if PHP happens not to load, or there's some other configuration problem, I suggest adding the following to your Apache configuration file to disallow display of .php and .inc pages. (Just confirm it's placed after the LoadModule directive that Zend Core for Oracle adds to load the php5 module.)
<IfModule !mod_php5.c>
<Files ~ "\.php$">
Order allow,deny
Deny from all
</Files>
</IfModule>
<Files ~ "\.inc$">
Order allow,deny
Deny from all
</Files>
The biggest reason for this is to keep potentially sensitive code away from prying eyes should the server not start as expected. Also consider changing the value of expose_php under Configuration>PHP>Language Options to off.
There are many other security-related tweaks that should be considered, so the security issues here are pointed out mostly to make you aware of the fact that the default configuration can always benefit from a security analysis as well as constant vigilance.
Another significant tweak has more to do with Oracle than PHP. If you're used to using SIDs or service names to connect to your Oracle instance you might find it interesting to know that you can still do so as long as your Oracle instance is configured to listen for such connections. To use a SID, however, you'll need to set an environment variable called TNS_ADMIN and set it to the path where the tnsnames.ora file can be found. To set a default service name use the environment variable TWO_TASK and set it to the service name you want to use.
Go! (Or, Off to the Races)
Now that you've got the power of Oracle and PHP at your fingertips, it's time to develop your applications. That's just a bit out of scope for this article, but you'll find plenty of useful information on best practices and other helpful hints on Oracle Technology Network's PHP Developer Center. There are a few things worth pointing out here to help you along your way, however.
Performance Monitoring. Zend Core for Oracle provides a handy Control Center when you log in to the Web UI. There you'll find useful information available at a glance about your platform, including graphs depicting what Web server threads are doing, transfer totals, CPU usage, and disk space usage. All these statistics are monitored at the time of request, so you'll have the information at hand when making decisions about scaling and performance.
Benchmarking. Zend Core for Oracle also provides an interface for benchmarking your applications. By going to the Control Center tab and selecting Benchmark you can test an individual PHP scripts right out of the box. Simply enter the URL of the script you'd like to benchmark in the Test URL field, then decide if you'd rather test by a set number of requests or time duration. Choose how many concurrent connections you'd like to initiate for the benchmark test, and you can even add headers and cookie data to your request if the script requires them. (See the Zend Core for Oracle User Guide PDF for details on the formats). Click on the Run button and watch for your results to be displayed below when the test finishes.
Here's an example run on the code listing we used to test drive Zend Core for Oracle earlier:
Using a combination of the Control Center's System Overview and Benchmark screens you should have much more information available to you than ever before for tweaking and scaling your Oracle and PHP platform.
Integration With Zend Studio. If you use Zend Studio as your development IDE you'll be happy to know that Zend Core for Oracle can act as your Studio Server. You can configure Studio to talk to Zend Core for Oracle and act as the server for debugging and profiling.
Integration with Zend Platform. Zend Core for Oracle is also fully compatible with Zend Platform. Zend Platform adds even more performance monitoring to Zend Core for Oracle along with features like PHP and Java integration, session clustering, and intelligent alerting and troubleshooting of your live systems.
Staying Up to Speed
Once you're up and running with Zend Core for Oracle staying current is a breeze. Remember that during the install process you were prompted for your Zend Network credentials; those credentials are used to check in with Zend about new version of the Core or any installed extensions or components. Depending on the choice you made during the install, the updates will either be applied automatically or you'll be notified of new releases on the Update page. And, you can always take advantage of the rollback feature to get your Zend Core for Oracle platform back to a known state if an upgrade goes badly. (Rollbacks can be completed from the command line setup tool.)
Conclusion
Assuming all has gone well, at this point you have a completely functional, reliable, and supported Oracle and PHP platform provided by Zend Core for Oracle. By no means have we covered every facet of this exciting product from Zend and Oracle, but you should now have a solid understanding of what it takes to get on the grid with PHP and Oracle: Not much!
Matt Kern has been searching for and developing ways to make life easier through technologies like PHP for years—mostly an attempt at finding ways to spend ever more time roaming the mountains of Central Oregon with his family. He is the founder of Artisan Technologies Inc. and co-founder of Atlanta PHP.
Send us your comments |