If you have a shared hosting environment you will find it is not straight forward to install WSO2 WSF/PHP + PHP DataServices Library since you have very limited authority on your environment. Here in this post, I’m explaining the steps I followed in setting up my custom PHP, WSF/PHP Extension and DataServices library for http://ws.dimuthu.org (This is a shared hosting environment from dreamhost based on LAMP stack) which I’m using to expose my blog as a WebService.

Prerequisites:

  1. You should be able to run PHP using mod_fastcgi (not using mod_php) in Apache. That allows you to configure your own php environment.
  2. The server configurations managed by the hosting service should allow you to handle .php extension from your own  CGI executable (here it is php.cgi), If not, you can still try with setting some other extension (say .phq or .qhq, anything not .php) to be handled by your php-cgi. Anyway if your hosting service allow you to run CGI then most probably you have this permission.
  3. You should have the access to the shell (using SSH) and should be able to use development libraries(libxml, libxsl, libopenssl) + build tools (make)
  4. There can be some other restrictions that your hosting service has put on you, that I have not experienced with my hosting service. So please check your hosting service support and documentation for possibility of building your own PHP environment.

Install PHP:

I have extracted out most of the steps from dreamhost documentation on custom php.ini.

  1. Download PHP and compile. You should at least enable –enable-fastcgi –enable-force-cgi-redirect in the configuration.
    ./configure --enable-fastcgi --enable-force-cgi-redirect --with-xsl --with-openssl --prefix=`pwd`/dist
    make
    make install

    Note: from here on, php source directory is referred as <php_source_directory>
    I have installed the php to <php_source_directory>/dist since we mostly don’t have permission to install it in to default location. (/usr/local). From here on I will be referring php installation directory as <php_install_dir>

  2. create a directory called ‘cgi-bin’ to your web document root directory(<document_root_directory>) and copy the <php_source_directory>/dist/bin/php-cgi in to that. (before php version 5.2.1 there is no php-cgi generated, you can just copy the php to the cgi-bin directory). And rename the php-cgi to php.cgi.
    cp <php_install_direcory>/bin/php-cgi <document_root_directory>/cgi-bin/php.cgi
  3. copy <php_source_direcory/php.ini-dist to the cgi-bin directory and rename it as php.ini, this can be used to configure your php environment.
    cp <php_source_directory/php.ini-dist <document_root_directory>/cgi-bin/php.ini
  4. Create .htaccess file in the cgi-bin directory to allow the access to php-cgi and php.ini files.
    cat << EOF ><document_root_directory>/cgi-bin/.htaccess
    Options +ExecCGI -Indexes +FollowSymLinks
    <FilesMatch "php(.?)\\.(cgi|ini)$">
    Order Deny,Allow
    Deny from All
    Allow from env=REDIRECT_STATUS
    </FilesMatch>
    
    EOF
  5. Create .htaccess file in the root directory redirect all the requests for .php extensions to go through our php-cgi.
    cat << EOF > <document_root_directory>/.htaccess
    AddHandler php-cgi .php
    Action php-cgi /cgi-bin/php.cgi
    EOF

    If your hosting service doesn’t allow to handle .php just rename the end of the first line to some other extension.

  6. Check the necessary permissions levels in files.
    chmod 644 <document_root_directory>/.htaccess
    chmod 755 <document_root_directory>/cgi-bin
    chmod 644 <document_root_directory>/cgi-bin/.htaccess
    chmod 644 <document_root_directory>/cgi-bin/php.ini
    chmod 755 <document_root_directory>/cgi-bin/php.cgi

That’s it. Now your .php files should be run on your own php environment. Just to check whether it is working or not create a phpinfo() file.

cat <<EOF > phpinfo.php
<?php
phpinfo();
?>
EOF

And go to the http://yourdoman.com/phpinfo.php and verify that it is your custom php environment .

Installing WSF/PHP

  1. Download WSF/PHP latest version and extract it to any of your preferred directory(<wsf_php_source_directory>).
  2. Set the PATH environment variable to search for your custom php installation first.
  3. export PATH=<php_install_dir>/bin:$PATH
  4. From the same shell go to the <wsf_php_source_directory> and compile it.
    ./configure
    make
    make install
  5. Now check where you have installed wsf/php. it is in fact the extension dir shown by the php-config command.
    php-config --extension-dir

    I will refer this directory as <php_extension_directory>

  6. Open the <document_root_directory>/cgi-bin/php.ini an set the extension directory, enable wsf extension and set the include path to the wsf scripts directory.cat <<EOF >> <wsf_php_source_directory>/cgi-bin/php.ini
    cat <<EOF >> <wsf_php_source_directory>/cgi-bin/php.ini
    extension_dir=<php_extension_directory>
    extension=wsf.so
    include_path=".:<wsf_php_source_directory>/scripts"
    
    EOF

    If you found that you can only put relative directories in your php.ini (that happens when you don’t even have the read access to your root directory ‘/’), follow the workaround mentioned in here, http://phpwebservices.blogspot.com/2008/08/installing-wsfphp-in-third-party.html

That’s all. Just check the phpinfo (created earlier section) whether your configurations and the wsf extension are loaded correctly. In order to test the functionality copy the wsf/php samples to the web document root and access them from the browser. Go to each sample and verify they function as expected.

cp /samples <wsf_php_source_directory>/ -R

And go to http://yourdomain.com/samples and click on each samples. If this not working most probably your localhost to ip mapping is wrong, most probably this problem is there in your shared host. Just go to the samples directory and change the endpoint to the domain name in place of ‘localhost’ in each client and try it again.

Installing PHP DataServices:

  1. Download and unpack PHP DataServices. <php_data_services_directory>
  2. Update the include_path entry in php.ini to DataServices libraries directory as well. So the <wsf_php_source_directory>/cgi-bin/php.ini  file now has the following entry.
    include_path=".:<wsf_php_source_directory>/scripts:<php_data_services_directory>/lib"

That’ all for the DataService installation. Please read <php_data_services_directory>/README to run the samples. Anyway if you are sure you did followed these simple steps correctly, go on with your DataService  development. Specially if you have wordpress database, you may start straight away with the making wordpress a DataService.

