Skip to content

Rundiz/phpswitch

Repository files navigation

PHPSwitch

Switch between PHP versions using command line on Windows and Linux.

GitHub all releases

Requirement:

Windows 10 64 bit.
Linux 64 bit.
Administrator privilege (Windows UAC or Linux sudo).

Installation

Prepare folders

PHP versions folder

There is one folder that contain multiple PHP versions and this is required. This folder will be called PHP versions folder.

The PHP versions folder structure must follow this guide line.

  • php (Assume that this is PHP versions folder).
    • php7.3 (Keep the version number after the text "php" in lower case, no space.)
      • dev
      • ext
      • ...
      • php.ini
      • ..
    • php7.4
      • ...
    • php8.0
      • ...
    • php-running (Required folder, case sensitive.)

Each PHP versions folder must contain the files that you have downloaded from php.net.

php-running folder

The php-running folder is required to make PHP works (CLI and web server).
If you want to access php executable file, please add this php-running path into your System variables > Path.
To do this, run rundll32.exe sysdm.cpl,EditEnvironmentVariables in the command line. Edit Path in System variables and then add the full path to php-running folder.

Apache folder

This folder is optional and for who is using Apache only. This folder will be called Apache folder.

The Apache folder structure must follow this guide line.

  • Apache24 (24 represent Apache v2.4)
    • bin
    • cgi-bin
    • conf
      • extra
        • ...
        • httpd-php.conf (This file is required if you use Apache. It should be included from httpd.conf.)
        • httpd-php-7.3.conf (Keep the version number after the text "php-" in lower case, no space.)
        • httpd-php-7.4.conf
        • httpd-php-8.0.conf
        • ...
      • ...
    • ...

The file httpd-php.conf must be included in the httpd.conf file. The file httpd-php-x.x.conf (where x.x is the version of PHP) will be included in the httpd-php.conf file - automatically generated by PHPSwitch application.

Prepare config file.

This application use JSON as config file. To read this file automatically, copy the code below and paste as phpswitch.json aside the phpswitch application file.

phpswitch.json file structure.

{
  "phpVersionsDir": "D:\\wwwserver-x64\\php",
  "phpRunningDir":  "D:\\wwwserver-x64\\php\\php-running",
  "webserverServiceName": [ "apache" ],
  "apacheDir": "D:\\wwwserver-x64\\apache\\Apache24",
  "apacheUpdateConfig": true,
  "additionalCopy": {
    "7.0": [
      {
        "copyFromDir": "D:\\ImageMagick\\6.9.3-7-vc14-x64\\bin",
        "searchPattern": "CORE_*.dll",
        "copyTo": "."
      },
      {
        "copyFromDir": "D:\\ImageMagick\\6.9.3-7-vc14-x64\\bin",
        "searchPattern": "IM_*.dll",
        "copyTo": "."
      }
    ],
    "7.1": [
      {
        "copyFromDir": "D:\\ImageMagick\\6.9.3-7-vc14-x64\\bin",
        "searchPattern": "CORE_*.dll",
        "copyTo": "."
      },
      {
        "copyFromDir": "D:\\ImageMagick\\6.9.3-7-vc14-x64\\bin",
        "searchPattern": "IM_*.dll",
        "copyTo": "."
      }
    ]
  },
  "_comment": "Copy and paste this file into the folder where phpswitch application is. Modify path above to your real path. Then run the `phpswitch` command."
}

The phpVersionsDir key is where it contains multiple PHP versions folder or PHP versions folder as described above.
The phpRunningDir key is where PHP executable files from selected version will be copy to.

To skip start/stop the services, set webserverServiceName key to empty array [].
To not working with Apache web server, set apacheUpdateConfig key to false and you are free to remove apacheDir key.
The apacheDir key is where it must contain conf folder. The additionalCopy key is for copy additional files and/or folders. The PHP version number must be matched here. For more information about searchPattern, please read more at .Net document

Extract your application file.

Extract phpswitch appliation file from the zip file and place it together with phpswitch.json for easy usage.

Add path that contain phpswitch application into your System variables > Path.

Usage:

Open your command console and run the command phpswitch x.x where x.x is your PHP version number.

To use phpswitch.json from other path, add option --config-json to your command. Example: phpswitch 7.4 --config-json "D:\phpswitch.json".