Computing: Website and Database Applications

Web/DB Home   Home   Contact

Web development environment setup on MS Windows.

2. PHP.
  2.1. PHP distributions.
"PHP is a popular general-purpose scripting language that is especially suited to web development.
Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world."
The paragraph above is how The PHP Group themselves describe the PHP scripting language on their website. The PHP document on my site explains how to install PHP on MS Windows and how to use it with the Apache webserver. Linux users normally don't need to install PHP, as it is included with most distributions.
Building software packages like PHP from the source on MS Windows is mostly complicated and frustrating. And in most cases, it's not necessary, as binaries do exist. "The PHP Group" offers a site specifically dealing with PHP for Windows, where you can download the binaries as a .zip archieve.
You have the choice to use the most recent version of PHP5 or PHP7. PHP5 is still very popular and used as default on lots of web hosting sites. PHP7 offers plenty of new features, several features available with PHP5 having been removed. It is said to be twice as fast as PHP5 and only consuming half of memory. There is no problem to install both versions. Just be sure to configure your PHP and your Apache accordingly!
  2.2. Installing PHP.
In order to use the correct distribution, please consider the following:
  • The Windows binaries for Apache are intended to be used with the Apache distribution provided by Apache Lounge
  • PHP must have been build with the same version of C++ as did Apache. Thus, if you use Apache, build with Visual Studio C++ 2017 (VC15), you must choose a corresponding PHP distribution.
  • PHP must have been build for the same platform as did Apache. Thus, if you use an Apache x64 distribution, you can't use PHP 32bit!
  • To run properly on Apache, you must use a Thread Safe (TS) version of PHP.
In my case, using Apache 2.4.32 Win 64, build with VC15, I actually use PHP 7.2.7 VC15 x64 Thread Safe.
Installing the C++ Redistributable.
This shouldn't be necessary, as it had to be installed with Apache (cf. part 1 of this tutorial).
Installing PHP.
Installing isn't the correct word, as PHP is distributed as a .zip file, containing the entire directory structure of the application. Unzip the archieve to "some program directory". To avoid all problems, you should not use "C:\Program Files". The default is "C:\php", I normally use "C:\Programs\php". Major PHP directories:
  • C:\Programs\php: php.exe, php.ini, major PHP DLLs
  • C:\Programs\php\ext: PHP extension DLLs
  • C:\Programs\php\extras: special features as Openssl or PDF management related files
The PHP7 directories
  2.3. Basic PHP configuration.
The PHP configuration file is called "php.ini" and is located in the top-level PHP directory (where php.exe is also located), in my case in "C:\Programs\php". php.ini doesn't exist when you unzip the PHP archieve, instead there are two template files called "php.ini-development" and "php.ini-production". Make a copy of one of them (the development file works well for me) and rename this copy to php.ini.
I never took the time to have a closer look at the php.ini file and I think there is no real need to change anything, except perhaps the path of the extensions directory. The only modifications in my actual file are the following:
Specifying an error log file (originally: not set):
    error_log = php_errors.log
Verifying default character set (originally: already set):
    default_charset = "UTF-8"
Setting the extensions directory (originally: not set):
    extension_dir = "C:\Programs\php\ext"
Please, note that after modifying php.ini, you have to restart Apache to make the modifications take effect!
  2.4. Configuring Apache for PHP.
There are 2 ways to use PHP on Apache: either as an Apache module or as CGI scripting. It's nearly always the first approach that is used, so do I.
Configuring PHP7 as an Apache 2 module.
Add the following to your Apache configuration file (httpd.conf) - note the usage of the slash (/) instead of the Windows usual backslash (\)!
    AddHandler application/x-httpd-php .php
    AddType application/x-httpd-php .php
    LoadModule php7_module "C:/Programs/php/php7apache2_4.dll"
    PHPIniDir "C:/Programs/php/"
Be sure to check in your PHP directory (C:\Programs\php) that the name of the php7apache2_4.dll is actually the correct one!
In order to serve index.php as default when accessing a web folder without specifying a filename, the same way it's done with index.html and prioritary to index.html, modify the DirectoryIndex directive as follows:
    DirectoryIndex index.php index.html
Editing the httpd.conf file
  2.5. Testing PHP (on Apache).
Here a very simple PHP script, called hello.php and placed in my /php/ directory on Apache, just to test if the settings done in php.ini and httpd.conf are valid.
            <title>Hello World</title>
                echo "<h1>Hello, World!</h1>";
Trying it out in the web browser by entering "localhost/php/hello.php" in the address field:
Testing PHP on Apache
  2.6. Enabling PHP extensions.
Which extensions you have to enable depends on what you want PHP to do, beside all that it's able to do by its core components. Here are some commonly used extensions, which, when not enabled, may result in "Call to undefined function" errors. To enable them, just uncomment the corresponding entry in your php.ini file.
To use PHP to access a MySQL database, you need at least one of the following extensions: