Howto: Setting Up Apache, PHP (NTS) and Guard 7 Loader on Windows

Follow

Summary

This article will help you set up a new PHP Web Service for testing or running Zend Guard encoded applications. I will be using Windows 7 64-bit, Apache 2.4 64-bit with FCGI module, PHP 5.6.10 NTS 32-bit, and Guard Loader for PHP 5.6.

Option 1: The Fastest setup

Download the final package, move to 'C:\' and double click to extract to 'C:\srv'.

Then, jump to Dora the Explorer's all time favourite quote: "Let's Go!!!" :)

If you take the blue pill (ready to run package) - you should probably need to edit php.ini (described in Setting up PHP section below) and change the date.timezone to match your geo location.

Option 2: The 10 steps

Read through this article, without going through the "Notes:" parts - they are here for clarifications and more info.

Option 3: Slow and Steady

Read everything, twice, send me all the typos you find and improvement suggestions, and hopefully you will not fall asleep before the article ends :)

Prerequisites

Apache packages built with VC11 / VC14 can be downloaded from Apache Lounge, or Apache Haus, or other known hosts of Apache builds for Windows (See master list here).

Important note about WAMP stacks:
There are packages just for Apache web server, and also full WAMP stack packages, like WAMP / BITNAMI / XAMPP, which includes PHP and MySQL and some Control applications.
Now, If you wish to use, or already using a full WAMP stack which does not come from Zend (i.e Zend Server), you will be running PHP as Apache multithreaded module, which means Thread Safety Enabled (PHP TS).

Guard Loader DOES NOT LOAD in PHP TS builds, so in order to use Guard Loader within existing WAMP stack, you will have to replace the PHP with NTS build. See this quick article on how to achieve that on XAMPP for Windows:
https://commaster.net/content/installing-php-fastcgi-and-zend-opcache-xampp-windows

For a new setup, I will use the following packages.

Apache + FCGI module

Step 1: Download and extract to 'C:\srv\Apache24'

http://www.apachelounge.com/download/VC14/binaries/httpd-2.4.12-win64-VC14.zip

Step 2: Download, extract and copy 'mod_fcgid.so' to 'C:\srv\Apache24\modules'

http://www.apachelounge.com/download/VC14/modules/mod_fcgid-2.3.9-win64-VC14.zip

Note about Apache 2.4 Builds:
I'm using VC14 builds, which have minimal modules in Apache lounge. Both Apache Lounge and Apache Haus are currently providing VC11 builds, which is also the state of PHP and Guard Loader. However, since Apache and PHP + Loader are not running under the same process, and VC14 builds are better for new OS versions, I'm using the latest and "greatest". If you need Apache modules which only exists in VC11 builds (they are old...), you can get the Apache and FCGI module from VC11 page, with any other Apache module you also need:
http://www.apachelounge.com/download/VC11/

Microsoft VC runtime - download and install

VC14 64-bit: Visual C++ Redistributable for Visual Studio 2015 RC

VC11 64-bit (ONLY NEEDED IF YOU USE VC11 APACHE): Visual C++ Redistributable for Visual Studio 2012 Update 4

Note about VC:
if you are going to use 32-bit Apache, you will need the 32-bit VC11/14 runtime as well.

PHP 5.6 NTS 32-bit

Step 3: Download and extract to 'C:\srv\php-5.6.10-nts-Win32-VC11-x86'

http://windows.php.net/downloads/releases/php-5.6.10-nts-Win32-VC11-x86.zip

Guard Loader for PHP 5.6 NTS

Step 4: Download and extract to 'C:\srv\zend-loader-php5.6-windows-x86'

http://www.zend.com/en/products/loader/downloads#Windows

At this point, I would WARMLY RECOMMEND to rename 'php_opcache.dll' in 'C:\srv\php-5.6.10-nts-Win32-VC11-x86\ext', so it will not be loaded instead of the Guard Loader packaged dll.

Note about PHP 5.5:
You can use this article for PHP 5.5 or 5.6. If you wish to use PHP 5.5, download PHP 5.5 and Guard Loader for PHP 5.5 with the exact same install process as PHP 5.6.

Setting Up Your Environment

Setting Up PHP with Guard Loader

Step 5: Copy 'C:\srv\php-5.6.10-nts-Win32-VC11-x86\php.ini-development' to 'C:\srv\php-5.6.10-nts-Win32-VC11-x86\php.ini' and edit the new file.

