Q: I've installed Zend Server Community Edition. Now I'd like to serve PHP pages from my APACHEDFT HTTP instance, which runs on port 80. We've been using this web instance for many years for CGI and static web pages, but I'd like to serve PHP from it as well. Is that possible?
A: Yes. One of the advantages of Zend Server over its predecessor (Zend Core) is that it connects directly to a native Apache instance. This article shows you how to add PHP support to an existing instance.
Background
The PHP interpreter is designed in a way that relies heavily on UNIX features, such as long file names, standard I/O streams, and the ASCII character set. Therefore, both Zend's implementation of PHP on IBM i, as well as the open-source offerings, have always used the PASE environment. After all, PASE provides the ability to run AIX code natively under IBM i, and it does a very good job of it. However, the fact that it's AIX code running on PASE brings up its own set of challenges.
Zend's first free PHP product for IBM i, Zend Core, used an AIX Apache module to provide PHP services to the HTTP server. Unfortunately, because the Apache module was compiled for AIX, it also had to run in a PASE instance of Apache. Zend worked around this shortcoming by providing a preconfigured PASE instance of Apache and setting up a proxy from a native Apache instance to talk to it. It worked, but it was a little clumsy for every request to go through two complete web servers in order to process.
IBM provided Zend with an alternative in the form of a native Apache implementation of FastCGI. FastCGI is a standard that was developed many years ago as a replacement for traditional CGI on UNIX machines. Traditional CGI on UNIX has some performance problems due to the way it spawns processes. (Spawning a process is the UNIX term for "submitting a batch job.") FastCGI was invented to solve those problems by loading a child process once and reusing it for many requests, thus eliminating the overhead of spawning a new process on each request. Interestingly, this is very similar to the way native CGI works on IBM i's HTTP server, when we're using tools such as CGIDEV2. But on UNIX and Windows systems, CGI always spawned a new process, so FastCGI was designed to solve that problem.
IBM added FastCGI support to the native Apache, so now Zend doesn't need to use a separate AIX Apache module. PHP can simply connect to the native HTTP server by using FastCGI. Problem averted.
This support also gives us the ability to add PHP capabilities to any HTTP server instance we like. All that's needed is to configure FastCGI on that instance as well.
Modifying Your Apache Config
For the sake of this article, I assume that you already have Zend Server (or an equivalent PHP server) installed on your machine in PASE, and that you have installed the appropriate IBM PTFs required for Zend Server. You've already tested the ZENDSVR Apache instance, and it works, but you want to add PHP support to another Apache instance named APACHEDFT.
The next step is to edit the configuration file for APACHEDFT and tell it to load the FastCGI module, and also tell it which IFS files will invoke that module.
You can edit your Apache configuration file via the IBM Web Administration for i GUI interface (i.e., the HTTP Administration option on the HTTP *ADMIN instance) by clicking the Edit Configuration File option. Or you can edit the IFS file directly by editing the
/www/apachedft/conf/httpd.conf
file.
For example, if you'd like to edit the file by using green-screen tools, you can type:
EDTF STMF('/www/apachedft/conf/httpd.conf')
You'll need to add the following lines to the top of the httpd.conf file:
LoadModule zend_enabler_module /QSYS.LIB/QHTTPSVR.LIB/QZFAST.SRVPGM
DirectoryIndex index.html index.php
AddType application/x-httpd-php .php
AddHandler fastcgi-script .php
- The LoadModule directive tells Apache to load an Apache module. In this case, it's named "zend_enabler_module" and its implementation is in the QZFAST service program. This is the service program that provides FastCGI support.
- The AddType directive associates a MIME content-type with a particular file extension. In this case, all files that end in .php will be considered to be of content type application/x-httpd-php.
- The AddHandler directive tells Apache to use a handler (external module) named fastcgi-script for any IFS object that ends in .PHP
So the directives tell Apache to use FastCGI for objects that end in .PHP.
When you configure FastCGI, you'll need to tell it which programs to run based on which MIME type is requested. So if the browser asks for a file that ends in .PHP, Apache will set the MIME type to application/x-httpd-php. Now your FastCGI configuration will say that application/x-httpd-php is handled by Zend Core's PHP program.
Once you've made changes to the httpd.conf file, save it to disk. The changes won't take effect until you restart the Apache instance, but don't do that yet—you need to configure FastCGI first.
Creating the FastCGI Configuration
Because it's assumed that you already have Zend Server installed, the fastest way to configure FastCGI is simply to copy the existing one from Zend and change it. You can do that by running the following command:
CPY OBJ('/www/zendsvr/conf/fastcgi.conf')
TOOBJ('/www/apachedft/conf/fastcgi.conf')
TOCCSID(819)
When Apache launches the FastCGI server, it looks for a file named fastcgi.conf in the same directory as the httpd.conf file, so it's important that you name the configuration file "fastcgi.conf" and put it alongside the httpd.conf file for your instance.
It's also vitally important that the fastcgi.conf file be in CCSID 819, since it'll be read by PASE software that expects things to be in ASCII. You should also use UNIX (not Windows) end-of-line conventions. So all lines in the config file should end in LF (not CRLF!). Using Windows Notepad to edit the file will mess up those conventions, so please use a different tool (such as the EDTF command).
Now that you have a copy, you'll need to change the IpcDir directive to point to the apachedft instance instead of the ZendSvr one.
EDTF STMF('/www/apachedft/conf/fastcgi.conf')
When you edit the fastcfi.conf file, it should look like this:
; Static PHP servers for default user
Server type="application/x-httpd-php" CommandLine="/usr/local/ZendSvr/bin/p
; Where to place socket files
IpcDir /www/zendsvr/logs
Note: The Server line of this file is much longer than what's shown on the screen. You'll need to scroll to the right to see the whole line. This line has all the details of how FastCGI handles application/x-httpd-php documents. However, you don't need to understand its contents to enable PHP. In fact, you can leave it as is.
The only required change is the last line of the file. You can see that it's pointing to the /www/zendsvr/logs directory. That'll need to be changed to the /www/apachedft/logs directory. Once you've changed that, save your changes and exit the editor.
Try It Out
Now that you've changed your configuration, you need to stop and restart the HTTP server so it sees your new options.
ENDTCPSVR SERVER(*HTTP) HTTPSVR(APACHEDFT)
-- wait a few seconds, then --
STRTCPSVR SERVER(*HTTP) HTTPSVR(APACHEDFT)
Now that your APACHEDFT instance of Apache has the appropriate directives, it should be able to run PHP programs from your APACHEDFT HTTP instance. Good luck!