開始指引使用Firebird 和 PHP

在PHP中使用Firebird 相對而言是很簡單,但是要了解一些十分複雜的其它程式.

首先要看你使用的作業系統Windows 或 Linux. Linux 會提供較快和較穩定, 但是相同的設定可以設在windows的伺服器.

為了簡單的應用, the windows route is being documented with the plan that different versions of Linux will be supported as required.

There are several web servers that can be used as the base html support package, and the one selected here is Apache2. While PHP is reported as being experimental on this package, no problems have so far been found with Apache 2.0.46 and PHP 4.3.2, the current stable releases.

必須下載

So the starting point is the Apache web site, to download the latest release there www.apache.org where the latest PHP download can also be found. The projects of interest being HTTP Server and of course PHP.

HTTP Server Project provides a Win32 binary version on it's download page (Win32 Binary (MSI Installer)) but it may be necessary to change the mirror from which it is downloaded if the default on is busy.

The PHP Project on the same site provides a download of the current PHP in the same way. I use the .zip package from the download page as it includes the extensions such as php_interbase, while the windows installer does not.

The third item that will help is a copy of Firebird itself. If this has not already been installed, then the current versions can be obtained from the sourceforge archive which can be accessed via the main firebird site or directly from IBPhoenix. The latest news is always available here, and the download link on the header gives access to all the firebird release. At the current time I would start with firebird-win32 version 1.0.3-Release as the 1.5 version is still awaiting release, but any current version should work with PHP on windows. Sourceforge will take you through it's mirror selection before actually starting the download.

Starting with the Web Server

All of these packages come with quite comprehensive installation guides, which is not planned to reproduce here, but a few pointers are appropriate to get your system up and running. In fact, there is not much needed to get the system running 'from the box'. I have a habit of avoiding the Microsoft default setting for things so that I know where I am, and so I start with a 'c:\Network' directory as the base for this installation, and then all the network stuff is in the one place. If your existing installations are some where else, it is just a matter of using the correct paths where requested. So we start with apache_2.0.46-win32-x86-no_src.msi, which will complain if your version of Windows does not have a recent Microsoft msi installer. The installer will ask all the usual questions, and information on this can be found in the installation guide, which is also available off-line once the package has been installed. Setting up the Domain and Server name the same as the machine name if you are not connecting to a real web domain, select the Typical setup until you have time to play, and I change the default path to c:\Network\. The install takes less than a minute on a fast machine. Once that is done, I extract the PHP .zip (php-4.3.2-Win32.zip in this case) into the network directory, keeping the zip files directory structure in tact. This produces a sub-directory in c:\Network of \php-4.3.2-Win32 which is both a bit of a mouthful, and has 'dots' in it which I find windows screws up on, so before using it, the name is changed, in this case to php-4_3_2.

Since Firebird is independent of Apache/PHP, we can first check the setup of the web server before adding the database stuff. The first nice feature in Apache2 is the Apache Service Monitor, which allows the starting and stopping of local and remote apache servers. As we have not changed anything yet, Apache should start without any problem, accessing localhost:80 should give you the Apache default page. If it does not, then **** and we need to find out why your network is not work. FAQ for Apache problems can be found at http://httpd.apache.org/docs/misc/FAQ.html. This is an Apache1 document and so is not available offline.

Adding PHP

Assuming we have a default page, then now is the time to start breaking things. Stop the server using the Monitor, or the stop button in the 'Program Menu'. Then find the Apache config file. This has a link to it from the 'Program Menu' under 'Apache HTTP Server 2.0.46-Configure Apache Server' - 'Edit the Apache httpd.conf'. It can be found in c:\Network\Apache2\conf\ directory, and I set .conf files to automatically open 'Notepad' to edit them. The on-line installation guide for PHP can be used, or downloaded as part of the help files (which is a windows help file version of the manuals).

There are only a couple of things to change, the first of which is optional. I like to build the Web site in the c:\Network\ directory, so I create a sub directory of Web site, and use that as my DocumentRoot, so the references to 'c:\Network\Apache2\htdocs' are changed to 'c:\Network\Website'. Then the test pages are loaded into that directory, but more of that later. The second and more important change is to enable PHP. This is actioned by adding

LoadModule php4_module c:/Network/php-4_3_2/sapi/php4apache2.dll

at the end of the LoadModule list and

AddType application/x-httpd-php .php
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .phpt

at the end of the AddType list. You may need to add additional lines here if other PHP packages use different extensions, but this will give us the basics.

