Skip to content

Instantly share code, notes, and snippets.

@oczki
Last active March 10, 2021 07:11
Show Gist options
  • Save oczki/703952850d09e913d6109e621793fec8 to your computer and use it in GitHub Desktop.
Save oczki/703952850d09e913d6109e621793fec8 to your computer and use it in GitHub Desktop.
How to: Add a shortcut to .php files to run PHP server at their location

How to add a context menu shortcut to .php files, so that a PHP server is run there. Tested on Windows 10.

1. Download PHP

Versions 5.4+ have a built-in web server.

Download PHP from windows.php.net.

2. Write a batch script

Create a file run_php_server.bat:

cd "%1"
CALL "C:\Program Files\PHP\php-7.2.34-Win32-VC15-x64\php.exe" -S 127.0.0.1:8000
pause

Modify the location if it doesn't match yours.

Save the above script in some good location – in my case that would be C:\Program Files\PHP.

3. Create a registry entry

  1. Open Registry Editor (Start -> regedit -> Enter).
  2. Add a new key (will be shown as a folder) at HKEY_CLASSES_ROOT\SystemFileAssociations\.php\shell\PHP server here\command. If something doesn't exist along the path, create it.
  3. In command, double-click the default entry and set its value to "C:\Program Files\PHP\run_php_server.bat" "%1".
  4. You can now close Registry Editor, or wait until you test it in case something went wrong.

4. Usage

Right-click an index.php file. A context menu entry "PHP server here" should appear, and run a command line window when clicked.

Navigate to 127.0.0.1:8000 in your browser. It should work with your index file.


5. Bonus: Running via AutoHotkey script

To run the PHP server via AHK, the working directory has to be changed using pushd instead of cd.

Create a file run_php_server_at_given_directory.bat:

pushd "%1"
CALL "C:\Program Files\PHP\php-7.2.34-Win32-VC15-x64\php.exe" -S 127.0.0.1:8000
pause

Then, in your AHK script, add a Run call with path to the above batch file and path to the desired working directory (use a directory, don't point directly to the PHP file!):

^+p::Run "C:\Program Files\PHP\run_php_server_at_given_directory.bat" D:/path/to/working/directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment