PHP for Windows
How to setup PHP on Windows with multiple versions
Scoop is necessary
This guide use scoop to install this binary, if you don't have it, check this guide
Install multiple PHP versions
If you try to install directly PHP with scoop you will have the last version, if you want to manage multiple versions, you need to install php bucket
scoop bucket add phpTry to search php with scoop
scoop search phpAnd you will find a lot of PHP versions, you can install any specific PHP version now. But you will nts versions and standard versions, if you want to use NGINX, I advice you to install nts version for each PHP version.
Install current version.
scoop install php-ntsInstall specific version.
scoop install php8.1-ntsSwitch PHP version
By default, the last PHP installed is the current version, here I install PHP 8.1 at the last, so current version with php -v will be 8.1. If I want to use PHP 8.2, I have just to use this command:
scoop reset php/php-ntsAnd php -v give me 8.2, I can back to PHP 8.1 when I want with scoop reset php/php8.1-nts.
php.ini
You will need to activate some extensions in php.ini, you will find it for each PHP version in each directory of PHP version installed: C:/Users/USERNAME/scoop/apps/php*-nts/current/php.ini. I offer an example of php.ini but you will need to update each php.ini.
- Example for PHP 7.1
- Example for PHP 7.3
- Example for PHP 7.4
- Example for PHP 8.0
- Example for PHP 8.1
- Example for PHP 8.2
php.ini config tips.
This is a configuration for PHP 8.2, you can have different options with another PHP version.
At around line 400
; Maximum amount of memory a script may consume (128MB)
; <http://php.net/memory-limit>
memory_limit = -1At around line 750
; Directory in which the loadable extensions (modules) reside.
; <http://php.net/extension-dir>
; extension_dir = "./"
; On windows:
extension_dir = "ext"At around line 900
; 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
; The MIBS data available in the PHP distribution must be installed.
; See http://www.php.net/manual/en/snmp.installation.php
;extension=snmp
extension=soap
extension=sockets
extension=sodium
extension=sqlite3
extension=tidy
extension=xmlrpc
extension=xslInstall cacert
sudo scoop install cacertAt around line 1900
curl.cainfo = "C:\Users\USERNAME\scoop\apps\cacert\current\cacert.pem"Composer
You can install composer if you want to use PHP packages:
scoop install composerYou can switch between composer version with
composer self-update --1 # composer v1composer self-update --2 # composer v2Setup Services for PHP
If you want to create VHost with NGINX and specific PHP version for each VHost, you will need to create Services. To do this, you will need NSSM (Non-Stucking Service Manager):
sudo scoop install nssmCreate service
And install a new service, here for PHP 8.2
sudo nssm install php8.2You will have a window to create new service, two input will be important: Path and Parameter
For Path, get path of current php-nts, in this example, change USERNAME and php-nts if you set a different PHP version in Path
C:\Users\USERNAME\scoop\apps\php-nts\current\php-cgi.exeFor Parameter, you will need to create specific port with path of current php-nts, in this example change USERNAME, 9082 if you want a different port and php-nts if you set a different PHP version in Path
-b 127.0.0.1:9082 -c C:\Users\USERNAME\scoop\apps\php-nts\current\php.iniSave the new service.
INFO
You can install another for PHP 8.1, for example:
C:\Users\USERNAME\scoop\apps\php8.1-nts\current\php-cgi.exeFor the port, I choose 9081 (for PHP 8.1)
-b 127.0.0.1:9081 -c C:\Users\USERNAME\scoop\apps\php8.1-nts\current\php.iniLaunch service
sudo nssm start php8.2If you change some data, like with php.ini, you will need to restart Service
sudo nssm restart php8.2Errors
cURL error 60: SSL certificate problem: unable to get local issuer certificate
If you have this error, you have a problem of certificate. You can manullay download certificate but I advice to use scoop.
sudo scoop install cacertOpen C:/Users/USERNAME/scoop/apps/php*-nts/current/php.ini to update this line, at ~1900 line
curl.cainfo = "C:\Users\USERNAME\scoop\apps\cacert\current\cacert.pem"And restart PHP-FPM with NSSM
sudo nssm restart php[INSERT-HERE-VERSION]WARNING
You have to do this FOR EACH PHP version you use with you VHost. If you use php artisan serve with PHP 8.2 CLI, you have a VHost with PHP 8.1 and another VHost with PHP 8.0... You have to update all php.ini files which used by VHost or live serve with Laravel or Symfony.
And you have to RESTART EACH version after php.ini file update.
PHP ini location problem
If Windows don't find php.ini assure yourself that this variable is set into Windows environnement variables for current user
PHP_INI_SCAN_DIR => [
C:\Users\ewila\scoop\persist\php-nts
C:\Users\ewila\scoop\apps\php-nts\current\conf.d
]Note: theses variables change when you change your current PHP CLI version with scoop reset php/php-nts for example