Setup PHP On Windows For Development and Debugging is a guide designed for people installing PHP with debugging for the first time. This guide also forms part of a larger collection aimed at setting up development environments.
Hint to make this process easier.
There is a reasonable amount of configuration file editing involved in this process. While it is possible to use notepad, it is hard to read compared to other editors. Both Notepad++ and Visual Studio code make reading the files much easier. Since this is a development environment, Visual Studio Code is probably a better choice as it offers greater scope in other areas. It is available from the Microsoft Website Downloads. While installing it is suggested you include ‘edit with Code’ options in Windows Explorer
Setup PHP On Windows – Download And Extract
Visit the PHP downloads page for Windows and choose the thread-safe version which matches your system (32 or 64 bit). At the time of writing the current version of PHP is 7.4.
First, extract the PHP Zip file into the Downloads folder rather than directly into its final location. Secondly, go into Windows Explorer. Navigate to your download then right-click it and choose extract all.
Once the folder has been extracted, rename it to either ‘php’ or ‘php-7.4.12’. Either option is ok, however, if you only intend to install one version of PHP on your system choosing ‘php’ is the most sensible choice.
Now move the folder to the root of your C:\ drive.
Setup PHP On Windows – Add PHP Folder to the system path
The PHP folder needs to be part of the system path. Use the following procedure to achieve this.
- Type ‘Environment’ into the search bar next to the start button and choose Edit the system environment
- Next Click on Environment Variables
- In the System Environment section find path, select it then click Edit
- Choose the new button in the following window
- Follow this by clicking browse
- Navigate to the PHP folder which was created above and click OK
Now restart the system. A restart might not be required, but this depends on the version of Windows and other system settings. Restart to be sure.
Configure Apache To Work with PHP
Apache needs to know where to find and how to use PHP files. Firstly navigate to the Apache folder. Secondly into a subfolder called ‘conf’ and edit the file called ‘httpd.conf’. Thirdly add the text below to the bottom of the file and save. Finally, The location of the Apache folder is not fixed. Consequently, if the PHP folder is not located at ‘C:\php’, the text below will need editing to match your system. The default location is c:\Apache24 for version 2.4*. A Guide to setting up Apache 2.4 is available in Setup Apache On Windows 10.
#php config PHPIniDir "C:/php" AddHandler application/x-httpd-php .php LoadModule php7_module "C:/php/php7apache2_4.dll"
Also edit the server name section to read
# ServerName gives the name and port that the server uses to identify itself. # This can often be determined automatically, but we recommend you specify # it explicitly to prevent problems during startup. # # If your host doesn't have a registered DNS name, enter its IP address here. # #ServerName www.example.com:80 ServerName localhost:80
- Navigate to the PHP folder
- Find the file called ‘php.ini-development’
- Copy this to a new file called ‘php.ini’
- Edit the php.ini file
Paths and Directories Sections: Remove the semi colons before
- extension_dir = “ext”
- include_path = “.;c:\php\includes”
;;;;;;;;;;;;;;;;;;;;;;;;; ; Paths and Directories ; ;;;;;;;;;;;;;;;;;;;;;;;;; ; UNIX: "/path1:/path2" ;include_path = ".:/php/includes" ; ; Windows: "\path1;\path2" include_path = ".;c:\php\includes" ; ; PHP's default setting for include_path is ".;/path/to/php/pear" ; http://php.net/include-path ; The root of the PHP pages, used only if nonempty. ; if PHP was not compiled with FORCE_REDIRECT, you SHOULD set doc_root ; if you are running php as a CGI under any web server (other than IIS) ; see documentation for security issues. The alternate is to use the ; cgi.force_redirect configuration below ; http://php.net/doc-root doc_root = ; The directory under which PHP opens the script using /~username used only ; if nonempty. ; http://php.net/user-dir user_dir = ; Directory in which the loadable extensions (modules) reside. ; http://php.net/extension-dir ;extension_dir = "./" ; On windows: extension_dir = "ext"
Remove semi colons to un-comment the following extensions
;;;;;;;;;;;;;;;;;;;;;; ; Dynamic Extensions ; ;;;;;;;;;;;;;;;;;;;;;; ; If you wish to have an extension loaded automatically, use the following ; syntax: ; ; extension=modulename ; ; For example: ; ; extension=mysqli ; ; When the extension library to load is not located in the default extension ; directory, You may specify an absolute path to the library file: ; ; extension=/path/to/extension/mysqli.so ; ; Note : The syntax used in previous PHP versions ('extension=<ext>.so' and ; 'extension='php_<ext>.dll') is supported for legacy reasons and may be ; deprecated in a future PHP major version. So, when it is possible, please ; move to the new ('extension=<ext>) syntax. ; ; Notes for Windows environments : ; ; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+) ; extension folders as well as the separate PECL DLL download (PHP 5+). ; Be sure to appropriately set the extension_dir directive. ; ;extension=bz2 extension=curl ;extension=ffi ;extension=ftp extension=fileinfo extension=gd2 ;extension=gettext ;extension=gmp ;extension=intl ;extension=imap ;extension=ldap extension=mbstring ;extension=exif ; Must be after mbstring as it depends on it ;extension=mysqli ;extension=oci8_12c ; Use with Oracle Database 12c Instant Client ;extension=odbc ;extension=openssl ;extension=pdo_firebird extension=pdo_mysql ;extension=pdo_oci ;extension=pdo_odbc extension=pdo_pgsql extension=pdo_sqlite ;extension=pgsql extension=shmop
Check PHP is working in command line
- Type ‘cmd’ into the search box next to the start button
- Click on Command Prompt
- Type ‘php-v’ into the command prompt window
Restart Apache Server
- Type ‘services’ into the search bar near the start button
- Click on the Services application
- Navigate to Apache
- Right-click and choose restart
Testing PHP In A Browser
- Navigate to the Apache Folder
- Then into a subfolder called ‘htdocs’
- Add a new file called info.php
- Edit ‘info.php’ and add the PHP code below
<?php // Show all information, defaults to INFO_ALL phpinfo(); ?>
- Save the file
- Open a browser and navigate to ‘localhost/info.php’
Setup PHP On Windows – Add Extension for Microsoft SQL Server
- First, download the files from the Microsoft Site or use the direct download
- Second, Run the executable
- Accept the licence agreement
- Browse to the PHP folders ‘ext’ subfolder
- Click OK
- Navigate to the php.ini file
- Edit php.ini and add the lines below to the end of the extensions
- N.B if you did not install PHP to C:\php the path in these lines will need adjusting accordingly.
- N.B.2 If your system is an X86 adjust the file name accordingly
- N.B.3 If you installed the non-thread-safe version adjust the file name accordingly
Setup PHP On Windows – Add Xdebug Extension
- Open command prompt
- Type in ‘php -i’
- Copy the entire output to the clipboard
- Visit the Xdebug install instructions page for windows
- Paste the output into the analyse box
- Download the file suggested
At the time of writing the suggested file is php_xdebug-2.9.8-7.4-vc15-x86_64.dll. This may change over time. If it does the file name added to the configuration files will also need to be updated.
- Move php_xdebug-2.9.8-7.4-vc15-x86_64.dll into the ext subfolder of the PHP folder.
- Edit the php.ini file
- Add the following information to the bottom of the extensions section
- Finally Add the following just under the Module Settings
[xdebug] xdebug.remote_host=127.0.0.1 xdebug.remote_enable=1 xdebug.remote_handler=dbgp
If you wish to use Xdebug with PhpStorm also add the following to the end of the [xdebug] section
Test the PHP configuration in the command line.
- Load cmd as describe above
- Type in ‘php -v’
- Hit enter
- Restart Apache again as described above
- Open a browser and navigate to ‘localhost/info.php’