Note about Editing Configuration Files (ini, conf):
Use a simple text editor, like notepad / notepad++, not any rich format editor which will mess up the content / encoding with extra data which PHP will not be able to parse.

Add the following section to the end of php.ini:

zend_extension="C:\srv\zend-loader-php5.6-windows-x86\php_opcache.dll"
zend_extension="C:\srv\zend-loader-php5.6-windows-x86\ZendLoader.dll"
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3

Fixing Timezone: Find and modify ';date.timezone =' to reflect your actual Timezone, e.g.:

date.timezone = Australia/Tasmania

You can also configure at this stage memory_limit, error_reporting etc.

Once php.ini is all set up to your preference, save the file and exit the text editor.

Step 6: Testing Guard Loader from CLI

Click start, type "cmd" and Enter - you will get a nice console window.

Run the following to check if Guard Loader and OPCache are loaded correctly in your PHP. If you don't see those components, verify if you have done everything properly (or, use the "Fastest setup" Package above).

> C:\srv\php-5.6.10-nts-Win32-VC11-x86\php.exe -v

Sample Output:

PHP 5.6.10 (cli) (built: Jun 10 2015 15:59:37)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
with Zend Guard Loader v3.3, Copyright (c) 1998-2014, by Zend Technologies

Setting Up Apache

Step 7: Edit 'C:\srv\Apache24\conf\httpd.conf' in a simple text editor

Note about TCP Port Setting
In this article, I'm assuming you have no service listening on port 80. In real life, you might already have Zend Server, Another Apache or IIS, even Skype is known to hog on port 80 and make new web service fail when trying to hook on port 80.
If you need alternative port, such as 81/82 etc., find one that is free and use it from this point on.
To check if there is a process listening on port 80, just try to load "http://127.0.0.1" in your web browser, or run telnet lookup (following is a sample output when something IS listening on port 80, and you need an alternative port):
> netstat -a -b -n | find ":80"
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING

Locate 'ServerRoot' directive and change its value to "C:/srv/Apache24" (including double quotes)

Locate 'ServerName' directive, uncomment the line (remove the leading #) and replace the value with <computer_name>:80 (or alternative port, see above).
Use your real computer name or make up something nice!

Locate 'Listen' directive and modify, in case you need alternative port.

Find and Replace all occurrences of 'c:/Apache24' with 'C:/srv/Apache24' (I found 5, out of which 1 was in a comment).

Locate '<Directory "C:/srv/Apache24/htdocs">' and in that directory section, add ' Includes ExecCGI' to the 'Options Indexes FollowSymLinks' line.

Find and Replace all occurrences of 'AllowOverride None' with 'AllowOverride All', so you can set .htaccess and actually use the filesystem (I found 3).

Locate 'ServerAdmin' directive and replace with your own email (not mandatory).

Locate 'DirectoryIndex index.html' directive and add ' index.php' to this line.

Finally, add the following section, which is the heart of loading PHP under FCGI module, to the end of httpd.conf:

## added for PHP and Guard Loader
LoadModule fcgid_module modules/mod_fcgid.so

<IfModule fcgid_module>
FcgidInitialEnv PATH "C:/srv/php-5.6.10-nts-Win32-VC11-x86;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;"
FcgidInitialEnv SystemRoot "C:/Windows"
FcgidInitialEnv SystemDrive "C:"
FcgidInitialEnv TEMP "C:/WINDOWS/Temp"
FcgidInitialEnv TMP "C:/WINDOWS/Temp"
FcgidInitialEnv windir "C:/WINDOWS"
FcgidIOTimeout 64
FcgidConnectTimeout 16
FcgidMaxRequestsPerProcess 1000
FcgidMaxProcesses 50
FcgidMaxRequestLen 8131072
# Location php.ini:
FcgidInitialEnv PHPRC "C:/srv/php-5.6.10-nts-Win32-VC11-x86"
FcgidInitialEnv PHP_FCGI_MAX_REQUESTS 1000

<Files ~ "\.php$>"
AddHandler fcgid-script .php
FcgidWrapper "C:/srv/php-5.6.10-nts-Win32-VC11-x86/php-cgi.exe" .php
</Files>
</IfModule>

Save the file and exit the editor.

Let's Go!!!

Step 8: Test your Apache + FCGI module + PHP + Guard setup

To run Apache as application

(This is great for troubleshooting when Apache service doesn't start normally, combined with checking the logs at 'C:\srv\Apache24\logs')

open 'cmd' and run:

> C:\srv\Apache24\bin\httpd.exe

To stop Apache, press CTRL+C (beautiful memories of MSDOS... ahhh).

To install Apache as a service

open 'cmd' and run:

> C:\srv\Apache24\bin\httpd.exe -k install
> C:\srv\Apache24\bin\httpd.exe -k start

The above commands will install, then start the Apache service if properly installed.

Now, when you open services.msc (Start Menu -> Run), you can find a new Apache2.4 service there (hopefully, started). Check 'httpd.exe /?' for more options, such as stop/restart/uninstall (for removing the service).

Windows will probably alert you at this stage that the new HTTPD process requests access to your network - allow it! - including checking "public networks" (unless you have any rejects of course).

Once Apache starts (and stays up) - Browse to http://127.0.0.1 and you should see some silly phrase: "It works!" (which means... you know.)

If you wish to give your local website a meaningful (or short) name, instead of using IP/localhost for default website, you can add a new line to Windows hosts file, which is located at 'C:\Windows\System32\drivers\etc\hosts', and use it on your browser. For example, I wish to call it 'zend' (I wonder why) - so browsing to http://zend will bring me to... my local server on port 80.

127.0.0.1 zend

Testing a bit further

You can rename index.html in 'C:\srv\Apache24\htdocs' to see directory tree when browsing to http://127.0.0.1.

Step 9: Create a simple text file in 'C:\srv\Apache24\htdocs' called info.php, and add this code in it, then save:

<?php phpinfo(); ?>

To load the new test and see PHP Information of your new setup, browse to http://127.0.0.1/info.php, or http://<your_site_name>/info.php

You should see OPCache and Guard Loader in the 2nd table where PHP engine information is (under the build configuration table):

This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
    with Zend Guard Loader v3.3, Copyright (c) 1998-2014, by Zend Technologies

You can create a shortcut / link on your desktop, or your start menu for the ApacheMonitor.exe which is in 'C:\srv\Apache24\bin'. Apache Monitor sits in the Systray (opposite side of the Start Menu) where you can control Apache server(s) easily without using the command line. You can also make it start on boot if you place the link under Start Menu -> All Programs -> Startup.

The Big Q: Can I Run Encoded PHP Now, Please?

Step 10: Well... Duh! Lets test encoded file, shall we?

If you extracted the srv.exe package provided above, you already have the Guard simple test. If not, download the attached zip, extract to 'C:\srv\Apache24\htdocs'.

Open your browser at 'http://127.0.0.1/Simple/encoded56/'.

Now click on one of PHP scripts to open encoded / obfuscated PHP script. If all goes according to plan, you will see the output of the simplest, but most beautiful, script you ever seen:

Hello PHP Info!

Game, Set and Match!


Closing Notes

For this article, I was using 3rd party Apache and PHP to load Zend Guard Loader runtime PHP module. Zend does not support 3rd party components within the scope of Guard Loader (free download). If you are not a Zend Guard customer with active SLA, please contact your Guard software maintainer to get assistance with your Guard Loader issues. If you are a Guard customer with active SLA, please open a support ticket with any technical issues or questions regarding Guard and its Loader PHP modules.

For a fully supported WAMP stack by Zend which also includes the Guard Loader, please check Zend Server (available for development, production and cloud).

 

Tags: howto, guard, loader, windows, apache, php, nts
Have more questions? Submit a request

Comments

  • Avatar
    Alexander Mitev

    This line "zend_loader.obfuscation_level_support=3" causes PHP 5.6 to crash (even PhpMyAdmin) with the extremely helpful message "mod_fcgid: get overlap result error", no matter which subversion of Apache and PHP I use. It took me 3 bloody days to figure this out... Pls remove it from this site and from the distro files.

  • Avatar
    Mutiara Bela

    I want to start phpmyadmin and mysql too with this installation. I already did this tutorial and when I install phpmyadmin and mysql, there are some errors. Can you please give the tutorial about how to install phpmyadmin and mysql after I did the tutorial above?
    Thank you..

Powered by Zendesk