Computing: Website and Database Programming

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 archive.
There are several versions of PHP available, and these versions are normally not compatible. PHP5 was very popular for long yers and often used as default on 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. More recently, they have released PHP8. There is no problem to install PHP7 and PHP8 and even PHP5, if you want so, side by side. Just be sure to configure your PHP and your Apache accordingly!
Note: The PHP language, that I used, when writing this tutorial, was version 7.2.7 (x64). For some considerations, concerning the actual (January 2021) version 8.0.0 (x64), cf. Actual PHP distribution.
  2.2. Installing PHP.
In order to use the correct PHP 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 for the same platform as did Apache. Thus, if you use an Apache x64 distribution, you can't use PHP 32bit!
  • 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.
  • 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 archive 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
Installing several PHP versions side by side.
It is possible to install several PHP versions (e.g. PHP 7.x and PHP 8.x) side by side, and running your Apache webserver with either the one, or the other. For details, please, have a look at my Using multiple PHP versions on Apache for Windows tutorial.
  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 located), in my case in "C:\Programs\php". php.ini doesn't exist when you unzip the PHP archive, 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 the path of the extensions directory. You should also set a logfile for errors during the execution of PHP scripts. 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 two 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 the 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 having priority 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 in php.ini and httpd.conf are valid and PHP is actually working correctly.
            <title>Hello World</title>
                echo "<h1>Hello, World!</h1>";
To run the script on your local webserver, enter localhost/php/hello.php in the address field of your browser.
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:
Note: If PHP does not find the extensions, uncomment the extension directory entry in php.ini and set its value to the folder that contains the extensions. Please note that you have to use two slashes (//) instead of the Windows backslash (\) when writing the path. In my case (with PHP installed in C:\Programs\php):
  2.7. Actual PHP distribution.
The actual (January 2021) version is PHP 8.0.0. It is build with Visual Studio C++ 2019 (VC16). You may download vc_redist_x64 from the Microsoft site (but you probably have already installed it with Apache).
Configuring Apache for PHP 8:
The basic configuration of PHP as described above for PHP 7 also applies to PHP 8. Concerning the configuration of Apache to use PHP 8, however, the module name as well as the one of the DLL to load have changed:
    AddHandler application/x-httpd-php .php
    AddType application/x-httpd-php .php
    LoadModule php_module "C:/Programs/PHP/php8apache2_4.dll"
    PHPIniDir "C:/Programs/php/"
PHP extensions:
When enabling the PHP extensions, as described above, please note that some extension names have changed with PHP 8. Thus, instead of "extension=gd2", you must use:

If you find this text helpful, please, support me and this website by signing my guestbook.