Answering users questions about wsf/php installation for many months, I thought it is worth to take some time and make a shortlist of some regular questions asked by them. Actually problems has been occurred not only due to users mistakes in following the installation steps properly, but also due to the status of the systems like already installed old versions, use of different package management tools, mulitple installations of same thing like php/apache and conflicts in libraries. In this little FAQ i’m trying to answer problems users come up with due to all of above mentioned reasons. And feel free to correct me or even add here some entries through comments.

  1. Where is the installation guide?If you have downloaded the packs (either source or binaries), you can find the installation README in their. README.INSTALL for the linux installation and README.INSTALL_WIN32 for the windows installation.

    You can find the online version from http://wso2.org/project/wsf/php/1.3.2/docs/install_guide.html

  2. I have lot of php.ini files in my system. Which should I edit? (Refers to http://wso2.org/forum/thread/3879)This is because you have many PHP installations in your system, one from deb, one from compiling source another can be from Zend core. Similarly there can be many apache instances as well.

    The best way to find the php.ini is to use the php function phpinfo(). For that you have to find the apache web root directory. By default it is ‘/var/www’. If it is not /var/www just find for the directory named htdocs and it can be most probably your web root directory.

    Create a php file (say phpinfo.php) with the following lines of code.

    <?php
    phpinfo()
    ?>

    And save the file inside your root web directory and access it though the browser. (most probably it is http://localhost/phpinfo.php)

    And search for the entry “Loaded Configuration File”. That is exactly what you were finding for..

  3. My Web Services Client correctly working on the Command line (using PHP CLI). But the same thing doesn’t work when I put it in the apache server in the same computer and access from a browser. It complains with the error message ‘WSClient’ not found, What can be the problem?The php.ini loaded by the CLI can be different from the one loaded by the server. Just find the correct php.ini loaded by the server. So the answer to this question is same as the answer to the above question.
  4. My Web Service Client run on the apache server correctly, but doesn’t run on the command line?
    There also you need to find the php.ini loaded from the PHP CLI. run the php -i command and check what is the loaded configuration.
  5. I’m in windows. I have followed all the steps correctly. But php error log always says wsf.dll is not loaded. What may I have missed here?
    The first thing you should do is check the PATH variable value from a phpinfo (not from command line), I have mentioned the steps to create phpinfo page in the answer to the question number 2. After that search the entry for PATH variable in the Apache environment section. Just check whether this is the same path that you think you have set. You get this problem mostly because the path loaded by apache is different from what you set. You may mistakenly have set the required library paths to the user environment variable and not to the system environment variable. Or you may have to restart the system to actually update these variable. Try these things until you actually get the PATH you want to have from the phpinfo.

    The other possibility is you have a set of old libraries prior to the new libraries in your PATH. This happens mostly if you are upgrading the system from old wsf/php version. If that so make sure you remove the paths of these old libraries from the PATH variable.

    And another possibility is the user mistake. Check your libxml, iconv and openssl libraries are actually in the PATH, if not just set it and try.

  6. All my samples are working except the samples in the wsdl_mode and wsdl_generation directories. Any clue?
    You may have missed either
    installing, enable php xsl extension
    or
    setting the include_path to the scripts directory. (This scripts directory is shipped with every binary and source packs, when you extract these packs the ’scripts’ directory will be there right in the root directory of the pack.

    The other very rare reason would be some compatibility issues on libxml2 version used by dom and the libxml2 version used by the xsl. Apparently if you have libxml2 version 2.6.31 with an older libxml2 version (like 2.6.2*) most probably you will find php crashes when using xsl and dom. See the link http://wso2.markmail.org/message/54yvh2pcowhrj332. This is like fight between dom and xsl for the libxml2 library, if the first one loaded the old version the other one will be down, since it is not compatible with that loaded version. in this situation you have to upgrade either the xsl extension or the dom extension (that mean you better upgrade all the php libraries and binaries)

  7. I installed apache and php using apt-get command in my ubuntu OS. but when I edit php.ini in the /etc/php5/apache2 directory the configuration are not loaded correctly. What should I do?
    This problem can be aroused in any debian based system when you install apache and php from apt-get or with deb. In this case you have to create a file like wsf.ini inside /etc/php5/apache2/conf.d directory and add the wsf specific configuration there.
    extension=wsf.so
    include_path=.:/my/script/directory/script
    Remember there you have to install php-xsl package from apt-get or using a deb.
  8. When I run samples It simply doesn’t work. And there are no complains in the php error log or apache error log, and in phpinfo it shows the wsf is correctly loaded. How can I solve this?
    If the wsf is loaded and still the samples fails, the next thing you should do is check out the wsf logs from where you specify to generate in the php.ini (if you didn’t specify it in linux it will be default to /tmp directory). Open the wsf_php_client.log and find the errors and entries in the later part that may have caused the problem. If it say something like client repo doesn’t exists or axis2.xml is failed to load just double check the wsf.home you specified in the php.ini
    If you don’t understand whatever the wsf log tell, please put a question in the forum http://wso2.org/forum/188 or put a mail http://wso2.org/mail#WSFPHP with both wsf_php_client.log and wsf_php_service.log attached. (Make sure you delete any sensitive data in the blog)
  9. Samples says it is working successfully. How can I make sure the clients/services actually sends messages?
    Download tcpmon from http://ws.apache.org/commons/tcpmon/download.cgi. You can send the messages through tcpmon and track their content. Just read the tcpmon tutorial and try to get it working.