PHP for 'i' #1

We have started to create an introduction to C programming for the IBM ‘i’ in a separate post so I thought it would be fun to add a PHP perspective to the project as well. Not sure how this will turn out and what we will add into the mix but our initial thoughts are to use PHP to carry out the same activities against the objects we create in the C project? One area we may add additional code is to address a MySQL database with the same content as the DB2 database, that way we could look at how to access the data from both databases, let me know…

So to start the project off we will create a website which will run on the IBM ‘i’. As we are a firm believer in using VirtualHosting as a base principle you may want to build the same web layout.

First the website, we will call it www.phpproj.local which seems appropriate, you will need to add an entry to your PC hosts file (see below) with the relevant IP address, I would not suggest adding it to your DNS server! Because we will be using VirtualHosting we can run it against the same port as any other website without any issue, that saves having to quote the port number on the URL such as www.phpproj.local:8901.

To set up the virtual hosts you will need to do the following:

1. Install and set up PHP on your IBM ‘i’, we will not go into this again as  we have covered this in previous blog entries. Once the official release of Zend server is available I am sure it will be even easier to set up PHP for the IBM ‘i’. However the information we have here is based on the existing Zend implementation.
2. Create the directory structure for the website. The IBM interface for creating websites can be used or you can manually configure the website and attach the configuration to the IBM server. If you do use the IBM interface be aware that the initial pages will be created in a CCSID which causes problems for the php interpreter.

Here is the directory structure to create for the exercises we will carry out.

/www/webserver is the base directory for the main webserver which will serve the virtual websites
/www/webserver/htdocs will hold the base pages but we wont use them in this project
/www/webserver/conf will hold the configuration files for the web server
/www/webserver/logs hold the main server logs for access and errors
/www/phpproj/htdocs will hold all the web pages
/www/phpproj/htdocs/scripts will hold all the scripts we create

We will not touch the Zend Core server (PASE server) as all of our changes will be to the IBM HTTP server. However we will need to update the configuration file to support the virtual hosts we will use.

Here is the configuration for the webserver.

# Configuration originally created by Create HTTP Server wizard on Thu Apr 16 08:03:35 EDT 2009
LoadModule proxy_module /QSYS.LIB/QHTTPSVR.LIB/QZSRCORE.SRVPGM
LoadModule proxy_http_module /QSYS.LIB/QHTTPSVR.LIB/QZSRCORE.SRVPGM
LoadModule proxy_connect_module /QSYS.LIB/QHTTPSVR.LIB/QZSRCORE.SRVPGM
LoadModule proxy_ftp_module /QSYS.LIB/QHTTPSVR.LIB/QZSRCORE.SRVPGM
Listen 192.168.200.21:80
DocumentRoot /www/webserver/htdocs
Options -ExecCGI -Includes -IncludesNoExec -Indexes -MultiViews
LogFormat "%h %T %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%{Cookie}n "%r" %t" cookie
LogFormat "%{User-agent}i" agent
LogFormat "%{Referer}i -> %U" referer
LogFormat "%h %l %u %t "%r" %>s %b" common
CustomLog logs/access_log combined
LogMaint logs/access_log 7 0
LogMaint logs/error_log 7 0

SetEnvIf "User-Agent" "Mozilla/2" nokeepalive
SetEnvIf "User-Agent" "JDK/1.0" force-response-1.0
SetEnvIf "User-Agent" "Java/1.0" force-response-1.0
SetEnvIf "User-Agent" "RealPlayer 4.0" force-response-1.0
SetEnvIf "User-Agent" "MSIE 4.0b2;" nokeepalive
SetEnvIf "User-Agent" "MSIE 4.0b2;" force-response-1.0

NameVirtualHost 192.168.200.21:80
DirectoryIndex index.html index.php
<Directory />
Order Deny,Allow
Deny From all
</Directory>
Allow From all
</Directory>

<VirtualHost *:80>
DocumentRoot /www/phpproj/htdocs
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8000/
ProxyPassReverse / http://127.0.0.1:8000/
LogLevel warn
ErrorLog /www/phpproj/logs/error.log
CustomLog /www/phpproj/logs/access.log combined
</VirtualHost>

OK so now we need to amend a couple of entries in the PHP server configurations, the file is located ‘/usr/local/Zend/apache2/conf/httpd.conf’ unless you have changed the base install for the ZendCore.
Page all the way down to ‘### Section 3: Virtual Hosts’ this is where we will start to add the Virtual Host definitions.

First uncomment the NameVirtualHost 127.0.0.1:8000. This will let the server know we are going to use VirtualHosting on that interface.
Next we will add a couple of virtual hosts, an important point to remember here is if you set up virtualhosting, the server will look for a match in the configurations, if it cannot find a match it will automatically use the first configuration it finds, in our case www.webserver.local. If you start to see strange content being served this is probably where you need to start looking.


# Web Server
<VirtualHost 127.0.0.1:8000>
ServerName www.webserver.local
<Directory /webserver/htdocs/www>
Allow From All
</Directory>
</VirtualHost >

<VirtualHost 127.0.0.1:8000>
DocumentRoot /www/phpproj/htdocs
ServerName www.phpproj.local
<Directory /www/htdocs/phpproj>
Allow From All
</Directory<
</VirtualHost>

Now restart all of the servers and you should have a working environment. Any problems a couple places to look are in the logs.

‘/www/webserver/logs’ for the webserver logs
‘/usr/local/Zend/apache2/logs’ for the PHP server logs.

Before we can use the web URL we need to add it to our hosts file which is usually found in C:WindowsSystem32Driversetc directory on your PC, if its not there ask your network or PC specialist where to find it. For us we just added


192.168.200.21 www.phpproj.local

Now we just need to add our first PHP file to show the proverbial ‘Hello World’. Create a file ‘index.php’ in the ‘/www/phpproj/htdocs’ directory and add the following content. (Please take note of the problems associated with using the IBM generated index.html file if you used the IBM web interface to create the web setup).

Here is the code you need in the file

< DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type></HEAD>
<BODY>
<?php
echo("Hello World PHP style");
?>
</BODY></HTML>

Now just use your favourite browser and enter the following URL http://www.phpproj.local/. If you get the following,
the CCSID for the file and its content is wrong!

Lo—ˆ— %…ƒˆ–MÈ…““–@æ–™“„@×È×@¢£¨“…]^ %on %

Correct it and you should see

Hello World PHP style

Thats all there is to it! Next we will be connecting to the IBM ‘i’ and reading and reading a file…
Please add any comments or ask any questions you may have, if you have specific problems with the set up we will need to see your config files before we can assist.

Chris..

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.