Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple versions of PHP on IIS simultaneously

I have developed a website named vishwasthali.org in PHP and have hosted it in IIS7 on a third-party server. Everything else works fine, except for the code for database access that I have written using MySQLi API. The code works fine on the localhost, but not on the server. Using phpinfo() on the server and my machine, I found that:

(1) On my machine (running in Windows 7 32-bit, Apache hosting environment), the PHP version is 5.3.8, and mysqli version is 5.0.8-dev, and

(2) On the server (running on Windows Server 2008, IIS7 hosting environment), the PHP version is 5.2.14 and mysqli version is 5.0.51a.


Here are the screen shots of phpinfo() on the server: enter image description hereenter image description here


Here is the sample of the mysqli code.

session_start();
include_once 'code_files/conn.php';
$stmt = $mysqli->prepare('SELECT * FROM adminusers WHERE Username = ? AND `Password` = ?');
$stmt->bind_param('ss', $_POST['Username'], $_POST['Password']);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0)
    ........
else
    ........

Using echo get_class($mysqli); does display mysqli. That means, the code does detect mysqli. But the statement $stmt is not prepared. Even the $mysqli->error; does not display any error, might be because of PHP being running on IIS.

Now I have two options, either change the code for database access to classic mysql API, or to upgrade the PHP and MySQLi versions on the server by downloading latest PHP binaries from the php.net. I won't like to go with the first option (that definitely works), hope you know why. The latter is not in my hands. Can't think anything more than these.

I suppose replacing the php_mysqli.dll on might upgrade the mysqli version, not sure. But what about PHP version?

What can I do in this case. Will an upgrade of PHP and MySQLi versions on the server solve my problem? If YES, can it affect other PHP websites running on the server?

Thanks in advance.

like image 836
Kumar Kush Avatar asked Jun 03 '12 17:06

Kumar Kush


2 Answers

All right. Here's the solution. Different PHP versions can be enabled and running side-by-side in IIS7. Here's the procedure that I have successfully tested on my machine. I am only briefing the process, considering that you are familiar with the IIS and FastCGI. If not, see the link: http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis'

  1. Download any different versions of non-thread safe zip packages with binaries of PHP from http://www.php.net/downloads.php.
  2. Extract them to separate folders in your hard-drive like C:\PHP5.2.14, C:\PHP5.4.3, etc.
  3. Open the php.ini files in all the folders and add/uncomment and change following settings in all of them.

    session.save_path = "C:\Windows\Temp"
    expose_php = Off
    ;; Choose any path here, but mostly preferred is this
    open_basedir = "C:\inetpub\wwwroot"
    extension_dir = "ext"
    fastcgi.impersonate = 1
    cgi.fix_pathinfo=1
    cgi.force_redirect = 0
    ;; You can chose different time zones for different web-sites
    date.timezone = "you time zone"
    ;; Enable any extensions that you want to per website, e.g.
    extension=php_mysql.dll
    extension=php_mysqli.dll

  4. Open IIS Manager. Add as many Handler Mappings as the number of PHP versions you want to install. Simply change the path of Executable in the Edit Module Mapping dialog box. Name the Mappings as convenient to identify them easily, like PHP 5.2.14 via FastCGI and PHP 5.4.3 via FastCGI.

  5. In the IIS section in IIS Manager, open FastCGI Settings. Here, check whether there are as many entries as the number of PHP versions you want to run. If not, click Add Applications from the right-pane. Select a php-cgi.exe from the folder of the required PHP version in the Full Path field in the 'Add FastCGI Application' dialog box. Click OK.

  6. Now, copy your website folder to C:\inetpub\wwwroot or whatever path you choose in open_basedir setting in php.ini. In the IIS Manager, expant Sites->Default Web Site from the left pane. Select the website that you want to target to a specific version of PHP.

  7. Open Handler Mapping, with a website selected from the left pane. You will find many mappings here. Look only for those entries that have been created by you (those which have path as *.php). Among them, retain the only one that you need for the particular web-site selected and Remove the others. This WILL NOT delete the mappings from you machine, but only from the web-site selected. You website will now run only on the PHP version whose Mapping you have retained. Do the same for any websites that you want to target to a specific PHP version. You can test it by running phpinfo() in the websites.

After you add new websites to wwwroot, they won't have any handler attached. Add a new handler targetting the desired PHP version. You are done.

This may not be the best procedure. But it worked for me. For verification, see this link:
http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis/#PHP_Versioning'

Hope it helps, in case someone needs this.

Regards

like image 187
Kumar Kush Avatar answered Sep 21 '22 15:09

Kumar Kush


There are a few things quite wrong here!

a) The difference between your development environment and your live site is too large! At least use the same web server and the same PHP 5.x version!

b) NEVER publicly make your phpinfo() accessible, it is a very large security risk! A hacker can get loads of great information from it.

c) Even when using prepared statements you should sanitize your user data before you use it in a query!

I don't think your problem has anything to do with the different mysqli versions. Maybe you should first make sure you use the same web server and php version in both environments and then ask again with more code, (e.g. how you make the connection) if it still doesn't work.

Yes, updating PHP can affect other projects on the server but that just means you need to update these projects, which you should do anyways!

And no, downloading PHP from php.net is not the right way in your case. You should use tested and approved versions provided by the respective repositories.

like image 35
markus Avatar answered Sep 18 '22 15:09

markus