Archive for May, 2005

PHP Apache and Cygwin

Boy did I have a hard time getting PHP and Apache to work under Cygwin. I had to set the environment up for a class project so first I did what any reasonably lazy person would do–check for a Cygwin package. I only saw the Apache package but I needed PHP too so I decided to hold off on installing it. So next I did what any reasonably lazy person would do next–Google for a page posted by someone who did the work already so I wouldn’t have to.

After browsing through numerous pages of Google results, I couldn’t come up with anything for the search terms ‘cygwin apache php’. Of course now that I just re-searched for those terms, the page I ended up with at the end of my journey is the fifth result. More on that later.

Instead, this site (Index of /support/cygwin-packages/apache-modules/mod_php4) caught my eye. It was the eighth result and I thought to myself, “OMG, there’s a package available!” So off I go to download and install the package. I installed the Apache and Apache-PHP packages. Now I might not be the most experienced Cygwin user so maybe I didn’t install the package correctly because it didn’t check for any of the dependencies. After looking at the setup.hint file, I installed the required packages and I thought I was set (cygwin apache libgdbm openssl bzip2 zlib gettext libxml2 postgresql libbz2_0 libbz2_1 rebase).

To start Apache, I ran ‘/usr/sbin/apachectl start’ and I saw that it started the http daemon. Woohoo! I made a test PHP file in /var/www/htdocs but it wasn’t being parsed. What gives? I looked at /etc/apache/httpd.conf for the appropriate PHP module lines but they weren’t there. Then I realized that installing both the Apache and Apache-PHP packages at the same time must not have been a good idea. I reinstalled the Apache-PHP package and it made the appropriate changes to the httpd.conf file. Now to restart the daemon…

# /usr/sbin/apachectl start
Syntax error on line 236 of /etc/apache/httpd.conf:
Cannot load /usr/lib/apache/libphp4.dll into server: dlopen: Win32 error 998
/usr/sbin/apachectl start: httpd could not be started

Hrm? Being the reasonably lazy guy that I am, I set off to Google in search for an answer. The results were mostly mailing list archives for people getting the error message without a resolution. Some people suggested rebasing the DLL so I tried that. Nope, same error. Other people suggested running ‘cygcheck libphp4.dll’ to check for any dependency problems. I did that and received some ‘could not find something.dll’ errors. So I tried to look for them and also for any packages which might have them. I couldn’t find cyghttpd.dll or cygcrypto.dll and I googled for the packages containing them but it seems that I already had them (Apache and OpenSSL). So I just made copies of libhttpd.dll and cygcrypto-0.9.7.dll to the respective missing files and hoped that the problem would be solved. I ran cygcheck and everything was okay so I tried to start up http daemon again. Same error.

I decided to rethink the strategy to take. How about compiling PHP myself? So I downloaded the PHP source files and looked at the INSTALL file. I decided to just try ‘./configure’ without any parameters first because I didn’t want to deal with them (read: lazy). It said it couldn’t find lex. D’oh, another package to install (flex). Then I ran configure again and it started doing it’s thing.

While configure was running, I decided to go back to my Google search results and see if I could find any useful information. Then I stumbled upon this page (Cygwin/apache/php - discypus). The Google page summary was in Japanese and what’s a discypus? Who knows, let’s click on the link! Even though I’ve only had four quarters of Japanese and couldn’t read any of the kanji, I understood “Apache 1.3.29-2 + PHP 4.3.8 (2004-07-26)”. I scrolled down a little more and, “OMG! PHP compiling instructions for Cygwin!” Since this information was on a Japanese site, I cancelled my configure immediately. I followed the instructions to a T and ended up with a working Apache with PHP (minus the MySQL which I didn’t really need anyway). The Discypus wiki is a goldmine of Cygwin information (and more!). Moral of the story? Trust any Japanese sites you visit.


Here are the instructions with some translatations. All credit goes to Discypus.

1. Required Cygwin packages (in addition to the ones mentioned above):

$ grep -A20 '@ apache' setup.ini | grep require
requires: cygwin crypt libgdbm4

$ cygcheck -cd cygwin apache crypt libgdbm4
Cygwin Package Information
Package              Version
apache               1.3.29-2
crypt                1.1-1
cygwin               1.5.10-3
libgdbm4             1.8.3-7

2. Extract the PHP source files:

$ cd /usr/local/src/php

$ tar jxvf php-4.3.8.tar.bz2
php-4.3.8/
php-4.3.8/ext/
:
php-4.3.8/README.UNIX-BUILD-SYSTEM
php-4.3.8/php4.spec.in