We are now ready to restart the Apache server, and check that PHP loads, which of cause it will not, so we address a couple of points there as well. While we have extracted the PHP directory structure, a couple of bits are in the wrong place. First copy 'c:\Network\php-4_3_2\php.ini-recommended to your windows directory ( c:\WINNT or c:\Windows, make a note of which ) and rename it php.ini. This gives the default configuration, and is the file that will be edited later to add the interbase stuff, but for now just worry about a default PHP. The second little tweak, is to copy 'c:\Network\php-4_3_2\php4ts.dll' into the sapi directory 'c:\Network\php-4_3_2\sapi'. This is because we need it accessible in the search path when the sapi file is opened in Apache, and this is the safest way of keeping it within the current PHP version. NOW it should be possible to start Apache, and do this BEFORE worrying about the interbase extensions, so as to ensure that the any problems are cleared before going to the next step. I have a set of simple html pages that allow the installed software to be checked at each stage. An index page allows selection of both test pages, and documentation. These will be listed later, but the most important page is 'testphpinfo.php', the "Hello World" of PHP coding. This accesses the comprehensive settings page for PHP, and allows all of the settings to be checked. If this page does not display, then PHP is not running, and needs to be sorted out before proceeding.The most useful file in this instance is c:\Network\Apache2\error.log, and usually points to the problem, otherwise it's back to the Apache\PHP sites, or shout for help in the newsgroups.

Adding Firebird

If we have a working PHP Version 4.3.2 information page then we are well on the way to a fully functional system. The next step needs an operational copy of Firebird, which again a break the rules and install in it's own directory structure as c:\Firebird. Firebird-1.0.3.972-Win32.exe should install without any problem, and just changing the install directory to c:\Firebird will align it with these notes (then any additional Firebird packages will be installed below the Firebird directory). This process will add gds32.dll to the windows systems directory, and that file is necessary in order for php_interbase.dll to run. Notes on checking the Firebird installation are available on the Firebird web site.

Now we are ready to tempt fate, and enable the php_interbase.dll extension to PHP. So first stop the Apache server again, so that the 'Windows' copy of php.ini (you did make a note of where it went?) can be edited. The easy change is to remove the ';' from the start of the php_interbase.dll line, I enable the php_gd2.dll line at the same time, for it's graphics facility, but that is optional. The second more important change is the extension_dir entry, change this to

extension_dir="c:\Network\php-4_3_2\extensions\"

which removes the need to move any of the other extension packages if you want to use them. Two further changes, help with later packages. the session.save_path entry should be changed to

session.save_path="c:\Network\Apache2\tmp"

and a \tmp sub directory added to the Apache2 directory.

The final change is recommended when php_interbase is being used, but causes other problems, so I am not currently using it. This is the magic_quotes_sybase entry, and leaving it 'off' allows ibWebAdmin to work properly. Watch this space for more information.

With these changes made, then we are ready to restart Apache, and check the testphpinfo.php page. Provided that Apache starts, then interbase should appear just above the mysql entry on the settings page, and we are ready to try out Firebird with PHP.

ibWebAdmin

ibWebAdmin is a nice PHP package to add as it provides all the facilities that you could need for database management. It can be downloaded from sourceforge and the latest version is 0.91. Download ibWebAdmin_0.91.zip, and then unzip it into the c:\Network\Web site directory where it will create a sub directory 'ibWebAdmin_0.91', which I change to 'ibWebAdmin_0_91' before windows complains.

Configuring ibWebAdmin is relatively easy, but does require the linux default values changing to Windows format, the following settings are affected. These entries are found in the configuration file in the /inc subdirectory (configuration.inc.php), being a unix file, you may have problems editing it, as it will not open normally in Notepad, but Wordpad will handle it quite happily, until you can decide on a suitable PHP Editor, which is another area for discussion.

define('BINPATH', 'c:/Firebird/bin/'); Firebird program directory

define('SECURITY_DB', 'c:/Firebird/isc4.gdb'); Firebird security file

define('TMPPATH', 'c:/Network/Apache2/tmp/'); This was the tmp directory we created earlier

define('DEFAULT_DB', 'isc4.gdb'); Use the security database because we know it is there

define('DEFAULT_PATH', 'c:/Firebird/'); As Above

define('BACKUP_DIR', 'c:/Firebird/Backup/'); Need to create \Backup directory in c:\Firebird

define('DATAPATH','data/'); Must have following /, but nothing in front

define('TTF_FONT', DATAPATH.'Bluehigh.ttf'); Remove / present in DATAPATH

The Test Site

In order to carry out tests, a simple PHP web site setup has been provided as a zip file, which can be unzipped to the c:\Network\Web site directory. It consists of a simple index.html page which accesses five functions which can be used to test the installation.

TestPHPInfo is the phpinfo command encapsulated in a PHP page in order to use it.

fbWebAdmin takes you to the ibWebAdmin package once it is installed.

TestConnection is a simple check of the database connection and USER table contents.

TestFBQuery is a more advanced check of the database connection listing the USER table fields.

NS Lookup allows host names and ip addresses to be looked up.

In addition to the test pages, the \Config directory contains copies of the configuration files for Apache (httpd.conf), PHP (php.ini) and ibWebAdmin (configuration.inc.php). These files can be used directly if the c:\Network and c:\Firebird directory structure is maintained, or they can be used in BeyondCompare to check which lines need modification in a new setup.

In addition to the Firebird 1.0 installation, a version configured for Firebird 1.5 is also available. This locates Firebird in the directory c:\Firebird_1_5, but is otherwise identical to the 1.0 version.