When talking about web applications, PHP is the most used programming languages to build them. In this article, we will see how we can install PHP on a Windows OS machine.
There are all-in-one installations which provide PHP interpreter, web server (for example Apache), and database (typically MySQL). Those are WAMP, XAMPP, and others. But, we won’t use those packages, we will install only PHP interpreter, and do it manually.
The great thing about new PHP versions is that PHP now has its own built in web server that we can start at the command line. This means that, for local development, we don’t need to install production ready web server applications like Apache, Nginx or IIS.
Install PHP Manually
The first thing to do is go to https://windows.php.net/download/ and download the latest PHP version. If you are going to use PHP interpreter with IIS or Apache web server, take a look at “Which version do I choose?” section. Depending on IIS or Apache, you will have to download Non-Thread Safe version (IIS) or Thread Safe version (Apache).
Once you download the ZIP file, extract it to “C:\php”. You can use any other path, but this is the default and recommended one.
Next, copy C:\PHP7\php.ini-development to C:\PHP7\php.ini. This will make the default development configuration available to PHP. Note that we also have a “production” version available (in which options are more restricted by default), but since we are installing PHP locally (for testing purposes), we’ll want the “development” version.
Open the newly copied C:\PHP7\php.ini in a text editor and find the “extension_dir” option. Uncomment it and set it to:
extension_dir = "C:\php\ext"
Next, in the same file, enable the extensions you want to be available to PHP (remove the semi-colon comment). For example:
extension=php_bz2.dll extension=php_curl.dll extension=php_fileinfo.dll ;extension=gd2 ;extension=gettext ;extension=gmp extension=php_intl.dll ;extension=imap ;extension=interbase ;extension=ldap extension=php_mbstring.dll ;extension=exif ; Must be after mbstring as it depends on it extension=php_mysqli.dll ;extension=oci8_12c ; Use with Oracle Database 12c Instant Client extension=php_openssl.dll ;extension=pdo_firebird extension=php_pdo_mysql.dll
Note that extension name has a ‘.dll’ part at the end. If you don’t have it, add it.
If you want to send emails using PHP mail() function, edit the SMTP settings in the same file:
[mail function] ; For Win32 only. ; http://php.net/smtp SMTP = localhost ; http://php.net/smtp-port smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = [email protected]
Feel free to skip the SMTP part if you won’t be using it.
At this point we have PHP installed. To test it, open up command line (CMD) and enter the command:
c:\php\php -v
This should print out information about the PHP version you are using. However, entering the full path to PHP executable is cumbersome, so we will add it to the path environment variable.
Environment Variables
To enable Windows OS to find PHP when we enter ‘php’ in CMD, we will add our C:\php path to the path variable. Search for ‘environment varables’ and select the first result or go to Control Panel and find System Properties. Click the ‘Environment Variables’ button, and on the ‘System variables’ section find the Path variable, select it and click the Edit button. On the list add the ‘C:\php’
Close and open the CMD again and then enter the command:
php -v
This should print the PHP version installed which means we can use it on any path we want.