3. Run configure:

$ cd php-4.3.8

$ ./configure --with-apxs=/usr/sbin/apxs --without-mysql --enable-mbstring
creating cache ./config.cache
checking host system type... i686-pc-cygwin
:
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+
|                          *** NOTE ***                              |
|            The default for register_globals is now OFF!            |
|                                                                    |
| If your application relies on register_globals being ON, you       |
| should explicitly set it to on in your php.ini file.               |
| Note that you are strongly encouraged to read                      |
| http://www.php.net/manual/en/security.registerglobals.php          |
| about the implications of having register_globals set to on, and   |
| avoid using it if possible.                                        |
+--------------------------------------------------------------------+

Thank you for using PHP.

4. Modifying the Makefile:

$ sed -i.org 's/libphp4.so/libphp4.dll/g' Makefile
$ diff Makefile.org Makefile
:

5. Run make:

$ make
:
Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).

6. Compile the PHP libraries:

$ cd libs
$ ls
libphp4.a  libphp4.la
$ gcc -shared -o libphp4.dll -Wl,--out-implib=libphp4.dll.a 
-Wl,--export-all-symbols 
-Wl,--whole-archive libphp4.a /bin/cyghttpd.dll 
-Wl,--no-whole-archive -lcrypt -lresolv
Creating library file: libphp4.dll.a
Info: resolving _ap_user_id by linking to __imp__ap_user_id (auto-import)
Info: resolving _ap_group_id by linking to __imp__ap_group_id (auto-import)
Info: resolving _ap_user_name by linking to __imp__ap_user_name (auto-import)
Info: resolving _ap_max_requests_per_child by linking to __imp__ap_max_requests_
per_child (auto-import)
Info: resolving _ap_server_root by linking to __imp__ap_server_root (auto-import
)
Info: resolving _top_module by linking to __imp__top_module (auto-import)
$ ls
libphp4.a  libphp4.dll  libphp4.dll.a  libphp4.la

7. Run make install:

$ cd ..
$ make install
Installing PHP SAPI module:       apache
[activating module `php4' in /etc/apache/httpd.conf]
cp libs/libphp4.dll /usr/lib/apache/libphp4.dll
chmod 755 /usr/lib/apache/libphp4.dll
cp /etc/apache/httpd.conf /etc/apache/httpd.conf.bak
cp /etc/apache/httpd.conf.new /etc/apache/httpd.conf
rm /etc/apache/httpd.conf.new
Installing PHP CLI binary:        /usr/local/bin/
Installing PHP CLI man page:      /usr/local/man/man1/
Installing PEAR environment:      /usr/local/lib/php/
[PEAR] Archive_Tar    - installed: 1.1
[PEAR] Console_Getopt - installed: 1.2
[PEAR] PEAR           - installed: 1.3.2
Wrote PEAR system config file at: /usr/local/etc/pear.conf
You may want to add: /usr/local/lib/php to your php.ini include_path
[PEAR] DB             - installed: 1.6.2
[PEAR] HTTP           - installed: 1.2.2
[PEAR] Mail           - installed: 1.1.3
[PEAR] Net_SMTP       - installed: 1.2.3
[PEAR] Net_Socket     - installed: 1.0.1
[PEAR] XML_Parser     - installed: 1.0.1
[PEAR] XML_RPC        - installed: 1.1.0
Installing build environment:     /usr/local/lib/php/build/
Installing header files:          /usr/local/include/php/
Installing helper programs:       /usr/local/bin/
  program: phpize
  program: php-config
  program: phpextdist

8. Make sure the following two lines were added to /etc/apache/httpd.conf (if not, add them):

LoadModule php4_module        lib/apache/libphp4.dll
AddModule mod_php4.c

9. Add the following two lines to the same file in the appropriate section (I just added the first line to the end of the file):

#
# Document types.
#

:
   # for PHP 4.x
   #
   AddType application/x-httpd-php .php
   AddType application/x-httpd-php-source .phps

10. Check the contents of httpd.conf:

$ /usr/sbin/apachectl configtest
Syntax OK

11. Start the http daemon

12. Create a test file /var/www/htdocs/phpinfo.php:

<?php phpinfo(); ?>

13. Test the file at http://localhost/phpinfo.php

18 comments May 31st, 2005 at 10:15pm


Calendar

May 2005
S M T W T F S
    Jun »
1234567
891011121314
15161718192021
22232425262728
293031  

Posts by Month

Posts by Category